我正在努力将本地化添加到使用.NET 3.5的应用程序中。应用程序使用MVVM模式和一个命令来更改区域性。除了DatePicker控件在我单击它之前不会改变语言之外,一切都运行良好。此时,选定的日期文本将正确更改。控件中的下拉日历也不会更改语言,直到我将这个月向前或向后移动一次。
在运行命令以更改区域性时,如何强制控件使用适当的语言刷新?
我尝试过几件没有成功的事情,包括:
App.xaml.cs
protected override void OnStartup(StartupEventArgs e)
{
ApplicationCulture.Instance.CultureChanged += Instance_CultureChanged;
base.OnStartup(e);
}
private void Instance_CultureChanged(object sender, CultureChangedEventArgs e)
{
System.Threading.Thread.CurrentThread.CurrentUICulture = e.Culture;
System.Threading.Thread.CurrentThread.CurrentCulture = e.Culture;
}视图
<UserControl x:Class="ManageAppointmentsView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:toolkit="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit">
<StackPanel>
<TextBlock Margin="5" FontSize="15" Text="{Binding LocalizedResources.Resource.Date}" />
<toolkit:DatePicker SelectedDate="{Binding SelectedDate}" SelectedDateFormat="Long" FontSize="15" VerticalContentAlignment="Center"
DisplayDateStart="{Binding StartDate}" CalendarStyle="{StaticResource CalendarStyle}" x:Name="DatePickerControl" />
</StackPanel>
</UserControl>ViewModel命令
ChangeLanguageCommand = new SimpleCommand
{
ExecuteDelegate = x =>
{
var newCulture = x == null
? "en-US"
: x.ToString();
ApplicationCulture.Instance.CurrentCulture =
new CultureInfo(newCulture);
}
};ApplicationCulture
public class ApplicationCulture : INotifyCultureChanged
{
private ApplicationCulture() { }
private static ApplicationCulture _instance;
public static ApplicationCulture Instance
{
get
{
if (_instance == null)
_instance = new ApplicationCulture();
return _instance;
}
}
private CultureInfo _currentCulture = CultureInfo.InvariantCulture;
public CultureInfo CurrentCulture
{
get { return _currentCulture; }
set
{
if (!CultureInfo.Equals(value, _currentCulture))
{
_currentCulture = value;
NotifyCultureChanged(value);
}
}
}
public event EventHandler<CultureChangedEventArgs> CultureChanged;
private void NotifyCultureChanged(CultureInfo culture)
{
if (CultureChanged != null)
CultureChanged(this, new CultureChangedEventArgs(culture));
}
}发布于 2013-02-09 10:59:37
在这种情况下,解决方案可能是更改用户交互模式。在分页的应用程序中,为了选择语言,我会切换到一个单独的页面,并在更改时切换回原始页面。因此,页面将重新初始化,包括所有本地化的但否则是静态的资源。在非分页应用程序中,您可以使用对话框来更改UI语言,同时关闭并重新打开主窗口。
在这两种情况下,诀窍都是在更改语言之前和之后保留ViewModel实例,以便在重新加载本地化资源时保留视图状态和输入的数据。
https://stackoverflow.com/questions/14779981
复制相似问题