简体   繁体   English

如何从堆栈导航 Xamarin Forms 中删除当前页面

[英]How to remove a current page from stack navigation Xamarin Forms

I'm trying so hard to do something but i wasnt able to solve this problem.我很努力地做某事,但我无法解决这个问题。 I have three pages, One is the MainPage, LoginUpPage and SignUpPage, inside of LoginUpPage has a button who navigates to SignUpPage, what i want to do is when I finish my logic navigates to another page CreatedPage, whitch contains a Label with a Message - Success and then after 2 seconds GoBack to the LoginPage, the problem is if I press the backbutton from device it will return to the last page that has the label with a message and I don't want that.我有三个页面,一个是 MainPage、LoginUpPage 和 SignUpPage,LoginUpPage 里面有一个导航到 SignUpPage 的按钮,我想要做的是当我完成我的逻辑导航到另一个页面 CreatedPage,其中包含一个带有消息的标签 -成功,然后在 2 秒后返回到登录页面,问题是如果我从设备上按下后退按钮,它将返回到带有带有消息的标签的最后一页,而我不希望那样。 I have a toobar with a BackButton to return to each page that i navigates.我有一个带有 BackButton 的工具栏,可以返回到我导航的每个页面。 So far I have this:到目前为止,我有这个:

LoginPage to SignUpPage :登录页面到注册页面:

Navigation.PushAsync(new SignupPage());

SignUpPage to CreatedPage : SignUpPage 到 CreatedPage :

await Navigation.PushModalAsync(new Created());

And inside of CreatedPage in my Contructor, this Method :在我的构造函数中的 CreatedPage 内部,此方法:

    public async void Redirect()
    {
        await Task.Delay(TimeSpan.FromSeconds(2));
        await Navigation.PushAsync(new LoginPage());
    }

I know by this question there's basically three ways to navigate to another page :我通过这个问题知道基本上有三种方法可以导航到另一个页面:

Navigation.PushAsync(new OtherPage()); // to show OtherPage and be able to go back

Navigation.PushAsyncModal(new AnotherPage());// to show AnotherPage and not have a Back button

Navigation.PopAsync();// to go back one step on the navigation stack

At the same question has a example how to remove from a page from stack but it doesn't work.在同一个问题中有一个示例如何从堆栈中删除页面,但它不起作用。

item.Tapped += async (sender, e) => {
await Navigation.PushAsync (new SecondPage ());
Navigation.RemovePage(this);

}; };

The number of time pages are clicked to and fro, pages gets added to navigation stack.来回点击页面的次数,页面被添加到导航堆栈中。 Possibly same page can be in navigation stack many times based on number of clicks.根据点击次数,同一页面可能会多次出现在导航堆栈中。

If there are two or pages on navigation stack.如果导航堆栈上有两个或多个页面。

for (int PageIndex = Navigation.NavigationStack.Count; PageIndex >= 0; PageIndex--)
{  
    Navigation.RemovePage(Navigation.NavigationStack[PageIndex]);
}

This will remove all the pages in the navigation stack excluding the Main Page (or the page at top of Navigation stack)这将删除导航堆栈中的所有页面,不包括主页(或导航堆栈顶部的页面)

OR或者

If user is left with two pages in Navigation Stack and want to make 2nd page in navigation stack as main page (Navigationstack[1]) along with removal of first page(Navigationstack[0]) , It works as below如果用户在导航堆栈中留下两个页面,并希望将导航堆栈中的第二个页面作为主页面 (Navigationstack[1]) 以及移除第一页 (Navigationstack[0]) ,其工作原理如下

if (Navigation.NavigationStack.Count == 2)
{
    App.Current.MainPage.Navigation.RemovePage(Navigation.NavigationStack.First());
}

In that case you need to set a Root page :在这种情况下,您需要设置一个根页面:

//Master Detail Page

  public class RootPage : MasterDetailPage
    {
        MenuPage menuPage;

        public RootPage()
        {
            menuPage = new MenuPage(this);
            Master = menuPage;
            Detail = new NavigationPage(new HomePage());
        }
    }

//Set the Root Page
public class App : Application
{
    public App()
    {
       InitializeComponent ();

       if(NewUser || NotLoggedIn)
          {
             MainPage = new LoginPage();
          }
       else
          {
             MainPage = new RootPage();
          }
    }
}

public class LoginPage : ContentPage
    {            
        private void SignupButtonOnClicked(object sender, EventArgs eventArgs)
        {
            Navigation.PushAsync(new SignupPage());
        }
    }


public class SignupPage : ContentPage
    {            
        private void CreatedButtonOnClicked(object sender, EventArgs eventArgs)
        {
            Navigation.PushModalAsync(new CreatedPage());
        }
    }

// Set the Login Page

public class CreatedPage : ContentPage
    {

        private void CreatedButtonOnClicked(object sender, EventArgs eventArgs)
        {
            Navigation.PushModalAsync(new LoginPage());

            //Special Handel for Android Back button
            if (Device.OS == TargetPlatform.Android)
            Application.Current.MainPage = new LoginPage();
        }

    }

In this way back button will not return to previous page as it will reset the Navigation stack to root page ie your LoginPage.这样后退按钮将不会返回上一页,因为它会将导航堆栈重置为根页面,即您的登录页面。

I didn't try this before but you can try this solution:我之前没有尝试过,但您可以尝试以下解决方案:

retrieve your navigation stack as a list:检索您的导航堆栈作为列表:

var existingPages = Navigation.NavigationStack.ToList();

then remove the page you want然后删除你想要的页面

then set your navigation stack to the modified list然后将您的导航堆栈设置为修改后的列表

App.Navigation.RemovePage(App.Navigation.NavigationStack.ElementAt(App.Navigation.NavigationStack.Count - 2)); App.Navigation.RemovePage(App.Navigation.NavigationStack.ElementAt(App.Navigation.NavigationStack.Count - 2));

will remove page after navigated to next page导航到下一页后将删除页面

where App.Navigation is App.Navigation 在哪里

public static INavigation Navigation { get;公共静态 INavigation 导航 { 获取; set;放; } }

in app class在应用类

The easiest way I found to clear a stack and open up a new page on a clear stack我发现清除堆栈并在清除堆栈上打开新页面的最简单方法

await Navigation.PushAsync(new UserJourneysTabPage()); //Push the page you want to push
            var existingPages = Navigation.NavigationStack.ToList(); 
            //get all the pages in the stack
            foreach (var page in existingPages)
            {
                    //Check they type of the page if its not the 
                    //same type as the newly created one remove it
                    if (page.GetType() == typeof(UserJourneysTabPage))

                    continue;

                Navigation.RemovePage(page);
            }

Note: This doesn't clear a page from the navigation stack if its the same type, but you can always add a property to distinguish the newly created one from the old ones注意:如果页面类型相同,这不会从导航堆栈中清除页面,但您始终可以添加一个属性来区分新创建的和旧的

Iam posting this solution for removing any desired page from the navigation stack using linq.我发布了这个解决方案,用于使用 linq 从导航堆栈中删除任何所需的页面。

Here as an example, I am removing all pages except the current active page.在这里作为示例,我将删除除当前活动页面之外的所有页面。

var currentPage = Navigation.NavigationStack[Navigation.NavigationStack.Count - 1];
var pageList = Navigation.NavigationStack.Where(y => y != currentPage).ToList();

foreach(var page in pageList)
    Navigation.RemovePage(page);

The following routine works perfectly for me:以下例程非常适合我:

Using System.Linq;

......


try

{

 foreach (var item in Application.Current.MainPage.Navigation.NavigationStack.ToList()

{

       if (item.GetType().Name == "ClientsPage" || item.GetType().Name == "PagoPage")


       {   

           Application.Current.MainPage.Navigation.RemovePage(item);


       }
  }
         
}
catch() {
        }

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

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