XAML 在.NET MAUI选取器中使用本地化

rseugnpd  于 2023-08-01  发布在  .NET
关注(0)|答案(1)|浏览(133)

`我在.NET MAUI项目中创建了resx文件,以使用不同的语言。我想在选择器中使用它们。我在XAML中遵循了下面的方法,但它不起作用。我该怎么办?

<Picker Title="{x:Static resx:AppResources.PickerTitle}">
    <Picker.ItemsSource>
        <x:Array Type="{x:Type x:String}">
            <x:String>{x:Static resx:AppResources.Option1}</x:String>
            <x:String>{x:Static resx:AppResources.Option2}</x:String>
            <x:String>{x:Static resx:AppResources.Option3}</x:String>
        </x:Array>
    </Picker.ItemsSource>
</Picker>

字符串

idfiyjo8

idfiyjo81#

下面是一个如何使用两种语言(英语,德语)的示例,其中英语是默认语言:

<StackLayout Padding="20">
    <Label Text="{x:Static resources:AppResources.Title}" FontSize="24" />
    <Picker x:Name="languagePicker" Title="Select Language" SelectedIndexChanged="OnLanguagePickerIndexChanged">
        <Picker.ItemsSource>
            <x:Array Type="{x:Type x:String}">
                <x:String>English</x:String>
                <x:String>German</x:String>
            </x:Array>
        </Picker.ItemsSource>
    </Picker>
</StackLayout>

个字符
为每种语言创建资源文件。举例来说:

  • AppResources.resx(默认-英语)
  • AppResources.de.resx(德文)

打开每个资源文件,并为每种语言添加具有相同名称但不同值的本地化字符串。例如,对于“标题”键:
AppResources.resx(默认值-英语):

Key: Title, Value: My App


AppResources.de.resx(德文):

Key: Title, Value: Meine App

相关问题