XAML 使用StaticResource呈现字符串文字

8yoxcaq7  于 11个月前  发布在  其他
关注(0)|答案(2)|浏览(113)

我在Microsoft Learn.学习MAUI
这让我想起了XAML中的Silverlight/WPF。
鉴于以下情况:

<Label>
    <Label.Resources>
        <OnPlatform x:Key="PlatformName" x:TypeArguments="system:String">
            <On Platform="iOS" Value="[iOS]" />
            <On Platform="Android" Value="[ANdroid]" />
            <On Platform="WinUI" Value="[Windows]" />
            <On Platform="MacCatalyst" Value="[macOS]" />
        </OnPlatform>
    </Label.Resources>
    <Label.Text>{StaticResource PlatformName}</Label.Text>
</Label>

字符串
如何将Label.Text属性设置为PlatformName资源的内容(字符串)?

wsewodh2

wsewodh21#

您可以尝试将StaticResource放在ContentPage.Resource中。例如:

<ContentPage.Resources>
    <OnPlatform x:Key="PlatformName" x:TypeArguments="system:String">
         <On Platform="iOS" Value="[iOS]" />
         <On Platform="Android" Value="[ANdroid]" />
         <On Platform="WinUI" Value="[Windows]" />
         <On Platform="MacCatalyst" Value="[macOS]" />
    </OnPlatform>
</ContentPage.Resources>

<Label Text="{StaticResource PlatformName}"/>

字符串

2nc8po8w

2nc8po8w2#

如果没有特定的理由使用StaticResource,那么你可以简单地这样做:

<Label>
    <Label.Text>
        <OnPlatform x:TypeArguments="system:String">
            <On Platform="iOS" Value="[iOS]" />
            <On Platform="Android" Value="[ANdroid]" />
            <On Platform="WinUI" Value="[Windows]" />
            <On Platform="MacCatalyst" Value="[macOS]" />
        </OnPlatform>
    </Label.Text>
</Label>

字符串
或者简单地使用{OnPlatform}标记扩展:

<Label
    Text="{OnPlatform iOS='[iOS]', Android='[ANdroid]', WinUI='[Windows]', MacCatalyst='[macOS]'}" />

相关问题