gpt4 book ai didi

c# - XAML 共享资源

转载 作者:行者123 更新时间:2023-11-30 16:06:44 24 4
gpt4 key购买 nike

我正在尝试实现 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}"
>

自定义 Entry 样式工作正常。但是填充没有。我收到错误消息:“SetValue:无法将 Xamarin.Forms.OnPlatform`1[Xamarin.Forms.Thickness] 转换为键入‘Xamarin.Forms.Style’”

我做错了什么?

最佳答案

正如错误所说,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}">

关于c# - XAML 共享资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31814229/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com