Xamarin表单中的导航,页面之间可以自定义移动 [英] Navigation in Xamarin Forms with Custom Movement between pages

查看:156
本文介绍了Xamarin表单中的导航,页面之间可以自定义移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Xamarin开发人员,

Hi Xamarin Developers,

所以我有一个要求,让用户从第1页->第2页->第3页->第4页转到第4页,然后他必须返回到第2页,如果他按返回按钮,则应将其导航回第1页.

So I have one requirement where user go From Page1 -> Page2 -> Page3 -> Page4 and after Page4, He has to return back to Page2 and If he press back button he should navigate it back to Page1.

如何在Xamarin表单中实现这一目标?

How to achieve this in Xamarin Forms.?

我尝试过这样,

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

但是它给出了奇怪的动画,并且无法正常工作.

But it's giving weird animation and not working properly.

请帮助我.

推荐答案

首先:如果您正在使用

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

来自 page4

Navigation.NavigationStack.Count - 1 = 3,是NavigationStack page4 的索引( page1 的索引为0!).

from page4, Navigation.NavigationStack.Count - 1 = 3, which is the index of page4 in the NavigationStack (page1 has index 0!).

我认为您应该使用

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

相反...

更多评论:

Shell (可从 Xamarin.Forms 4.0 获得)旨在简化应用程序中Navigation的管理. XF 4.0 会在几周内发布,因此也许值得等待(

Shell, available from Xamarin.Forms 4.0, is meant to easy the management of Navigation in an Application. XF 4.0 ships in a couple of weeks, so maybe it would be worth to wait for it (X.F 4.0 is at the moment in pre-release, and you can use Shell features now if you want to give it a chance!).

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

a) In order to go back to the first page (root page) you can use

Navigation.PopToRootAsync();

b)为了从 page4 弹出到 page2 ,我还使用了Navigation.RemovePage的技术,但区别在于我在执行完后立即执行了此删除操作PushAsync page4 ,因此,一旦您从 page4 呼叫PopAsync(),它便会直接弹出到 page2 ,而无需在此时删除任何内容(请参见下面的代码).请以这种方式尝试,如果您成功,请告诉我:)

b) In order to pop from page4 to page2, i also use the technique with Navigation.RemovePage, but with the difference that i perform this removal immediately after i PushAsync to page4, so once you call PopAsync() from page4, it pops directly to page2 without having to remove anything at that time (see code below). Please try it this way and let me know if you succeed :)

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

}

这篇关于Xamarin表单中的导航,页面之间可以自定义移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆