【问题标题】:Xamarin Forms switch language at runtime on each pageXamarin Forms 在运行时在每个页面上切换语言
【发布时间】: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


【解决方案1】:

您可以使用 Xamarin 社区工具包提供的 TranslateExtension。您无需重新加载页面。

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
             x:Class="MyLittleApp.MainPage">

    <StackLayout>

        <Label Text="{xct:Translate AppResources.ATranslatedMessage}" />

        <Label Text="{xct:Translate AppResources.AnotherTranslatedMessage, StringFormat='#{0}'}" />

    </StackLayout>
</ContentPage>

你应该先初始化它:

LocalizationResourceManager.Current.PropertyChanged += (_, _) => AppResources.Culture = LocalizationResourceManager.Current.CurrentCulture;
LocalizationResourceManager.Current.Init(AppResources.ResourceManager);

语言更改后您调用:

LocalizationResourceManager.Current.CurrentCulture = newCulture;

这是文档 https://docs.microsoft.com/en-us/xamarin/community-toolkit/extensions/translateextension

https://docs.microsoft.com/en-us/xamarin/community-toolkit/helpers/localizationresourcemanager

【讨论】:

    【解决方案2】:

    正如@Michael 所说,我认为没有办法在不弹出到 Root 的情况下更改语言。虽然我不推荐他先复制导航堆栈然后推送页面的想法。但我会为你实现这个想法。 请注意只读属性中的导航堆栈。

     
    var navStack = Application.Current.MainPage.Navigation.NavigationStack;
    Application.Current.MainPage.Navigation.RemovePage(navStack.First());
    
    Application.Current.MainPage = new AppShell ();
    foreach (var item in navStack)
    {
       await Application.Current.MainPage.Navigation.PushAsync((Page)Activator.CreateInstance(item.GetType()));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-31
      • 1970-01-01
      • 2019-02-01
      • 1970-01-01
      • 2020-08-27
      • 1970-01-01
      • 2013-05-10
      相关资源
      最近更新 更多