我正在尝试实现相当于CSS样式的XAML。我想为ContentPage创建一个自定义布局,我可以在我的应用程序的所有页面中使用它,并且每个平台都有不同的值。
具体来说,我从自定义填充开始:我正在尝试将此代码放在App.xaml文件中:
<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>
在单独的ContentPage中,我正在执行以下操作,但它不起作用:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyApp.LoginScreen"
Style="{DynamicResource MyPadding}"
>
自定义条目样式工作正常。但填充没有。我收到错误:“SetValue:无法转换Xamarin.Forms.OnPlatform`1 [Xamarin.Forms.Thickness]以输入'Xamarin.Forms.Style'”
我做错了什么?
答案 0 :(得分:3)
正如错误所述,Thickness
不是Style
。将其更改为:
<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}">
或强>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyApp.LoginScreen"
Padding="{StaticResource MyPadding}">