Cant add button to already created FlexLayout - xamarin

Tis is the xml file
<ContentPage Title="Teras">
<ContentPage.Content>
<ScrollView>
<FlexLayout x:Name="teras">
<Label Text="Teras"/>
</FlexLayout>
</ScrollView>
</ContentPage.Content>
</ContentPage>
see i already have FlexLayout
i want to add button with code but i cant include the button to this layout :/ im new on xamarin

var button = new Button { .. };
teras.Children.Add(button);

Related

Xamarin Forms - how do colors work on Navigation bar for iOS

I am trying to set the BarBackgroundColor and BarTextColor for a simple XF app, and am not understanding the output. A picture of what I am seeing follows the code, and all the code can be downloaded here.
I set BarBackgroundColor, BarTextColor, and BackgroundColor below. It seems the only color (from the attached image) being set is the Color.Blue BarBackgroundColor. Shouldn't the text in the navigation bar and status bar be White? Why isn't the rest of the page Red?
App.xaml.cs
public App()
{
InitializeComponent();
var navPage = new NavigationPage(new MainPage());
navPage.BarBackgroundColor = Color.Blue;
navPage.BarTextColor = Color.White;
navPage.BackgroundColor = Color.Red;
MainPage = navPage;
}
XAML
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="SampleApp.MainPage">
<NavigationPage.TitleView>
<StackLayout HorizontalOptions="Center"
VerticalOptions="Center">
<Label Text="Page Title" />
</StackLayout>
</NavigationPage.TitleView>
<StackLayout>
<!-- Place new controls here -->
<Label Text="Welcome to Xamarin.Forms!"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand" />
</StackLayout>
</ContentPage>
Here is the output I am getting.
Shouldn't the text in the navigation bar and status bar be White?
Yes , if code as shared , will not .
First , we will check how to set the text color in the navigation bar .
Because setting custom title view in Navigation Bar as follow :
<NavigationPage.TitleView>
<StackLayout HorizontalOptions="Center"
VerticalOptions="Center">
<Label Text="Page Title" />
</StackLayout>
</NavigationPage.TitleView>
Then code navPage.BarTextColor = Color.White; will not work .The reason is that , you set a custom title view in ContentPage ,and ContentPage Overrides the effect of the navigation bar text .
If want to show white color of Naviagtion Title , there are two ways based on shared code .
One is : Set TextColor for Label in custom title view .
<NavigationPage.TitleView>
<StackLayout HorizontalOptions="Center"
VerticalOptions="Center">
<Label Text="Page Title" TextColor = "White" />
</StackLayout>
</NavigationPage.TitleView>
Another is : Using original navigation bar title .
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="SampleApp.MainPage"
Title="Page Title"> // Adding title text here
<StackLayout>
<!-- Place new controls here -->
<Label Text="Welcome to Xamarin.Forms!"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand" />
</StackLayout>
</ContentPage>
Then code navPage.BarTextColor = Color.White; will work for text of naviagtion bar.
Second, we will check how to set the text color in the status bar .
Here you need to enter iOS Solution folder, then open Info.plist file with Xml Editor as follow :
Then adding the Key UIViewControllerBasedStatusBarAppearance and set its value to false as follow.
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
Saving and rebuliding project , now code navPage.BarTextColor = Color.White; can work for
text color of status bar .
Why isn't the rest of the page Red?
This problem should be different for Android . If running navPage.BackgroundColor = Color.Red; in Android ,it will work.However will not in iOS.
The reason is that , Navigation Controller is different between Android and iOS. The Navigation Bar just a bar in Android , however Navigation Bar can be a whole Page view in iOS. That means if setting background color to NavigationPage in Android , it can work in Navigation Page and ContentPage. However , in iOS it only can work in Navigation page . If Want content page show different , you should individually set in Content page.
Therefore , setting as follow in iOS background color can be work.
//Adding background color here can work
<StackLayout>
<!-- Place new controls here -->
<Label Text="Welcome to Xamarin.Forms!"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand" />
</StackLayout>
Final effect as follow:

How to bind a nested Layout with external ContentView in Xamarin.Forms?

I have a Page with a StackLayout using BindableLayout.ItemsSource, inside each item I have a ListView, for each item in this nested ListView I need to do a Binding to a property on the Page's ViewModel. I'm trying to use the Source+Path approach but the app crashes as soon as I open the page that contains this structure.
MainPage.xaml:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
x:Class="BindableLayoutReferenceBug.ListViewPage"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:BindableLayoutReferenceBug">
<StackLayout BindableLayout.ItemsSource="{Binding Items}">
<BindableLayout.ItemTemplate>
<DataTemplate>
<local:MessageListViewTemplate />
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
</ContentPage>
MessageListViewTemplate.xaml:
<?xml version="1.0" encoding="UTF-8" ?>
<ContentView
x:Class="BindableLayoutReferenceBug.MessageListViewTemplate"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Name="listView">
<ListView ItemsSource="{Binding Options}">
<ListView.ItemTemplate>
<DataTemplate>
<StackLayout>
<Label Text="{Binding .}" />
<Button
BackgroundColor="Blue"
Command="{Binding Source={x:Reference Name=listView}, Path=Parent.BindingContext.ShowMessageCommand}"
CommandParameter="{Binding .}"
Text="Show Message" />
</StackLayout>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentView>
The exception shows that there is a problem finding the reference to the x:Name I used: Can not find the object referenced by listView
This only happens when I have a nested structure as I mentioned (StackLayout with BindableLayout > ListView). I'm not sure if this is not a supported scenario or if it is a bug.
I tried using different Paths for the binding, but since this is a problem parsing the XAML not finding the x:Name referenced, I don't think it even starts evaluating my Path.
Am I doing something wrong or is this a bug in Xamarin.Forms?
Xamarin.Forms version used: 3.6.0.293080
Repro sample: https://github.com/akamud/BindableLayoutReferenceBug
Maybe, x:Name wasn't recognized at runtime even though you have set it to your content view in XAML. You could raise an issue on GitHub.
Here I tried this binding using code behind and it works fine. You could use it as an alternative:
public MessageListViewTemplate()
{
InitializeComponent();
listView.ItemTemplate = new DataTemplate(() =>
{
ViewCell viewCell = new ViewCell();
Label label = new Label();
label.SetBinding(Label.TextProperty, new Binding("."));
Button button = new Button() { Text = "Show Message", BackgroundColor = Color.Blue };
button.SetBinding(Button.CommandProperty, new Binding("Parent.BindingContext.ShowMessageCommand", source: ContentView));
button.SetBinding(Button.CommandParameterProperty, new Binding("."));
viewCell.View = new StackLayout
{
Children =
{
label,
button
}
};
return viewCell;
});
}
XAML:
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="BindableLayoutReferenceBug.MessageListViewTemplate"
x:Name="ContentView">
<StackLayout>
<ListView ItemsSource="{Binding Options}" x:Name="listView" HasUnevenRows="True">
</ListView>
</StackLayout>
</ContentView>
Here, I defined the parent content view as ContentView and named ListView listView.
Please notice that the list view's data template should be a view cell. So I used a view cell to wrap the content here.
It was a bug after all, it is now fixed and the above should work: https://github.com/xamarin/Xamarin.Forms/issues/6192

How to add entry inside picker popup? Xamarin Forms

I have a picker with more than one hundred data. I want to search a particular item. How can I add a entry inside a picker popup? Is it possible? If not, What are the best alternative to search data inside a picker?
What you want might be able to be done via custom renderer, but I personally don't know how that would be done...
If you want custom functionality in a picker, I highly recommend Rg.Plugins.Popup. This package allows a user to define there own custom popup. I use this for all my Popup pages/controls in the application I develop.
GitHub: Rg.Plugins.Popup
Define a PopupPage with an Entry and ListView and you can provide the kind of functionality you are wanting.
There is no out of the box solution for this. But a picker with searchbar can be implemented with rg.plugins.popup.(https://github.com/rotorgames/Rg.Plugins.Popup).
Create a view with label and down arrow.Provide click for that.In that click event call rg.popup.
The design of popup for referance.
<?xml version="1.0" encoding="utf-8" ?>
<pages:PopupPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="iVerify.Sample"
xmlns:pages="clr-namespace:Rg.Plugins.Popup.Pages;assembly=Rg.Plugins.Popup"
xmlns:animations="clr-namespace:Rg.Plugins.Popup.Animations;assembly=Rg.Plugins.Popup"
BackgroundColor="Transparent" >
<pages:PopupPage.Animation>
<animations:ScaleAnimation
PositionIn="Center"
PositionOut="Center"
ScaleIn="1.2"
ScaleOut="0.8"
DurationIn="400"
DurationOut="300"
EasingIn="SinOut"
EasingOut="SinIn"
HasBackgroundAnimation="False"/>
</pages:PopupPage.Animation>
<StackLayout VerticalOptions="Center" Padding="10" HorizontalOptions="FillAndExpand" Margin="0,20,0,0" >
<StackLayout>
<SearchBar x:Name="Searchbar" />
</StackLayout>
<StackLayout>
<ListView x:Name="ListView" ItemsSource="{Binding}"
ItemSelected="List_Tapped">
ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
// Your controls
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</StackLayout>
</StackLayout>
</pages:PopupPage>
By using Itemselected property you can get the value of tapped view. Then set it to the label of Mainpage(label that clciked to open the popup).This can be achieved by messeging centre.
Sample screenshot
I did it with Telerik though it didn't launch on Android in main project for me and the dropdown behaves strange on UWP - it is dropping up instead of down for some layout constellations.
Telerik is not free though, so if you have it you can use:
<StackLayout Orientation="Horizontal"
VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand">
<telerikInput:RadAutoCompleteView x:Name="autoCompleteView"
HorizontalOptions="FillAndExpand"
ShowSuggestionView="True"
ShowMoreItems="True"
SuggestMode="Suggest"
Watermark="Search here..." />
<Button Clicked="Button_Clicked"
Text="▼"/>
</StackLayout>
and in code behind
private void Button_Clicked(object sender, System.EventArgs e)
{
autoCompleteView.ShowSuggestions();
}

Edit elements in C# after making them in XAML

I have created a couple of entries and buttons in XAML (no C# code yet).
Is it possible to edit or even add elements in C# code to a StackLayout which was made in XAML?
For example, having the following XAML code:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:ORTEC_MOBILE"
x:Class="ORTEC_MOBILE.MainPage"
BackgroundColor="#0074ff">
<RelativeLayout>
<StackLayout HorizontalOptions="FillAndExpand"
RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.25,Constant=0}"
RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width,Factor=1,Constant=0}">
<Label Text="Login" FontSize="36" TextColor="White" HorizontalTextAlignment="Center">
</Label>
<Label Text="Welcome back! Please log into your account." FontSize="12" TextColor="White" HorizontalTextAlignment="Center">
</Label>
</StackLayout>
</RelativeLayout>
Is it possible to add a button to the layout using C# code?
Yes, definitely possible. When you give an element an attribute of x:Name="element" it is available in code-behind as "element". See this sample on the Xamarin site to see how they call elements in code:
https://github.com/xamarin/xamarin-forms-book-samples/tree/master/Chapter08/XamlClock
The x:Name attribute allows an object instantiated in XAML to be given a name. The rules for these names are the same as for C# variable names. Following the return of the InitializeComponent call in the constructor, the code-behind file can refer to these names to access the corresponding XAML element. The names are actually converted by the XAML parser into private fields in the generated partial class.
It is definately possible to add in controls programatically from the code behind, I'll give you an example below.
It's probably useful to give the component you wish to add a control to, a name, that way it can be accessed from the class behind the xaml.
So in this instance I've simply attributed a name to your xaml stack layout.
XAML:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:ORTEC_MOBILE"
x:Class="ORTEC_MOBILE.MainPage"
BackgroundColor="#0074ff">
<RelativeLayout>
<StackLayout x:Name="mainStackLayout" HorizontalOptions="FillAndExpand"
RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.25,Constant=0}"
RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width,Factor=1,Constant=0}">
<Label Text="Login" FontSize="36" TextColor="White" HorizontalTextAlignment="Center">
</Label>
<Label Text="Welcome back! Please log into your account." FontSize="12" TextColor="White" HorizontalTextAlignment="Center">
</Label>
</StackLayout>
</RelativeLayout>
C# Backing Class:
Public void SomeMethodThatsInMyBackingClass()
{
Label newLabel = new Label {
Text = "Hello, Forms!",
FontSize = Device.GetNamedSize (NamedSize.Large, typeof(Label)),
VerticalOptions = LayoutOptions.CenterAndExpand,
HorizontalOptions = LayoutOptions.CenterAndExpand,
},
mainStackLayout.Children.Add(newLabel);
}
Using the above as a rough example should be enough for you to replicate it in your own project. Good luck.

How to remove the Xamarin.Forms Navigation Bar?

Is there any way to remove the Navigation Bar from Xamarin.Forms - Portable (xaml) in Android?
I want to remove the "less than sign" ('<') and the application icon which appears above the content page of the Xamarin.Forms xaml.
You can remove navigation bar from Xaml using Xamarin.Forms using below code.
NavigationPage.SetHasNavigationBar (this, false);
Where this stands for current page / form instance.
Hope this helps!
NavigationPage.SetHasNavigationBar(this, false);
The above mentioned is not the good solution.
By using this code it disable the NavigationBar present in the page.
We can achieve the real solution only by creating a NavigationRenderer for NavigationPage for Android.
void RemoveAppIconFromActionBar()
{
var actionBar = ((Activity)Context).ActionBar;
actionBar.SetIcon (new ColorDrawable (Color.Transparent.ToAndroid ()));
}
Refer the Github for the complete code snippet : https://gist.github.com/Vaikesh/f86d1968c8166519f102#file-customnavigationrenderer-cs
It's called the "back button" and it is available in the action bar. you can remove it using:
NavigationPage.SetHasBackButton(this, false);
The easiest way to achieve this is to add NavigationPage.HasNavigationBar = "false" in your ContentPage
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="SterlingSwitch.Pages.Page1"
NavigationPage.HasNavigationBar="False">
<ContentPage.Content>
<StackLayout>
<Label Text="Welcome to Xamarin.Forms!"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
The Best way to achieve this from xml page
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
x:Class="ICLDC.Digital.General.Pages.AboutApp.AboutApplication"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"
xmlns:local="clr-namespace:ICLDC.Digital.General.Pages.Generic"
xmlns:translate="clr-namespace:ICLDC.Digital.General.Helpers"
ios:Page.UseSafeArea="True"
NavigationPage.HasNavigationBar="False">
<ContentPage.Content>
<StackLayout
BackgroundColor="White"
HorizontalOptions="FillAndExpand"
Spacing="0"
VerticalOptions="FillAndExpand"/>
</ContentPage.Content>
</ContentPage>

Resources