【问题标题】:Navigation in Xamarin Forms with Custom Movement between pages使用页面之间的自定义移动在 Xamarin 表单中导航
【发布时间】:2019-09-29 07:54:05
【问题描述】:

您好 Xamarin 开发人员,

所以我有一个要求,用户从 Page1 -> Page2 -> Page3 -> Page4 并且在 Page4 之后,他必须返回到 Page2,如果他按下返回按钮,他应该将其导航回 Page1。

如何在 Xamarin Forms 中实现这一点?

我试过这样,

 Navigation.RemovePage(Navigation.NavigationStack[Navigation.NavigationStack.Count - 1]);
               _ = Navigation.PopAsync(true);

但是它给出了奇怪的动画并且不能正常工作。

请帮帮我。

【问题讨论】:

    标签: xamarin xamarin.forms xamarin.android xamarin.ios


    【解决方案1】:

    首先:如果你正在使用

    Navigation.RemovePage(Navigation.NavigationStack[Navigation.NavigationStack.Count - 1]);
    

    来自page4Navigation.NavigationStack.Count - 1 = 3,即NavigationStackpage4的索引(page1的索引为0!)。

    我认为你应该使用

    Navigation.RemovePage(Navigation.NavigationStack[Navigation.NavigationStack.Count - 2]);
    

    相反...


    更多内容:

    使用 Shell/Xamarin.Forms 4.0

    Shell(可从 Xamarin.Forms 4.0 获得)旨在简化应用程序中 Navigation 的管理。 XF 4.0 将在几周内发布,所以也许值得等待它(X.F 4.0 is at the moment in pre-release,如果您愿意,现在可以使用 Shell 功能这是一个机会!)。

    传统方式

    a) 为了返回第一页(根页),您可以使用

    Navigation.PopToRootAsync();
    

    b) 为了从 page4 弹出到 page2,我还使用了 Navigation.RemovePage 的技术,但不同的是我在我执行此操作后立即执行此删除PushAsyncpage4,所以一旦您从 page4 调用 PopAsync(),它会直接弹出到 page2,而无需在那时删除任何内容(见下面的代码)。请以这种方式尝试,如果成功请告诉我:)

    private async Task PushMyPage4RemovingCurrentPageFromNavigationStack()
    {
        var currentPage = ((NavigationPage)mainPage).CurrentPage as MyPage3;
    
        await currentPage.Navigation.PushAsync(new MyPage4());
    
        await Task.Run(() =>
        {
            // At this point MyPage4 is already pushed, so it is now the CurrentPage.
            var newCurrentPage = ((NavigationPage)mainPage).CurrentPage;
    
            IReadOnlyList<Page> navStack = newCurrentPage.Navigation.NavigationStack;
    
            // If not moved to main thread in iOS we get:
            //
            // UIKit.UIKitThreadAccessException: UIKit Consistency error: you are 
            // calling a UIKit method that can only be invoked from the UI thread.
            //
            if (Device.RuntimePlatform == Device.iOS)
    
                Device.BeginInvokeOnMainThread(() => newCurrentPage.Navigation.RemovePage(navStack[navStack.Count - 2]));
    
            else
    
                newCurrentPage.Navigation.RemovePage(navStack[navStack.Count - 2]);
        });
    
    }
    

    【讨论】:

      猜你喜欢
      • 2018-04-17
      • 2021-12-17
      • 2015-04-26
      • 1970-01-01
      • 1970-01-01
      • 2017-07-23
      • 1970-01-01
      • 2017-04-21
      • 2021-11-04
      相关资源
      最近更新 更多