简体   繁体   English

如何在顶部导航中没有返回按钮的情况下在 Xamarin Shell 中切换页面

[英]How to switch pages in the Xamarin Shell without a back button in the top navigation

i have picker in my left menu navigation shell that change the main page language, for example actually the app in english, when i change to french, it change and redirect to main page with french text, but the problem is when i press the back arrow in navigation page, it back to old language (English ).. here the shell navigation我的左侧菜单导航中有选择器 shell 可以更改主页语言,例如实际上是英文的应用程序,当我更改为法语时,它会更改并重定向到带有法语文本的主页,但问题是当我按下后退导航页面中的箭头,它返回旧语言(英文).. 这里是 shell 导航

<Shell ..>
    <Shell.FlyoutHeader>
<Picker  ItemsSource="{Binding MultiLang, Mode=TwoWay}" SelectedItem="{Binding SelectedLang}">
     </Picker>
</Shell.FlyoutHeader>


<ShellContent x:Name="home"
Route="main"
ContentTemplate="{DataTemplate home:Dashboard}" />
</Shell>

and this is the method that redirect to main page:这是重定向到主页的方法:

private async void ChangeLange(string lang)
{
...
            Routing.RegisterRoute(nameof(Dashboard), typeof(Dashboard));
            await Shell.Current.GoToAsync($"{nameof(Dashboard)}");// it redirect but with button back
            RemoveStackNavigation ()
         //   await Shell.Current.GoToAsync("//main"); like this , it dosent refresh the page with 

            Shell.Current.FlyoutIsPresented = false; 

}

here is the MVMM这是 MVMM

        public string_selectedLang;
        public string SelectedLang
        {
            get
            {
                return _selectedLang;
            }
            set
            {
                if (_selectedLang != value)
                {
                    _selectedLang = value;
                    OnPropertyChanged("SelectedLang");
                    ChangeBuilding(value);


                }
            }
        }

i tried to RemoveStackNavigation befire make redirection to Dashboard like this:我试图像这样将RemoveStackNavigation重定向到仪表板:

        public static void RemoveStackNavigation()
        {
            var existingPages = Shell.Current.Navigation.NavigationStack.ToList();
            foreach (var page in existingPages)
            {
                if (page != null)
                    Shell.Current.Navigation.RemovePage(page);
            }

        }

Please help me.请帮我。

The navigation stack of Shell has something special. Shell 的导航堆栈有一些特别之处。 Clear the navigation stack is not a good choice.清除导航堆栈不是一个好的选择。 We always use Routes to do the navigation for the Shell.我们总是使用 Routes 为 Shell 进行导航。 You need to know which would generate the stack.您需要知道哪个会生成堆栈。

  • A route, which defines the path to content that exists as part of the Shell visual hierarchy.一个路由,它定义了作为 Shell 视觉层次结构的一部分存在的内容的路径。
  • A page.页面。 Pages that don't exist in the Shell visual hierarchy can be pushed onto the navigation stack from anywhere within a Shell application. Shell 视觉层次结构中不存在的页面可以从 Shell 应用程序中的任何位置推送到导航堆栈。 For example, a details page won't be defined in the Shell visual hierarchy, but can be pushed onto the navigation stack as required.例如,详细信息页面不会在 Shell 视觉层次结构中定义,但可以根据需要推送到导航堆栈中。

For more details, please check the MS docs.有关更多详细信息,请查看 MS 文档。 https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/shell/navigation https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/shell/navigation

When you press the back button, override the back button event and then judge the route to do the navigation would be better.当您按下返回按钮时,覆盖返回按钮事件,然后判断路线进行导航会更好。 Use the specific route name to go back.使用具体的路由名称到 go 回来。

I didn't understand so much of what happens, but I assume that you want to change the language of your app when you swipe the picker.我不太了解会发生什么,但我假设您想在滑动选择器时更改应用程序的语言。 I think the problem isn't in the navigation, but the way of pages are created.我认为问题不在于导航,而在于创建页面的方式。

For example, if you run now your app and it goes to a Main page it will stored in a temp cache, so if you go to another page and go back, it will load faster than the first time that you entered in app.例如,如果您现在运行您的应用程序并进入Main page面,它将存储在临时缓存中,因此如果您将 go 转到另一个页面并返回 go,它将比您第一次进入应用程序时加载速度更快。

Something that you can test is (ONLY TEST):您可以测试的是(仅测试):

  • Put this picker result (eg: if is French than a variable is 1, if is English, 2, etc) and this variable as static , just to it don't refresh and you lost this value.把这个选择器结果(例如:如果是法语而不是一个变量是 1,如果是英语,2 等等)和这个变量作为static ,只是它不刷新,你失去了这个值。
  • In your main page, create a Label and the set the dynamic text:在您的主页中,创建一个Label并设置动态文本:
switch(pickerResult):
case "1":
 lblTest.Text = "french";
break;
case "2":
 lblTest.Text = "English";
break;
case "3":
 lblTest.Text = "Italian";
break;

AND, the page that you go when you click in back button that now is showing in English, create exactly the same label above, and see if works.并且,当您单击现在以英文显示的后退按钮时,您 go 的页面,创建与上面完全相同的 label,看看是否有效。

If it works, then some hints:如果它有效,那么一些提示:

  • Your page can be with static texts;您的页面可以包含 static 文本;
  • Your page isn't getting the language in runtime;您的页面在运行时没有获得语言;

SO, to solve this, you need to create some List<Words> and get the properly language when the picker was changed:所以,要解决这个问题,您需要创建一些List<Words>并在选择器更改时获取正确的语言:

private void SelectedIndexChanged(sender s, EventArgs e)
{
  switch(pickerResult):
  case "1":
     App.wordsList = GetListOfWords("french");
  break;
}

This GetListOfWords is a function to you call your xml or App Resource or whatever you're using to change the language (even if was a translator).这个GetListOfWords是一个 function 给你打电话给你的 xml 或应用程序资源或任何你用来更改语言的东西(即使是翻译)。

Now your wordsList (or whatever are your way to call it) will be updated.现在你的 wordsList (或任何你称之为的方式)将被更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM