Xamarin change font in XAML - xamarin

How can I change the font of a label in Xamarin.Forms XAML?
Predictions are not available for font family attribute in XAML.
I tried 2 or 3 fonts like 'Arial' in font family, but it's not working.
Help me to resolve this.
<StackLayout Orientation="Vertical">
<Label Text="Welcome !"
VerticalOptions="Start"
HorizontalOptions="StartAndExpand"
FontFamily="Arial"
/>
</StackLayout>

store ttf file Location
Android: YouNameSpace.Droid/Assets/Poppins-Light.ttf
Ios: YouNameSpace.iOS/Resources/fonts/Poppins-Light.ttf
for iOS Need to put Below line in info.plist file
create static resource in App.xaml
<Application.Resources>
<ResourceDictionary>
<!-- FontFamily -->
<OnPlatform x:Key="PoppinsBold"
x:TypeArguments="x:String"
iOS="Poppins-Bold"
Android="Poppins-Bold.ttf#Poppins"/>
<OnPlatform x:Key="PoppinsLight"
x:TypeArguments="x:String"
iOS="Poppins-Light"
Android="Poppins-Light.ttf#Poppins" />
<!-- style for Lable -->
<Style x:Key="PoppinsBoldLabelStyle"
TargetType="{x:Type Label}">
<Setter Property="FontFamily"
Value="{StaticResource PoppinsBold}" />
</Style>
<Style x:Key="PoppinsLightLabelStyle"
TargetType="{x:Type Label}">
<Setter Property="FontFamily"
Value="{StaticResource PoppinsLight}" />
</Style>
</ResourceDictionary>
</Application.Resources>
use static resource in Lable
<Label Text="Hello" Style="{StaticResource PoppinsBoldLabelStyle}"/>

I resolved it by copying the font in 'Assets' folder for Android and changed the code as following:
<StackLayout Orientation="Vertical">
<Label Text="Hello Forms with XAML" FontFamily="ariblk.ttf#ariblk"/>
<Label Text="New Line following the first line " FontFamily="chiller.ttf#chiller" />
</StackLayout>

Please refer to this, and here is official demo.
Or you can use Custom renderers.
Here download the Arial, and put your Assets/Fonts folder.
MyLabel in your PCL:
namespace FontTes
{
public class MyLabel:Label
{
}
}
MyLabelRender in your Android platform:
using Android.Content;
using Android.Graphics;
using Android.Widget;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: ExportRenderer(typeof(FontTes.MyLabel), typeof(FontTes.Droid.MyLabelRender))]
namespace FontTes.Droid
{
class MyLabelRender: LabelRenderer
{
public MyLabelRender(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
var label = (TextView)Control;
label.Typeface = Typeface.CreateFromAsset(Android.App.Application.Context.Assets, "Fonts/arial.ttf");
}
}
}
PCL's MainPage.xaml:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:FontTes;assembly=FontTes"
x:Class="FontTes.MainPage">
<StackLayout>
<Label Text="Welcome to Xamarin.Forms!"
VerticalOptions="Center"
HorizontalOptions="Center"/>
<local:MyLabel
Text="Welcome to Xamarin.Forms!"
VerticalOptions="Center"
HorizontalOptions="Center"/>
</StackLayout>
</ContentPage>

Related

Xamarin fontawesome icons displayed as square with x

After following some tutorials to include FontAwesome 5 Free in my Xamarin project it always ends up showing as a square with an x inside.
I think this is probably related to my xamarin.forms version (2.3.0.46-pre3) and i can't seem to make it work.
Here's the file names
Build action is set to AndroidAsset and copy if newer and BundleResource for IOS
Xaml where im using them
<?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="TrainInURL.MainPage">
<Grid >
<Image Source="bg_home.png" VerticalOptions="Start" HorizontalOptions="Center"/>
<StackLayout Orientation="Vertical" HorizontalOptions="FillAndExpand">
<StackLayout Orientation="Horizontal" Margin="10" Padding="10" >
<Label Text="Bienvenid#" TextColor="White" Font="Bold,16"></Label>
<Label Text="" TextColor="White" FontFamily="{StaticResource FontAwesomeSolid}"/>
</StackLayout>
<StackLayout Orientation="Vertical" HorizontalOptions="FillAndExpand" Margin="10" Padding="10" >
<Label x:Name="txt_Nombre" TextColor="White" Font="Bold,16"></Label>
<Label x:Name="txt_Apellido" TextColor="White" Font="Bold,16"></Label>
<Label x:Name="lblMensaje" HorizontalTextAlignment="Center"></Label>
</StackLayout>
<StackLayout >
</StackLayout>
</StackLayout>
</Grid>
</ContentPage>
App.xaml
<?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TrainInURL.App">
<Application.Resources>
<ResourceDictionary>
<OnPlatform x:Key="FontAwesomeSolid" x:TypeArguments="x:String"
iOS="Font Awesome 5 Free Solid"
Android="FontAwesomeSolid.ttf#Font Awesome 5 Free Solid" />
<OnPlatform x:Key="FontAwesomeRegular" x:TypeArguments="x:String"
iOS="Font Awesome 5 Free Regular"
Android="FontAwesomeRegular.ttf#Font Awesome 5 Free Regular" />
<OnPlatform x:Key="FontAwesomeBrands" x:TypeArguments="x:String"
iOS="Font Awesome 5 Free Brands"
Android="FontAwesomeBrands.ttf#Font Awesome 5 Free Brands" />
</ResourceDictionary>
<!-- Application resource dictionary -->
</Application.Resources>
</Application>
info.plist
<key>UIAppFonts</key>
<array>
<string>FontAwesomeSolid.ttf</string>
<string>FontAwesomeRegular.ttf</string>
<string>FontAwesomeBrands.ttf</string>
</array>
Is it even possible to use custom fonts with my version? Any help is welcomed
Update 1: Updated to version 2.5.1.527436, same issue
Update 2: Tried using a different font file, seems to indicate it can find the font but can't render it. Using the following font renderer gave the following results
CustomFontRenderer
[assembly: ExportRenderer(typeof(CustomFontLabel), typeof(CustomFontRenderer))]
namespace TrainInURL.Droid
{
public class CustomFontRenderer : LabelRenderer
{
public CustomFontRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
TextView label = (TextView) Control;
if (e.NewElement?.FontFamily != null)
{
Typeface font = null;
// the try-catch block will ensure the element is at least rendered with default
// system font in Xamarin Previewer instead of crashing the view
try
{
font = Typeface.CreateFromAsset(Android.App.Application.Context.Assets, e.NewElement.FontFamily);
}
catch (Exception)
{
font = Typeface.Default;
}
label.Typeface = font;
}
}
}
}
Xaml
<StackLayout Orientation="Horizontal" Margin="10" Padding="10" >
<Label Text="Bienvenid#" TextColor="White" FontSize="16" FontAttributes="Bold"></Label>
<Label Text="" TextColor="White" FontFamily="{StaticResource FontAwesomeSolid}"/>
<Label Text="" TextColor="White" FontFamily="{StaticResource FontAwesomeBrands}"/>
<Label Text="" TextColor="White" FontFamily="{StaticResource FontAwesomeRegular}"/>
<trainInUrl:CustomFontLabel Text="" FontFamily="{StaticResource FontAwesomeSolid}"/>
<trainInUrl:CustomFontLabel Text="" FontFamily="{StaticResource FontAwesomeBrands}"/>
<trainInUrl:CustomFontLabel Text="" FontFamily="{StaticResource FontAwesomeRegular}"/>
</StackLayout>
Result
I followed the instructions found here which got me started. there was also a bit of further tweaking to completely get it working (particularly UWP). I do note you are using the .ttf files and I am using the .otf files, which could be an issue.
My final final assets folder and resource implementation are below (android - no IOS here), hopefully helps you out.
<Application.Resources>
<ResourceDictionary>
<OnPlatform x:TypeArguments="x:String" x:Key="FontAwesomeBrands">
<On Platform="Android" Value="FontAwesome5Brands400.otf#Regular"/>
<On Platform="UWP" Value="/Assets/Fonts/FontAwesome5Brands400.otf#Font Awesome 5 Brands"/>
</OnPlatform>
<OnPlatform x:TypeArguments="x:String" x:Key="FontAwesomeSolid">
<On Platform="Android" Value="FontAwesome5Solid900.otf#Regular"/>
<On Platform="UWP" Value="/Assets/Fonts/FontAwesome5Solid900.otf#Font Awesome 5 Free"/>
</OnPlatform>
<OnPlatform x:TypeArguments="x:String" x:Key="FontAwesomeRegular">
<On Platform="Android" Value="FontAwesome5Regular400.otf#Regular"/>
<On Platform="UWP" Value="/Assets/Fonts/FontAwesome5Regular400.otf#Font Awesome 5 Free"/>
</OnPlatform>
</ResourceDictionary>
</Application.Resources>
Updated the app to version 4.4 and that solved the issue

Xamarin Forms NavigationPage.TitleView RTL, the content disappered

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>

How can I create a new Xamarin Element based on a Frame with a StackLayout inside of it?

I have a Frame with a StackLayout inside of it:
<Frame CornerRadius="1" HasShadow="false" Margin="10"
BackgroundColor="White" BorderColor="Silver" Padding="0" >
<StackLayout Orientation="Vertical" Spacing="0" Padding="0" >
<xaml:PtiXaml />
<template:LineTemplate />
<xaml:AtiXaml />
<template:LineTemplate />
<xaml:StiXaml />
</StackLayout>
</Frame>
Can I create a new object called NewFrame that is the same as the Frame with the StackLayout inside?
<template:NewFrame>
<xaml:PtiXaml />
<template:LineTemplate />
<xaml:AtiXaml />
<template:LineTemplate />
<xaml:StiXaml />
</template:NewFrame>
or
<template:NewFrame>
<xaml:ABCXaml />
</template:NewFrame>
or
<template:NewFrame>
<Label Text="X" />
</template:NewFrame>
It was suggested I use a Custom View but I have looked and can not find an example of this where it contains other elements inside.
Right-Click at the desired position in your Shared Project (or PCL) in your Solution Explorer (I would recommend adding a folder named "Views" or "CustomViews" and creating the item inside that folder), select "Add new item" and choose "Content View" (without (C#) behind it. The filename should be something like "View1.xaml", you can change that due to your liking, however the important thing is that the xaml extension is there.
This will create a new ContentView with a xaml and xaml.cs file.
Inside the xaml file you can declare your xaml code posted above and write any code necessary into the xaml.cs file.
Now you can add a namespace declaration to the page you want to put your view into:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
...
xmlns:customs="clr-namespace:YourNamespace.Views;assembly=YourNamespace"
and declare the element in that Page's or any layout's content:
<customs:CustomViewName ... />
If you want to be able to control the element's behaviour you can add BindableProperties in the codebehind.
For more in-depth information on that, you might want to take a look into this article: https://visualstudiomagazine.com/articles/2017/10/01/add-custom-controls.aspx
Use a ContentView along with a ControlTemplate to create a Custom Control. This way you can create a new control called NewFrame, write the XAML for your control and then use the <ContentPresenter> tag inside your <ControlTemplate> to assign where you'd like your content to be.
Like so:
.
└── NewFrame
├── NewFrame.cs
└── NewFrame.xaml -> Is a ResourceDictionary
NewFrame.cs:
namespace TestApp.Controls
{
public partial class NewFrame : ContentView
{
}
}
NewFrame.xaml:
<ResourceDictionary
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:newFrame="clr-namespace:TestApp.Controls"
x:Class="Namespace.For.A.ResourceDictionary">
<Style TargetType="newFrame:NewFrame">
<Setter Property="ControlTemplate">
<Setter.Value>
<ControlTemplate>
<ContentView BackgroundColor="Transparent">
<Frame CornerRadius="1" HasShadow="false" Margin="10" BackgroundColor="White" BorderColor="Silver" Padding="0" >
<StackLayout Orientation="Vertical" Spacing="0" Padding="0">
<ContentPresenter/>
</StackLayout>
</Frame>
</ContentView>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
ConsumingYourControl.xaml:
<template:NewFrame>
<template:NewFrame.Content>
<xaml:PtiXaml />
<template:LineTemplate />
<xaml:AtiXaml />
<template:LineTemplate />
<xaml:StiXaml />
</template:NewFrame.Content>
</template:NewFrame>
<template:NewFrame>
<template:NewFrame.Content>
<xaml:ABCXaml />
</template:NewFrame.Content>
</template:NewFrame>
<template:NewFrame>
<template:NewFrame.Content>
<Label Text=""/>
</template:NewFrame.Content>
</template:NewFrame>

Make sure an item is picked with Picker Xamarin Forms

I'm currently working on a cross-platform app build with Xamarin Forms and I've several picker. I need to be sure all the pickers have a selected item, and if not I'd like to set their background to red.
I've already tried to loop to each element of the stacklayout to pick all Pickers and check their selected items but it's not working (it seems that my layout have only 2 children and no pickers). I cant't see how to do this with behavior too.
My loop (in code behind)
public void checkChampsVides()
{
for (int i = 0; i < DiagHabitat.Children.Count(); i++)
{
DisplayAlert("e", DiagHabitat.Children.GetHashCode().ToString(), "ok");
if (DiagHabitat.Children.ElementAt(i).GetType() == typeof(Picker))
{
Picker p = DiagHabitat.Children.ElementAt(i) as Picker;
if (p.SelectedIndex == 0)
p.BackgroundColor = Color.Red;
}
}
}
Xaml
<ContentPage
Title="Diagnostic Habitat"
Padding="20"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="XXX.DiagnosticHabitatPage">
<ContentPage.Resources>
<ResourceDictionary>
<Style x:Key= "BoutonSauvegarde" TargetType="Button" >
<Setter Property="BackgroundColor" Value="#6AD0C6"/>
</Style>
</ResourceDictionary>
</ContentPage.Resources>
<StackLayout x:Name="DiagHabitat">
<ProgressBar Progress="1"/>
<TableView x:Name ="DiagnosticHabitat" Intent="Form" HasUnevenRows="True">
<TableRoot Title="Diagnostic habitat">
<TableSection Title="Title1">
<ViewCell>
<StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand" >
<Label VerticalOptions="Center" Text="text1"/>
<Picker x:Name="accesPorteEntree" HorizontalOptions="FillAndExpand" SelectedIndex="{Binding DiagHabitatAjoute.AccesPorteEntreeEPC, Mode=OneWayToSource}" >
<Picker.Items>
<x:String>Seuil haut</x:String>
<x:String>Seuil bas</x:String>
<x:String>Sans seuil</x:String>
</Picker.Items>
</Picker>
</StackLayout>
</ViewCell>
<ViewCell>
<StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand" >
<Label VerticalOptions="Center" Text="text2"/>
<Picker x:Name="niveauSecuAcces" HorizontalOptions="FillAndExpand" SelectedIndex="{Binding DiagHabitatAjoute.SecuAccesEPC, Mode=OneWayToSource}">
<Picker.Items>
<x:String>Bas</x:String>
<x:String>Moyen</x:String>
<x:String>Haut</x:String>
</Picker.Items>
</Picker>
</StackLayout>
</ViewCell>
<ViewCell>
<StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand" >
<Label VerticalOptions="Center" Text="Largeur circulation"/>
<Picker x:Name="largeurCirculation" HorizontalOptions="FillAndExpand" SelectedIndex="{Binding DiagHabitatAjoute.LargeurCircuEPC, Mode=OneWayToSource}" >
<Picker.Items>
<x:String>Inf à 75 cm</x:String>
<x:String>75 - 90 cm</x:String>
<x:String>Sup à 90 cm</x:String>
</Picker.Items>
</Picker>
</StackLayout>
</ViewCell>
...
You can use triggers, and apply them using implicit Style.
<TableView.Resources>
<ResourceDictionary>
<Style TargetType="Picker">
<Setter Property="BackgroundColor" Value="Green" />
<Style.Triggers>
<Trigger TargetType="Picker" Property="SelectedItem" Value="{x:Null}">
<Setter Property="BackgroundColor" Value="Red" />
</Trigger>
<Trigger TargetType="Picker" Property="SelectedIndex" Value="0">
<Setter Property="BackgroundColor" Value="Red" />
</Trigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
</TableView.Resources>
You can recursively descend through your top most Layout container that holds the Pickers and use some switch pattern matching to determine when to recurse into the next container.
public void CheckPickers(Layout layout)
{
foreach (var child in layout.Children)
{
switch (child)
{
case Picker picker:
if (picker.SelectedIndex <= 0)
picker.BackgroundColor = Color.Red;
else
picker.BackgroundColor = Color.Green;
break;
case Layout l:
CheckPickers(l);
break;
default:
break;
}
}
}

how to define/override Style for ContentView when it is child of ContentPage

I have contentview as below
<?xml version="1.0" encoding="utf-8" ?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:convertors="clr-namespace:myApp.Convertors;assembly=myApp"
x:Class="myApp.cwPart">
<ContentView.Content>
<StackLayout Orientation="Horizontal"
BackgroundColor="White">
<RelativeLayout Style="{StaticResource myStyle}"
HorizontalOptions="Start"
VerticalOptions="Center">
I am referencing this ContentView inside multiple ContentPages as example one below. Every ContentPage should set different BackgroundColor, Height and Width. That's why I thought that style should be defined in the ContentPage but it is throwing error that it is not recognized. How can I achieve this?
PS, interesting thing I am using Gorilla player to achieve such changes and Gorilla player doesn't return any error :)
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:convertors="clr-namespace:myApp.Convertors;assembly=myApp"
xmlns:c="clr-namespace:myApp;assembly=myApp"
x:Class="myApp.AppLogsPage"
Title="{Binding AppName}"
x:Name="AppLogsPage">
<ContentPage.Resources>
<ResourceDictionary>
<Style x:Key="myStyle" TargetType="RelativeLayout">
<Setter Property="HeightRequest" Value="148"/>
<Setter Property="WidthRequest" Value="80"/>
<Setter Property="BackgroundColor" Value="White"/>
</Style>
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
<StackLayout x:Name="MainHolder">
<c:cwPart />
...
If I define ContentView Resources as below, it works fine but I am not able to override the Style defined inside ContentView
<?xml version="1.0" encoding="utf-8" ?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:convertors="clr-namespace:myApp.Convertors;assembly=myApp"
x:Class="myApp.cwPart">
<ContentView.Resources>
<ResourceDictionary>
<Style x:Key="myStyle" TargetType="RelativeLayout">
<Setter Property="HeightRequest" Value="148"/>
<Setter Property="WidthRequest" Value="80"/>
<Setter Property="BackgroundColor" Value="White"/>
</Style>
</ResourceDictionary>
</ContentView.Resources>
<ContentView.Content>
<StackLayout Orientation="Horizontal" BackgroundColor="White">
<RelativeLayout Style="{StaticResource myStyle}"
HorizontalOptions="Start" VerticalOptions="Center" >
There are many issues i would change in your approach but let's keep it simple. First of all i would use contentview with control template as;
in App.xaml;
<ControlTemplate x:Key="MyControlTemplate">
<StackLayout Orientation="Horizontal" BackgroundColor="White">
<RelativeLayout HorizontalOptions="Start" VerticalOptions="Center" />
</StackLayout>
</ControlTemplate>
then in your page.xaml
<ContentView ControlTemplate={StaticResource MyControlTemplate}/>
if you want to set individual styles for every page then you can create a control template for each of them.
To create a WPF like user control;
in App.xaml;
<ControlTemplate x:Key="MyControlTemplate">
<StackLayout Orientation="Horizontal" BackgroundColor="White">
<RelativeLayout HorizontalOptions="Start" VerticalOptions="Center" />
</StackLayout>
</ControlTemplate>
in the same project
public class CustomView: ContentView
{
public CustomView()
{
ControlTemplate = (ControlTemplate)Application.Current.Resources.FirstOrDefault(x => x.Key == "MyControlTemplate").Value;
}
}
then you can use CustomView control/view in any xaml page. Don't forget to add xaml namespace of the CustomView in consuming xaml page.

Resources