c# – 即时刷新UI语言
发布时间:2020-12-15 22:36:05 所属栏目:百科 来源:网络整理
导读:我正在关注这个 guide来本地化我的应用程序.我不想获得系统语言,因此我不使用ILocalize接口和依赖服务.我有这3个用于英语,西班牙语和法语的resx文件: AppResources.es.resx AppResources.fr.resx AppResources.resx(英文) 这是我使用指南中的TranslateExten
我正在关注这个
guide来本地化我的应用程序.我不想获得系统语言,因此我不使用ILocalize接口和依赖服务.我有这3个用于英语,西班牙语和法语的resx文件:
> AppResources.es.resx 这是我使用指南中的TranslateExtension类的TestPage.xaml代码: <?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 } 如何刷新de page以显示法语文本而无需再次执行新的TestPage()? 解决方法
这不是Resx的特定解决方案,但它可以让您根据用户需求加载任何语言.无需手动设置字符串,因为绑定将完成工作.看看
I18NPortable和样品.它很容易启动和运行.
结合样本: <Button Text="{Binding Strings[key]}" /> 要动态更改语言(将更新所有绑定): I18N.Current.Locale = "fr"; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |