如何在WP7中使用Blend在不同的视觉状态下设置不同的本地化字符串,而不使用任何代码?
我可以在不同的视觉状态下设置不同的非本地化字符串(虽然它会闪烁)。这可行,但本地化字符串怎么样?
如果我在Blend中使用数据绑定更改字符串,Blend只会覆盖Base状态下的数据绑定,而不是我正在录制的实际状态。
修改
这是我对字符串进行本地化的方法:
我有一个名为AppPresources.resx
的资源文件。然后我会在代码中执行此操作:
// setting localized button title
mainButton.Content = AppResources.MainButtonText;
然后我从MVVM Light Toolkit获得GlobalViewModelLocator
,其中包含以下数据绑定属性。
private static AppResources _localizedStrings;
public AppResources LocalizedStrings
{
get
{
if (_localizedStrings == null)
{
_localizedStrings = new AppResources();
}
return _localizedStrings;
}
}
在xaml文件中:
<Button x:Name="mainButton" Content="{Binding LocalizedStrings.MainButtonText, Mode=OneWay, Source={StaticResource Locator}}" ... />
答案 0 :(得分:4)
你需要做的,与你已经做的非常接近。首先,使用以下内容
定义名为 Resources.cs 的类public class Resources
{
private static AppResources resources = new AppResources();
public AppResources LocalizedStrings
{
get
{
return resources;
}
}
}
这允许我们在XAML中创建资源文件的实例。为此,请打开 App.xaml 并添加以下内容
<Application.Resources>
<local:Resources x:Key="Resources" />
</Application.Resources>
现在,当您需要在XAML中进行绑定时,可以这样做:
<Button Content="{Binding LocalizedStrings.MainButtonText,
Source={StaticResource Resources}}" />
您会注意到它在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>
现在,您在Visual Studio中按F6重新编译,瞧,您的本地化字符串在Expression Blend中可用!
我的一个项目的真实示例: