简体   繁体   English

Xamarin.Forms Shell GoToAsync 在 Z1BDF605991920DB1ZEB1CBDF8C 中未按预期工作

[英]Xamarin.Forms Shell GoToAsync is not working as expected in iOS

I am using the Xamarin.Forms - Shell feature.我正在使用 Xamarin.Forms - Shell 功能。 I need to navigate from one tab (root) to another tab (second level).我需要从一个选项卡(根)导航到另一个选项卡(第二级)。 The example has three pages, for simplicity I named Page1, Page2, and Page3.该示例包含三个页面,为简单起见,我将其命名为 Page1、Page2 和 Page3。 Page1 and Page2 are main tabs for the App Shell, the Page 3 is a second level of Page1 (stack). Page1 和 Page2 是 App Shell 的主要选项卡,第 3 页是 Page1(堆栈)的第二级。

The next code is part of AppShell.xaml:下一个代码是 AppShell.xaml 的一部分:

  <FlyoutItem FlyoutDisplayOptions="AsMultipleItems">
     <ShellContent Route="page1" Title="Page1" ContentTemplate="{DataTemplate local:Page1}" />
     <ShellContent Route="page2" Title="Page2" ContentTemplate="{DataTemplate local:Page2}" />
</FlyoutItem>

The constructor in AppShell.cs is AppShell.cs 中的构造函数是

 public AppShell()
        {
            InitializeComponent();
            Routing.RegisterRoute(nameof(Page3), typeof(Page3));
        }

Page 1 and Page 3 are ver similar, just to represent two pages.第 1 页和第 3 页非常相似,只是代表两个页面。 The next is Page1.xaml接下来是Page1.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"
             x:Class="TestGoTo.Views.Page1"
              Title="Page1" BackgroundColor="LightGreen">
    <ContentPage.Content>
        <StackLayout>
            <Label Text="Welcome to Page1"
                VerticalOptions="CenterAndExpand" 
                HorizontalOptions="CenterAndExpand" />
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

Page2.xaml includes a button to call the GoToAsync function Page2.xaml 包括一个按钮来调用 GoToAsync function

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="TestGoTo.Views.Page2"
             Title="Page2" BackgroundColor="LightYellow">
    <ContentPage.Content>
        <StackLayout>
            <Label Text="Page2" HorizontalOptions="CenterAndExpand" />
            <Button Text="GoTo" Clicked="Button_Clicked"></Button>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

Page2.cs - Button Action is Page2.cs - 按钮操作是

private async void Button_Clicked(object sender, EventArgs e)
        {
             await Shell.Current.GoToAsync($"//page1/{nameof(Page3)}");
        }

The Problem:问题:

When you go the Page2 and click the button, the expected behavior is to show the page 3 under tab of page 1, but you remain in the tab/page2.当您 go Page2并单击按钮时,预期的行为是在第 1 页的选项卡下显示第 3 页,但您仍然在选项卡/第 2 页中。 If you go (manually) to page 1 you will see the stack with the page 3. So, the page 3 is in the right place (loaded), but you did not see it because you remain in the page 2.如果您 go (手动)到第 1 页,您将看到第 3 页的堆栈。因此,第 3 页位于正确的位置(已加载),但您没有看到它,因为您仍然在第 2 页中。

This problem is presented only in iOS, Android works as expected.此问题仅在 iOS 中出现,Android 按预期工作。

What's wrong here?这里有什么问题? Is there a Bug in Xamarin.Forms (Shell for iOS)? Xamarin.Forms(iOS 外壳)中是否有错误?

Your help is appreciated.感谢您的帮助。

Looks like you cannot navigate to another stack (even if you name it) without change the CurrentItem.看起来您无法在不更改 CurrentItem 的情况下导航到另一个堆栈(即使您命名它)。 For some reason Android accepted, but not iOS.由于某种原因,接受了 Android,但不接受 iOS。

I just found a workout.我刚找到一个锻炼。 I don't like too much, but now it is working.我不太喜欢,但现在它正在工作。 You need to change the CurrentItem (Shell.Current.CurrentItem) to a tab you want to navigate.您需要将 CurrentItem (Shell.Current.CurrentItem) 更改为要导航的选项卡。 In this case I want to navigate to the Tab1 (page1).在这种情况下,我想导航到 Tab1(page1)。 so, I need to change the CurrentItem as所以,我需要将 CurrentItem 更改为

  Shell.Current.CurrentItem = Shell.Current.CurrentItem.Items[0];

or I can use a better way (if I do not know the index).或者我可以使用更好的方法(如果我不知道索引)。

Shell.Current.CurrentItem = Shell.Current.CurrentItem.Items.FirstOrDefault(x => x.Title= "Page1");

Below is the new button action.下面是新的按钮操作。

  private async void Button_Clicked(object sender, EventArgs e)
        {
            
            Shell.Current.CurrentItem = Shell.Current.CurrentItem.Items.FirstOrDefault(x => x.Title=="Page1");
            //Shell.Current.CurrentItem = Shell.Current.CurrentItem.Items[0];
            await Shell.Current.GoToAsync($"//page1/{nameof(Page3)}");
        }

Comments are welcome.欢迎评论。 Thanks!谢谢!

I think it will work better if you declare the route, the way you want to use it.我认为如果你声明路线,你想使用它的方式,它会更好。 Here, you want page3 to be a sub-page of page1, but you don't declare its route that way.在这里,您希望 page3 成为 page1 的子页面,但您没有那样声明它的路由。

Try:尝试:

Routing.RegisterRoute("page1/page3", typeof(Page3));

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

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