Buj*_*uju 6 localization mvvm expression-blend windows-phone
如何在WP7中使用Blend在不同的视觉状态下设置不同的本地化字符串而不使用任何代码?
我可以在不同的视觉状态下设置不同的非本地化字符串(虽然它会闪烁).这有效,但本地化字符串怎么样?
如果我在Blend中使用数据绑定更改字符串,Blend只会覆盖Base状态下的数据绑定,而不是我正在录制的实际状态.
编辑:
这是我本地化我的字符串的方式:
我有一个名为的资源文件AppPresources.resx.然后我会在代码中执行此操作:
// setting localized button title
mainButton.Content = AppResources.MainButtonText;
Run Code Online (Sandbox Code Playgroud)
然后我有一个GlobalViewModelLocator来自MVVM Light Toolkit的具有以下属性的数据绑定.
private static AppResources _localizedStrings;
public AppResources LocalizedStrings
{
get
{
if (_localizedStrings == null)
{
_localizedStrings = new AppResources();
}
return _localizedStrings;
}
}
Run Code Online (Sandbox Code Playgroud)
并在xaml文件中:
<Button x:Name="mainButton" Content="{Binding LocalizedStrings.MainButtonText, Mode=OneWay, Source={StaticResource Locator}}" ... />
Run Code Online (Sandbox Code Playgroud)
您需要做的事情与您已经在做的事情非常接近。首先,定义一个名为Resources.cs的类,其内容如下
public class Resources
{
private static AppResources resources = new AppResources();
public AppResources LocalizedStrings
{
get
{
return resources;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这使我们能够在 XAML 中创建资源文件的实例。为此,请打开App.xaml并添加以下内容
<Application.Resources>
<local:Resources x:Key="Resources" />
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)
现在,当您需要在 XAML 中进行绑定时,您可以这样做:
<Button Content="{Binding LocalizedStrings.MainButtonText,
Source={StaticResource Resources}}" />
Run Code Online (Sandbox Code Playgroud)
您会注意到,它在 Blend 中还不起作用。要使其在 Expression Blend 中工作,请在属性文件夹中添加以下文件: DesignTimeResources.xaml ,然后添加以下内容
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:YourNameSpace">
<local:Resources x:Key="Resources" />
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
现在,您在 Visual Studio 中按 F6 重新编译,瞧,您的本地化字符串可在 Expression Blend 中使用!
我的一个项目中的一个真实示例: