In the screenshot below, my app is showing a title bar with a small white border on the left and right sides. How can I get rid of this border when setting a custom TitleView? In the case below, the red box should stretch from edge to edge of the screen, but you can see the small white border on either side.
Here I set up the NavigationPage.
public partial class App : Application
{
public App()
{
InitializeComponent();
ContainerRegistration.Register();
var authPage = FreshPageModelResolver.ResolvePageModel<LoginPageModel>();
var authPageNavigation = new FreshNavigationContainer(authPage, NavigationContainerNames.AuthenticationContainer);
MainPage = authPageNavigation;
}
}
Here is the XAML that references the navigation page to set the TitleView contents to a BoxView.
<?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:control="clr-namespace:WP.MobileMidstream.Device.Pages"
x:Class="WP.MobileMidstream.Device.Pages.LoginPage"
Visual="Material">
<NavigationPage.TitleView>
<BoxView BackgroundColor="Red" />
</NavigationPage.TitleView>
<ContentPage.Content>
<StackLayout Orientation="Vertical">
<Entry Placeholder="Username" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
It seems that the Navigation bar has a default Padding set (although i could not find that documented anywhere), and i could not find a way to change that (without using custom renderers).
Nevertheless, if what you are looking for is simply get the whole bar of a desired color, you could set the BarBackgroundColor property of your page as follows:
protected override void OnAppearing()
{
base.OnAppearing();
((NavigationPage)App.Current.MainPage).BarBackgroundColor = Color.Red;
}
I suggest you don't add BoxView in NavigationPage.TitleView, just set BarBackgroundColor in App.xaml.cs, like this:
<?xml version="1.0" encoding="utf-8" ?>
<!--<NavigationPage.TitleView>
<BoxView BackgroundColor="Red" VerticalOptions="CenterAndExpand" />
</NavigationPage.TitleView>-->
<ContentPage.Content>
<StackLayout Orientation="Vertical">
<Entry Placeholder="Username" />
</StackLayout>
</ContentPage.Content>
public App()
{
InitializeComponent();
MainPage = new NavigationPage(new MainPage()) { BarBackgroundColor=Color.Red};
}
It really depends on solution which you use for your toolbar.
Mostly updating toolbar in your android solution would be enough.
If it wont work it could be in your toolbar renderer (if you use) or in styles.xml
Check out for more solutions
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"
Full Toolbar.xml
<androidx.appcompat.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/activity_my_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
This saved me! Add it in App.xaml
under ResourceDictionary
<ResourceDictionary>
<!--Global Styles-->
<Style TargetType="NavigationPage">
<Setter Property="BarBackgroundColor" Value="Red"/>
<Setter Property="BarTextColor" Value="White"/>
</Style>
</ResourceDictionary>
Related
I have a menu with SideMenuView in xamarin , what I want to know if it is possible that when I slide my finger to the right the menu appears.
Now it only works for the main window for me, but the question is whether it can be set for all windows.
Xaml:
<Frame Padding="0" xct:SideMenuView.MainViewScaleFactor="1"
xct:SideMenuView.MenuAppearanceType="SlideIn"
xct:SideMenuView.MenuGestureEnabled="True"
xct:SideMenuView.MenuWidthPercentage="{OnPlatform Android=0.85, UWP=0.4, iOS=0.9}"
xct:SideMenuView.Position="LeftMenu"
BackgroundColor="Transparent" CornerRadius="0"
HasShadow="False" IsVisible="True">
c#:
switch (MenuState)
{
case SideMenuState.MainViewShown:
MenuState = SideMenuState.LeftMenuShown;
break;
case SideMenuState.LeftMenuShown:
MenuState = SideMenuState.MainViewShown;
break;
}
El ejemplo esta hecho con MVVM
You could use control templates.
Create a ContentViewTemplate:
<ContentView.Content>
<xct:SideMenuView x:Name="sideMenuView">
.....
</xct:SideMenuView>
</ContentView.Content>
Add this into Resources(App.xaml):
<Application.Resources>
<ControlTemplate x:Key="MainTemplate">
<controls:MainTemplate></controls:MainTemplate>
</ControlTemplate>
</Application.Resources>
Use this template in content page:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:controls="clr-namespace:App1"
x:Class="App1.MainPage"
ControlTemplate="{StaticResource MainTemplate}">
....
</ContentPage>
I have a xamarin.forms app that supports RTL, but when i convert a Page to RTL the NavigationPage.TitleView text disapears.
The NavigationPage.TitleView Code:
<NavigationPage.TitleView>
<Label Text="{Binding Title}" Style="{StaticResource TittleLabel}" HorizontalTextAlignment="Start" HorizontalOptions="Start" VerticalOptions="CenterAndExpand"></Label>
</NavigationPage.TitleView>
This is the Result screen.
Thanks in Advance.
For RTL, the Label having RTL issues. Please find the issue link below.
https://github.com/xamarin/Xamarin.Forms/issues/3611
When setting RTL to Page, the title view is disappeared. you can report to the Xamarin team. For instead of setting a title through Label, directly setting title property of ContentPage like below,
<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"
Title="RTL"
FlowDirection="RightToLeft">
It's working but RTL is not applying. But the title is displayed. You can report this issue also.
I guess maybe the binding do not work. I make a sample for your reference.
MainPage.xaml
<StackLayout>
<Button Clicked="Button_Clicked" Text="Title View Page" />
</StackLayout>
MainPage.xaml.cs
private void Button_Clicked(object sender, EventArgs e)
{
Navigation.PushAsync(new TitleViewPage());
}
TitleViewPage.xaml
<ContentPage.Resources>
<ResourceDictionary>
<Style x:Key="TittleLabel" TargetType="Label">
<Setter Property="TextColor" Value="Green" />
<Setter Property="FontSize" Value="Large" />
</Style>
</ResourceDictionary>
</ContentPage.Resources>
<NavigationPage.TitleView>
<StackLayout>
<Label
HorizontalOptions="Start"
HorizontalTextAlignment="Start"
Style="{StaticResource TittleLabel}"
Text="{Binding Title}"
VerticalOptions="CenterAndExpand" />
</StackLayout>
</NavigationPage.TitleView>
<ContentPage.Content>
<StackLayout>
<Label
HorizontalOptions="CenterAndExpand"
Text="Welcome to TitleView Page!"
VerticalOptions="CenterAndExpand" />
</StackLayout>
</ContentPage.Content>
TitleViewPage.xaml.cs
public string Title { get; set; }
public TitleViewPage()
{
InitializeComponent();
Title = "My TitleView";
this.BindingContext = this;
}
App.xaml.cs
public App()
{
InitializeComponent();
MainPage = new NavigationPage(new MainPage());
}
Updated:
When you set the FlowDirection to RightToLeft, the titleview could be seen on horizontal screen.
On the Right-to-left localization document, it lists the limitations.
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/localization/right-to-left
NavigationPage button location, toolbar item location, and transition animation is controlled by the device locale, rather than the FlowDirection property.
This question has also be reported. You could follow the update . https://github.com/xamarin/Xamarin.Forms/issues/9083
this aproach worked for me:
-set RTL-FlowDirection for main-layout (wrapper) of ContentPage's content (instead of ContentPage)
App.xaml
<Application.Resources>
<ResourceDictionary>
<Style x:Key="PageTitleStyle" TargetType="Label">
<Setter Property="HorizontalOptions" Value="EndAndExpand"/>
<Setter Property="Padding" Value="0,0,5,0"/>
</Style>
</ResourceDictionary>
</Application.Resources>
MainPage.xaml
<ContentPage ....>
<NavigationPage.TitleView >
<Label Text="{Binding Title}" Style="{StaticResource PageTitleStyle}" />
</NavigationPage.TitleView>
<StackLayout FlowDirection="RightToLeft">
....
</StackLayout>
</ContentPage>
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
I am using Zxing.Net.Mobile.Forms to display a barcode. I'm not sure how to add other elements along with the barcode on one page. I've tried adding it in both c# and xaml but my additional elements do not show. I want to add a label after the barcode and a picture above the barcode.
Barcode.xaml.cs
public partial class BarCode : ContentPage
{
ZXingBarcodeImageView barcode;
public BarCode()
{
InitializeComponent();
barcode = new ZXingBarcodeImageView
{
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand,
};
barcode.BarcodeFormat = ZXing.BarcodeFormat.CODE_128;
barcode.BarcodeOptions.Width = 300;
barcode.BarcodeOptions.Height = 150;
barcode.BarcodeOptions.Margin = 10;
barcode.BarcodeValue = Helpers.Settings.CardNumber;
Content = barcode;
}
}
Barcode.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"
x:Class="LoyaltyWorx.BarCode"
BackgroundImage="NewBg.jpg">
<ContentPage.Content>
<StackLayout>
</StackLayout>
</ContentPage.Content>
</ContentPage>
You are assigning Content with barcode. Hence, anything in the XAML will be overwritten.
You could do this instead. Add the ZXingBarcodeImageView to your XAML like:
<?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:zxing="clr-namespace:ZXing.Net.Mobile.Forms;assembly=ZXing.Net.Mobile.Forms"
x:Class="LoyaltyWorx.BarCode"
BackgroundImage="NewBg.jpg">
<ContentPage.Content>
<StackLayout>
<zxing:ZXingBarcodeImageView x:Name="Barcode"
BarcodeFormat="CODE_128"
HorizontalOptions="Fill" VerticalOptions="Fill"
WidthRequest="300" HeightRequest="150" Margin="10" />
<!-- add other stuff here -->
</StackLayout>
</ContentPage.Content>
</ContentPage>
Then you can remove your code in the constructor so it looks something like:
public partial class BarCode : ContentPage
{
public BarCode()
{
InitializeComponent();
Barcode.BarcodeValue = Helpers.Settings.CardNumber;
}
}
Bonus: If you are using the MVVM pattern, you could also bind BarcodeValue to a ViewModel and eliminate all the code behind by adding BarcodeValue="{Binding CardNumber}" to the ZXingBarcodeImageView in the XAML and somewhere setting the Binding Context.
Here is what you want:Showing barcode, an image on the center of it (overlapping), and a label after the barcode
<?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:zxing="clr-namespace:ZXing.Net.Mobile.Forms;assembly=ZXing.Net.Mobile.Forms"
x:Class="LoyaltyWorx.BarCode">
<ContentPage.Content>
<AbsoluteLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<Image Source="{Binding BarcodeImage}"
AbsoluteLayout.LayoutFlags="All"
AbsoluteLayout.LayoutBounds="0, 0, 1, 1"/>
<Image Source="{Binding OtherImage}"
AbsoluteLayout.LayoutFlags="PositionProportional"
AbsoluteLayout.LayoutBounds="0.5, 0.5, 100, 100"/>
</AbsoluteLayout>
<Label Text="MyLabel"/>
</ContentPage.Content>
</ContentPage>
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.