【发布时间】:2022-03-07 20:37:07
【问题描述】:
我正在开发一个跨平台应用程序(android、IOS),它必须能够通过按下按钮在每个页面上切换语言。我正在使用 AppShell 进行导航,并且在 AppShell 的工具栏中有语言切换按钮。我制作了资源文件:AppResources.resx 和 AppResources.fr.resx。我在切换时重新加载了当前页面和 AppShell,这似乎具有返回到我在导航栏上设置的第一页的副作用。 AppShell 的重新加载似乎是必要的,因为当我不这样做时,页面似乎在顶部并且没有更多的导航以及我在 AppShell 中设置的颜色资源被删除。我使用下面的代码来切换我的应用程序的语言:
private void Language_switch(object sender, EventArgs e)
{
var lang_switch = Lang.Text;
if (lang_switch == "FR")
{
CultureInfo language = new CultureInfo("fr");
Thread.CurrentThread.CurrentUICulture = language;
AppResources.Culture = language;
Application.Current.Properties[key: "LanguageCode"] = "fr_FR";
}
else
{
CultureInfo language = new CultureInfo("");
Thread.CurrentThread.CurrentUICulture = language;
AppResources.Culture = language;
Application.Current.Properties[key: "LanguageCode"] = "nl_NL";
}
Application.Current.MainPage = new Surveys();
Application.Current.MainPage = new AppShell();
}
这段代码过去可以工作,但现在不能工作了。我确实记得不久前更新了 Xamarin Forms,所以这可能与代码不再工作有关。在 XAML 中,我从以下资源文件中读取:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyOpinion.Views.Surveys"
xmlns:vm="clr-namespace:MyOpinion.ViewModels"
Title="{Binding Title}"
xmlns:resource="clr-namespace:MyOpinion.Resx">
<Label Text="{x:Static resource:AppResources.Openstaande}" TextColor="{StaticResource Text}" FontSize="24" Margin="0,0,0,10"/>
</ContentPage>
【问题讨论】:
-
你为什么要做这个 Application.Current.MainPage = new Surveys(); Application.Current.MainPage = new AppShell();
-
这是重新加载调查页面和 AppShell。还有另一种方法吗?因为只是做语言切换似乎并没有改变任何标签,而且据我所知只在后端切换
-
亲爱的您只需要加载一页 appshell 或调查。如果您只想在更改语言后导航到调查,请删除 Application.Current.MainPage = new AppShell();
-
是的,这可以工作并切换语言。但是,如果我不重新加载 AppShell,它似乎会回到标准颜色,只有我自己赋予颜色的元素是它们应该是的颜色。页面底部的导航消失了,顶部的工具栏也变成了不同的颜色。
-
好的,我知道您想更改语言,然后导航到调查页面。这样做: Application.Current.MainPage = new AppShell();等待 Application.Current.MainPage.Navigation.PushAsync(new Surveys());
标签: xamarin.forms xamarin.android xamarin.ios