xamarin 如何在样式资源中包含OnPlatform?

tp5buhyn  于 2023-05-27  发布在  其他
关注(0)|答案(2)|浏览(177)

我有这样的风格:

<Style x:Key="pointButton" TargetType="Button">
    <Setter Property="BorderColor" Value="#999999"/>
    <Setter Property="BorderWidth" Value="1" />
    <Setter Property="TextColor" Value="#AAAAAA" />
    <Setter Property="FontAttributes" Value="Bold" />
    <Setter Property="HorizontalOptions" Value="FillAndExpand" />
    <Setter Property="VerticalOptions" Value="StartAndExpand" />
    <Setter Property="Margin" Value="10,10,10,10" />
    <Setter Property="HeightRequest" Value="40" />
</Style>

我想把这一点包括在内。这可能吗?

<Button.FontSize>
    <OnPlatform x:TypeArguments="x:Double" iOS="25" Android="20" />
</Button.FontSize>
tyu7yeag

tyu7yeag1#

通过新的XamarinOnPlatformspecification,可以直接在style标签中定义平台样式:

<Style x:Key="buttonStyle" TargetType="Button">
    <Setter Property="FontSize">
        <OnPlatform x:TypeArguments="x:Double">
            <On Platform="Android">40</On>
            <On Platform="iOS">10</On>
        </OnPlatform>
    </Setter>
</Style>
j9per5c4

j9per5c42#

将每个属性定义为OnPlatform

<OnPlatform x:Key="MyFontSize" x:TypeArguments="x:Double" iOS="14" Android="14" WinPhone="14" />

并以yout按钮样式引用它,如

<Style x:Key="MyButtonStyle" TargetType="{x:Type Button}">
    <Setter Property="FontSize" Value="{StaticResource MyFontSize}" />
</Style>

相关问题