Ibr*_*him 13 c# navigation android xamarin.forms
我的应用程序中的导航问题.我使用xamarin.forms如何清理我的导航堆栈.没有用Pop和push.我可以看到我的完整导航堆栈吗?
use*_*er1 37
在最新版本的Xamarin.Forms中,您可以看到使用的导航堆栈
Navigation.NavigationStack
Run Code Online (Sandbox Code Playgroud)
因此你可以使用
var existingPages = Navigation.NavigationStack.ToList();
foreach(var page in existingPages)
{
Navigation.RemovePage(page);
}
Run Code Online (Sandbox Code Playgroud)
此代码必须进入导航页面后面的代码或实现INavigation的代码.
更多信息Xamarin.Forms.INavigation成员
这是我用来清空堆栈并导航到指定页面的函数.(用例是应用程序在使用过程中被取消激活,我需要将用户踢出去)
public async Task PopAllTo(ViewModel vm)
{
if (vm == null) return;
Page page = PreparePage(vm); //replace 'page' with the page you want to reset to
if (page == null) return;
_navigation.InsertPageBefore(page, _navigation.NavigationStack.First());
await _navigation.PopToRootAsync();
}
Run Code Online (Sandbox Code Playgroud)
对于在 2020 年查看此内容的任何人:
await Navigation.PopToRootAsync();
Run Code Online (Sandbox Code Playgroud)
或者如果您使用 Shell 导航
await Shell.Current.Navigation.PopToRootAsync();
Run Code Online (Sandbox Code Playgroud)
你可以试试这个...
public void ResetNavigationStack()
{
if (_navigation != null && _navigation.NavigationStack.Count() > 0)
{
var existingPages = _navigation.NavigationStack.ToList();
foreach (var page in existingPages)
{
_navigation.RemovePage(page);
}
}
}
Run Code Online (Sandbox Code Playgroud)
和嘘!导航堆栈清除了兄弟!
或者,如果您想重置模式堆栈
public async Task<Page> PopAllModals()
{
Page root = null;
if (_navigation.ModalStack.Count() == 0)
return null;
for (var i = 0; i <= _navigation.ModalStack.Count(); i++)
{
root = await _navigation.PopModalAsync(false);
}
return root;
}
Run Code Online (Sandbox Code Playgroud)
和嘘!这些模态消失了!
| 归档时间: |
|
| 查看次数: |
20111 次 |
| 最近记录: |