我有4页,第1页,第2页,第3页。第4页。我使用推模式异步进行导航。当我点击按钮单击第4页时,它正在导航到page2。但是点击页面2中的后退按钮将显示堆栈页面的所有历史记录。因此,如何从模式堆栈中删除页面。我还使用了navigation.Remove(page),但它引发了异常。请指导。
foreach (var page in Navigation.ModalStack)
{
if (page is Page3)
{
await PopModalPage();
}
}
foreach (var page in Navigation.ModalStack)
{
if (page is Page4)
{
await PopModalPage();
}
}
要从第4页导航到第2页,我正在使用此代码 有没有比这更好的方法了?
答案 0 :(得分:0)
您可以使用以下帮助器功能
/// <summary>
/// Class with extension methods for INavigation interface to help working with the modal page stack
/// </summary>
public static class ModalHelper
{
/// <summary>
/// Unwinds the modal stack of the navigation until reaching a page with the given type
/// </summary>
/// <typeparam name="PageType"></typeparam>
/// <param name="navigation">The navigation object of the current top page</param>
/// <returns></returns>
public async static Task UnwindModalStackTo<PageType>(this INavigation navigation) where PageType : Page
{
await navigation.UnwindModalStackTo(p => p is PageType);
}
/// <summary>
/// Unwinds the modal stack of the navigation until reaching the given page
/// </summary>
/// <param name="navigation">The navigation object of the current top page</param>
/// <param name="page">The page where to stop unwinding the modal stack</param>
public async static Task UnwindModalStackTo(this INavigation navigation, Page page)
{
await navigation.UnwindModalStackTo(p => p == page);
}
/// <summary>
/// Unwinds the modal stack of the navigation until reaching a page that fulfils the predicate
/// </summary>
/// <param name="navigation">The navigation object of the current top page</param>
/// <param name="predicate">A function which tests whether to stop at a given page</param>
public async static Task UnwindModalStackTo(this INavigation navigation, Func<Page, bool> predicate)
{
bool found = false;
while (navigation != null && navigation.ModalStack.Count > 0)
{
// Get the current top page of the modal stack
Page topPage = navigation.ModalStack[navigation.ModalStack.Count - 1];
// Get the second page in the modal stack from the top (This one will become top next after we pop)
Page parentPage;
if (navigation.ModalStack.Count > 1)
{
parentPage = navigation.ModalStack[navigation.ModalStack.Count - 2];
}
else
{
parentPage = null;
}
// When the top page fulfills the predicate, stop
if (predicate(topPage))
{
found = true;
break;
}
// Pop the top page
await navigation.PopModalAsync();
// We need to use the navigation of the new top page from here on
navigation = parentPage?.Navigation;
}
// When the target page was not found, throw an exception
if (!found)
{
throw new Exception("Desired page not found in modal stack");
}
}
}
请注意,从堆栈中弹出顶部的模式页面后,我们必须使用新首页中的“导航”继续操作。
示例:
想象一下,我们有一个包含五个页面的应用程序:MainPage,Page1,Page2,Page3,Page4。
主页上有一个按钮可以打开Page1模式,Page1有一个按钮可以打开Page2模式,依此类推。
在主页中:
private async void Button_Clicked(object sender, EventArgs e)
{
Page p = new Page1();
await Navigation.PushModalAsync(p);
}
在第1页中:
private async void Button_Clicked(object sender, EventArgs e)
{
Page p = new Page2();
await Navigation.PushModalAsync(p);
}
依此类推...
现在在Page4中(为了返回到Page2),我们使用以下代码关闭所有打开的模式页面,直到到达Page2。
private async void Button_Clicked(object sender, EventArgs e)
{
await Navigation.UnwindModalStackTo<Page2>();
}
答案 1 :(得分:-1)
请使用await Navigation.PopModalAsync()
并通读this link以获得有关ModalPages
的信息。
编辑:
尚未测试过,但可以帮助您了解这种方法:
for (var i = Navigation.NavigationStack.Count - 1; i > 0; i--)
{
if (Navigation.NavigationStack[i] is Page2)
{
return;
}
await Navigation.PopModalAsync();
}
答案 2 :(得分:-1)
RemovePage
仅支持从NavigationStack
中删除指定的页面。
像这样:Navigation.RemovePage(Navigation.NavigationStack[Navigation.NavigationStack.Count - 1]);
您可以参考此以获取更多详细信息: https://docs.microsoft.com/en-us/dotnet/api/xamarin.forms.inavigation?view=xamarin-forms
答案 3 :(得分:-1)
如果您想返回2页,这将起作用
Navigation.RemovePage(Navigation.NavigationStack[Navigation.NavigationStack.Count - 2]);
Navigation.RemovePage(this);
首先,我们先删除当前页面之前的页面,然后再删除当前页面。