首页 文章

iOS上不支持全局支持PushAsync,请使用NavigationPage

提问于
浏览
4

当我处理 ListView 项目的Tap事件时,一切正常,但是当我在 TabbedPage 中使用它时,它会显示异常请提前解决此问题 .

例外:iOS上不支持全局支持PushAsync,请使用NavigationPage .

这是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" xmlns:local="clr-namespace:NavTest" x:Class="NavTest.NavTestPage" Title="Home">
        <ContentPage.Content>
            <StackLayout Orientation="Vertical" VerticalOptions="Center">
                <Label Text="Welcome to Xamarin Forms!" VerticalOptions="Center" HorizontalOptions="Center" />
                <!--<Button Text="Go to " BackgroundColor="Lime" VerticalOptions="Center" Clicked="Handle_Clicked" >
            </Button>-->
                <ListView x:Name="myListView" ItemSelected="Handle_ItemSelected">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <Label Text="{Binding}" VerticalOptions="Center" HorizontalOptions="Center" />
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </StackLayout>
        </ContentPage.Content>
    </ContentPage>

这是OnClick处理程序:

async void Handle_Clicked(object sender, System.EventArgs e)
    {
        await  Navigation.PushAsync(new Page1());
    }

4 回答

  • 1

    仅在iOS中运行时会出现此错误 .

    In App.cs Rootpage(Start page) give like below

    public App()
      {
          MainPage=new NavigationPage(new LoginPage());
      }
    

    现在,我们可以在全球范围内使用PushAsyn()

  • 0

    你可能在_1295859中有一些与此类似的代码:

    public App()
    {
        InitializeComponent();
    
        var tabs = new TabbedPage();    
        tabs.Children.Add(new NavTestPage() { Title = "Tab title" });
        MainPage = tabs;
    }
    

    你应该改为:

    public App()
    {
        InitializeComponent();
    
        var tabs = new TabbedPage();
        var page = new NavTestPage() { Title = "Page title" };    
        tabs.Children.Add(new NavigationPage(page) { Title = "Tab title" });
    
        MainPage = tabs;
    }
    

    通过包装 NavigationPage ,您可以选择将页面推送到导航堆栈 . TabbedPage 本身就是每个标签只有1页的标签 . 这应该给你一个这样的结构:

    TabbedPage
        NavigationPage
            ContentPage
        NavigationPage
            ContentPage
        NavigationPage
            ContentPage
    

    Update: 我猜你正在做的只是在你的XAML中将 ContentPage 改为 TabbedPage . 这不是 TabbedPage 的工作方式 . 您应该首先阅读 TabbedPage 实际上是如何工作的 .

    https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/navigation/tabbed-page/

    在您的情况下,您应该创建一个新页面,其中包含XAML,如下所示:

    <TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
                xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                xmlns:local="clr-namespace:NavTest"
                x:Class="NavTest.MyTabbedPage">
        <NavigationPage Title="NavTest">
            <x:Arguments>
                <local:NavTestPage />
            </x:Arguments>
        </NavigationPage>
    </TabbedPage>
    

    App.xaml.cs 中将其设置为 MainPage

    public App()
    {
        InitializeComponent();
        MainPage = new MyTabbedPage();
    }
    
  • 0

    您应该将TabbedPage子页面放入NavigationPage . 以下是Xamarin文档中的代码片段 .

    In Xaml:

    <TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            xmlns:local="clr-namespace:TabbedPageWithNavigationPage;assembly=TabbedPageWithNavigationPage"
            x:Class="TabbedPageWithNavigationPage.MainPage">
        <local:TodayPage />
        <NavigationPage Title="Schedule" Icon="schedule.png">
            <x:Arguments>
                <local:SchedulePage />
            </x:Arguments>
        </NavigationPage>
    </TabbedPage>
    

    Or In Code:

    public class MainPageCS : TabbedPage
    {
        public MainPageCS ()
        {
            var navigationPage = new NavigationPage (new SchedulePageCS ());
            navigationPage.Icon = "schedule.png";
            navigationPage.Title = "Schedule";
    
            Children.Add (new TodayPageCS ());
            Children.Add (navigationPage);
        }
    }
    
  • 8

    在这个错误中,它会找到根页面,如果我们使用主详细信息页面,那么我们只需要为详细信息页面设置导航 . 因此,以下将是最佳解决方案 .

    ((RootPage)Application.Current.MainPage).Detail.Navigation.PushAsync(new SearchPage());
    

相关问题