如何在运行时更改Windows Phone APP语言?
我按照以下示例构建了一个多语言应用程序:如何目标:为 Windows Phone 构建本地化应用程序。
我成功地将资源数据绑定到这样的文本:
<TextBlock x:Name="ApplicationTitle" Text="{Binding Path=MultiLangResources.Mainpage_Welcome, Source={StaticResource MultiLang}}"/>
我尝试更改 Thread.CurrentThread.CurrentUICulture ,我可以使用代码输出正确的密钥:
ApplicationTitle.Text = LangResource.Mainpage_Welcome;
但是,绑定的文本永远不会更新。
如何让绑定的文本像普通绑定一样更新?
有没有人可以帮助我解决这个问题? 我也尝试过这个但没有运气。
public class MultiLang : INotifyPropertyChanged
{
public MultiLang()
{
}
private static MLTest.LangResource multiLangResources = new GigapodV2.LangResource();
public MLTest.LangResource MultiLangResources
{
get { return multiLangResources; }
set
{
if (value != multiLangResources)
{
multiLangResources = value;
NotifyPropertyChanged("MultiLangResources");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string property)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
I followed this example to build a multilingual app: How to: Build a Localized Application for Windows Phone.
I successfully bound the resource data to text like this:
<TextBlock x:Name="ApplicationTitle" Text="{Binding Path=MultiLangResources.Mainpage_Welcome, Source={StaticResource MultiLang}}"/>
And I tried to change the Thread.CurrentThread.CurrentUICulture
and I can output correct key with code:
ApplicationTitle.Text = LangResource.Mainpage_Welcome;
However, the bound text never update.
How to make the bound text update like ordinary bind?
Is there anyone could help me with this problem?
I also tried this with no luck.
public class MultiLang : INotifyPropertyChanged
{
public MultiLang()
{
}
private static MLTest.LangResource multiLangResources = new GigapodV2.LangResource();
public MLTest.LangResource MultiLangResources
{
get { return multiLangResources; }
set
{
if (value != multiLangResources)
{
multiLangResources = value;
NotifyPropertyChanged("MultiLangResources");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string property)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这很容易完成:
主要问题是所有已加载的页面上的文本(例如主页,如果您将语言选择放在那里),将需要“手动刷新”。
但这可以通过编程轻松完成:
It can be easily done:
The main problem is that the text on all the pages that are ALREADY loaded (for example the main page, if you put the language choose there), will need to be "manually refreshed".
But this can be easily done programatically:
LangResource
类本身是不可观察的,因此它的任何绑定属性都不会被观察到。而且,手机不会仅仅因为您即时更改它而通知与 CurrentCulture 相关的所有属性。否则,以这种方式更改语言将需要重新启动您的应用程序。但我建议您要小心这一点,因为操作系统本身允许语言交换,这意味着您需要有一个非常充分的理由让您的应用程序本身允许它。
解决方案是创建一个包装类,当您更改 CurrentCulture 时,它会通知所有属性已更新。使用 Simon 的 NotifyPropertyWeaver 的 [DependsOn] 属性可能会使这变得非常简单。
The
LangResource
class isn't observable by itself, and thus any bound properties from it won't be observed. And also, the phone won't notify all properties that's related to the CurrentCulture just because you change it on-the-fly.Language change in that fashion would otherwise require a reboot of your app. But I would advice you to be careful with this, as the OS itself allows language swapping, meaning you need to have a really good reason for your app to allow it in itself.
The solution is to create a wrapper class, that notifies all properties that they been updated, when you change the CurrentCulture. Using Simon's NotifyPropertyWeaver's [DependsOn] attribute could probably make this very easy.