简体   繁体   English

Xamarin 表单 - 关闭导航页面

[英]Xamarin Forms - Dismiss NavigationPage

I am trying to dismiss the current page I am on when I use:我正在尝试关闭我在使用时所在的当前页面:

Detail = new NavigationPage((Page)Activator.CreateInstance(item.TargetType));

How would I do that?我该怎么做? I tried:我试过:

Application.Current.MainPage.Navigation.PopAsync();

But got this error:但是得到了这个错误:

PopAsync is not supported globally on iOS, please use a NavigationPage. iOS 不支持 PopAsync,请使用 NavigationPage。

What am I doing wrong?我究竟做错了什么?

your MainPage is a MasterDetail , so you need to您的MainPageMasterDetail ,因此您需要

var nav = ((MasterDetailPage)Application.Current.MainPage).Detail.Navigation;
nav.PopAsync();

Depending on where you are trying to dismiss the current page from you can use:根据您尝试关闭当前页面的位置,您可以使用:

await Navigation.PopAsync();

If you're trying to dismiss the Page from outside the context of a NavigationPage, for example in a Controller class you can send the current Page's Navigation instance to the Controller.如果您尝试从 NavigationPage 的上下文外部关闭 Page,例如在 Controller 类中,您可以将当前 Page 的 Navigation 实例发送到 Controller。

Something like this in your Page:在您的页面中是这样的:

public MyConentPage () {
    var myController = new MyController(Navigation);
}

Then inside your Controller:然后在您的控制器中:

public MyController (INavigation Navigation) {
    this.Navigation = Navigation;
}

public async void ClosePage () {
    await Navigation.PopAsync();
}

as per your code, you are setting a Detail Page from Master Detail Page, So that you Detail Page is work like an Initial Page, you did not push or pop in to Navigation Page.根据您的代码,您正在从主详细信息页面设置详细信息页面,以便您的详细信息页面像初始页面一样工作,您没有推送或弹出导航页面。

So Simple Solution is use to set Desire Page of Application or where you want to move using Application Context Like, So Simple Solution 用于设置应用程序的 Desire Page 或使用 Application Context Like 移动的位置,

Application.Current.MainPage = new NavigationPage(new NewPage());

Hope this helps.希望这可以帮助。 if any Query than Please feel free to ask.如果有任何疑问,请随时询问。

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

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