c# - Xamarin.Forms Shell GoToAsync 在 iOS 中无法按预期工作

标签 c# ios shell xamarin.forms

我正在使用 Xamarin.Forms - Shell 功能。 我需要从一个选项卡(根)导航到另一个选项卡(第二级)。 该示例有三个页面,为简单起见,我将其命名为 Page1、Page2 和 Page3。 Page1 和 Page2 是 App Shell 的主要选项卡,Page 3 是 Page1(堆栈)的第二层。

下一个代码是 AppShell.xaml 的一部分:

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

AppShell.cs中的构造函数是

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

第 1 页和第 3 页非常相似,只是代表两个页面。接下来是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 包含一个调用 GoToAsync 函数的按钮

<?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 - 按钮操作是

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

问题:

当您转到Page2并单击按钮时,预期的行为是在第 1 页的选项卡下显示第 3 页,但您仍保留在选项卡/第 2 页中。如果您(手动)转到页面 1,您将看到包含页面 3 的堆栈。因此,页面 3 位于正确的位置(已加载),但您没有看到它,因为您仍保留在页面 2 中。

此问题仅在 iOS 中出现,Android 正常工作。

这里出了什么问题? Xamarin.Forms(iOS Shell)中是否存在错误?

感谢您的帮助。

最佳答案

看起来您无法在不更改 CurrentItem 的情况下导航到另一个堆栈(即使您命名了它)。由于某种原因,Android 被接受,但 iOS 不被接受。

我刚刚找到了一个解决方法。我不太喜欢,但现在它正在发挥作用。 您需要将 CurrentItem (Shell.Current.CurrentItem) 更改为您要导航的选项卡。在本例中,我想导航到 Tab1 (page1)。所以,我需要将 CurrentItem 更改为

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

或者我可以使用更好的方法(如果我不知道索引)。

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

下面是新的按钮操作。

  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)}");
        }

欢迎评论。谢谢!

关于c# - Xamarin.Forms Shell GoToAsync 在 iOS 中无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70719731/

相关文章:

c# - 获取字典中的任何值 C#

shell - 在 unix 中的两个固定格式文件中查找字段值 - 不工作

bash - 找到(1): "Parameter format not correct" under CygWin

c# - 从 C# 在远程服务器上执行包含 Sharepoint 命令的 Powershell

c# - 如何让 Helm 机转到某个角度,例如60度

c# - 在 asp.net C# 中的客户端打印机上打印 pdf 文件?

objective-c - UIImageView 标签和点击手势问题

iphone - 核心数据 : Replacing Data Model with New Version?

ios - 从我的 iPhone 或 Blackberry 访问本地托管的 Web 服务

java - 命令在终端中有效,但不适用于 Runtime.exec