XAML Shares
I am trying to implement the XAML equivalent of CSS styles. I want to create a custom layout for the ContentPage that I can use across all pages in my application and will have a different meaning for each platform.
Specifically, I'm starting with a custom add-on: I'm trying to put this code in my App.xaml file:
<Application.Resources>
<ResourceDictionary>
<OnPlatform x:Key="MyPadding"
x:TypeArguments="Thickness"
iOS="0, 20, 0, 0"
Android="0, 0, 0, 0"/>
<Style
x:Key="labelGreen"
TargetType="Entry">
<Setter
Property="TextColor"
Value="Green"/>
</Style>
</ResourceDictionary>
</Application.Resources>
In a separate ContentPage, I do the following, but it doesn't work:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyApp.LoginScreen"
Style="{DynamicResource MyPadding}"
>
Custom input style works great. But the gasket doesn't work. I am getting the error: "SetValue: Unable to convert Xamarin.Forms.OnPlatform`1 [Xamarin.Forms.Thickness] to enter" Xamarin.Forms.Style "
What am I doing wrong?
source to share
As the error says, Thickness
it is not Style
. Change it to:
<Application.Resources>
<ResourceDictionary>
<OnPlatform x:Key="MyPadding"
x:TypeArguments="Thickness"
iOS="0, 20, 0, 0"
Android="0, 0, 0, 0"/>
<Style
x:Key="pageStyle"
TargetType="ContentPage">
<Setter
Property="Padding"
Value="{StaticResource MyPadding}"/>
</Style>
<Style
x:Key="labelGreen"
TargetType="Entry">
<Setter
Property="TextColor"
Value="Green"/>
</Style>
</ResourceDictionary>
</Application.Resources>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyApp.LoginScreen"
Style="{StaticResource pageStyle}">
OR
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyApp.LoginScreen"
Padding="{StaticResource MyPadding}">
source to share