【发布时间】:2017-05-25 10:44:07
【问题描述】:
我正在关注这个guide 来本地化我的应用程序。我不想获得系统语言,所以我不使用ILocalize 接口和依赖服务。我有这 3 个用于英语、西班牙语和法语的 resx 文件:
- AppResources.es.resx
- AppResources.fr.resx
- AppResources.resx(英文)
这是我的TestPage.xaml 代码,使用指南中的TranslateExtension 类:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:t="clr-namespace:LanguageTest.Resources;assembly=LanguageTest.Resources"
x:Class="LanguageTest.TestPage">
<Label Text="{t:Translate TestText}" VerticalOptions="Center" HorizontalOptions="Center" />
</ContentPage>
使用此代码我初始化语言设置:
public App ()
{
// I can change among "en", "es" and "fr"
AppResources.Culture = new CultureInfo("es");
this.MainPage = new NavigationPage(new TestPage());
}
在这种情况下,文本以西班牙语显示。但我想在页面显示后更改语言(例如,使用LanguageSettingsPage 用户可以在应用运行时更改语言)。
public App ()
{
AppResources.Culture = new CultureInfo("es");
this.MainPage = new NavigationPage(new TestPage());
AppResources.Culture = new CultureInfo("fr");
// At this point the texts are diplayed in Spanish, not in French
}
如何在不重复new TestPage()的情况下刷新页面以显示法语文本?
【问题讨论】:
-
您找到解决方案了吗?如果可以,可以分享一下吗?
-
@batmaci 不,我没有找到解决方案。选择一种语言后,我会更改 App.cs 代码中的所有文本。没有系统允许您使用绑定或类似的方式更改文本。
-
重新显示主页你可以做
MainPage = new NavigationPage(...);
标签: c# xamarin.forms