如何在Windows应用商店应用程序中本地化通知和组合框?(C#/XAML,多语言应用程序工具包)
我在windows应用商店应用程序本地化方面有几个问题。我能够本地化xaml内容,如TextBlock.Text或Button.Content(),但我不知道如何本地化以下内容: 1) 。我的组合框中的项目如何在Windows应用商店应用程序中本地化通知和组合框?(C#/XAML,多语言应用程序工具包),windows,localization,windows-runtime,windows-store-apps,winrt-xaml,Windows,Localization,Windows Runtime,Windows Store Apps,Winrt Xaml,我在windows应用商店应用程序本地化方面有几个问题。我能够本地化xaml内容,如TextBlock.Text或Button.Content(),但我不知道如何本地化以下内容: 1) 。我的组合框中的项目 <ComboBox x:Name="comboBoxTopAppBar" Width="120" Margin="10,0,0,0" MinWidth="200" SelectedItem="{Binding SelectedStatus,
<ComboBox x:Name="comboBoxTopAppBar" Width="120" Margin="10,0,0,0" MinWidth="200"
SelectedItem="{Binding SelectedStatus, Mode=TwoWay}">
<x:String>Item 1</x:String>
<x:String>Item 2</x:String>
<x:String>Item 3</x:String>
</ComboBox>
3) 。C#代码中的Toast通知(我正在使用《Windows运行时通过C#》一书中的类库)
我如何将其本地化?有趣的是,它们都联系在一起 对于2)和3)您需要创建一个控制器,该控制器将保存
ResourceLoader
对象。您可以使用(如果使用Windows 8.1),ResourceLoader.GetForIndependentUse()
在ResourceController
中创建一个名为GetTranslation(字符串资源)
的方法。它看起来像这样:
private static ResourceLoader resourceLoader = ResourceLoader.GetForIndependentUse();
public static string GetTranslation(string resource)
{
return resourceLoader.GetString(resource);
}
然后,无论何时需要静态编码转换,只要调用ResourceController.GetString(*所需字符串的键*)
这对于简单的代码调用非常有效,但不能直接用于Xaml,例如场景1)。不过不要害怕,因为你有转换器的魔力
public class ResourceTranslationConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
var valString = value as string;
// If what is being converted is a string, return the resource translation
// Else return something else, such as the object itself
return valString == null ? value : ResourceController.GetString(valString);
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
然后,您只需定义一次转换器(可能从所有xaml都可以访问,也可能是合并到App.xaml
)中的字典),您可以随时在绑定中引用它
在本例中,类似于:
<!-- This is defined somewhere accessible, or just in the Page Resources -->
<!-- 'converters' is whichever namespace definition your converter is in -->
<converters:ResourceTranslationConverter x:Key="ResourceTranslationConverter"/>
<ComboBox x:Name="comboBoxTopAppBar" Width="120" Margin="10,0,0,0" MinWidth="200"
SelectedItem="{Binding SelectedStatus, Mode=TwoWay}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={StaticResource ResourceTranslationConverter}}"
</DataTemplate>
<ComboBox.ItemTemplate>
<x:String>Item 1</x:String>
<x:String>Item 2</x:String>
<x:String>Item 3</x:String>
</ComboBox>
我想发布一个问题(1)的替代方案,用C#code将组合框中的项目本地化。这更直截了当:
xaml
<ComboBox x:Name="comboBoxTopAppBar"/>
谢谢你的回答。它真的很有用。但是ResourceLoader没有GetForIndependentUse()方法,我使用了GetForCurrentView()。不管怎样,它很好用。
<!-- This is defined somewhere accessible, or just in the Page Resources -->
<!-- 'converters' is whichever namespace definition your converter is in -->
<converters:ResourceTranslationConverter x:Key="ResourceTranslationConverter"/>
<ComboBox x:Name="comboBoxTopAppBar" Width="120" Margin="10,0,0,0" MinWidth="200"
SelectedItem="{Binding SelectedStatus, Mode=TwoWay}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={StaticResource ResourceTranslationConverter}}"
</DataTemplate>
<ComboBox.ItemTemplate>
<x:String>Item 1</x:String>
<x:String>Item 2</x:String>
<x:String>Item 3</x:String>
</ComboBox>
<ComboBox x:Name="comboBoxTopAppBar"/>
var loader = ResourceLoader.GetForCurrentView();
comboBoxTopAppBar.Items.Add(loader.GetString("kItem1"));
comboBoxTopAppBar.Items.Add(loader.GetString("kItem2"));
comboBoxTopAppBar.Items.Add(loader.GetString("kItem3"));
comboBoxTopAppBar.SelectedIndex = 0;