Xamarin.Forms 如何禁用导航栏中的后退按钮 [英] Xamarin.Forms How to disable Back Button In Nav Bar

查看:75
本文介绍了Xamarin.Forms 如何禁用导航栏中的后退按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Xamairn.Forms,如何禁用导航栏中的后退按钮ViewList 正在刷新.

I am using Xamairn.Forms, How I can Disable Back Button In Navigation Bar when ViewList Is Refreshing.

实际上,我正在使用以下代码禁用默认后退按钮,但现在我正试图找到禁用导航栏后退按钮

Actually, I am using the following code to disable Default Back Button, but right now I am trying to find away also to disable Navigation Bar back button

 protected override bool OnBackButtonPressed()
   {
     if (ListView.IsRefreshing)
         return true;
      return false;
   }

知道如何禁用导航栏中的后退按钮吗?

Any Idea how I Can Disable Back Button In Navigation bar?

推荐答案

总的来说,Android 和 iOS 没有正常的 API 来阻止导航栏后退按钮被执行,这是一个很好的理由,用户希望能够去回来,你不应该阻止他们这样做.

Overall Android and iOS do not have normal APIs to prevent a navigation bar back button from being executed for one very good reason, the user expects to be able to go back and you should not prevent them from doing so.

您应该改变您的设计,只为用户提供您希望他们能够完全创建或隐藏导航栏的选项,并使用您自己的按钮创建自己的导航栏.

You should instead change your design by only giving the user the options you want them to be able to make or hide the navigation bar entirely and create your own bar with your own buttons.

Adam 的指南 提供了我对这个主题的绝佳概览希望我在遇到这个问题时能发现.

Adam's guide provides a great over view of the subject which I wish I would have found when faced with this issue.

他提出了我刚刚做过的同样的事情,但也提供了非常hacky的方法来防止在您拒绝更改设计时返回(下面的代码从上面链接的文章中大部分被取消):

He suggest the same things I just did but also provides VERY hacky ways to prevent navigating back if you refuse to change your design (code below is lifted mostly unchanged from the article linked above):

在 Android 上,您可以尝试找到后退按钮并使用 ContentPage 的 ViewModel 方法来确定是否应该允许返回.您将在 MainActivity 中覆盖 OnOptionsItemSelected:

On Android you can attempt to find the back button and use the ContentPage's ViewModel method to find out if going back should be allowed or not. You would overwrite OnOptionsItemSelected within MainActivity:

public override bool OnOptionsItemSelected(IMenuItem item) {
    var app = Application.Current;

    if (item.ItemId == 16908332) { // This makes me feel dirty.
        var navPage = ((app.MainPage.Navigation.ModalStack[0] as MasterDetailPage).Detail as NavigationPage); // Notice this code assumes it is looking for a MasterDetailPage being shown as a modal

        if (app != null && navPage.Navigation.NavigationStack.Count > 0) {
            int index = navPage.Navigation.NavigationStack.Count - 1;

            var currentPage = navPage.Navigation.NavigationStack[index];

            var vm = currentPage.BindingContext as ViewModel; //You would want to cast this to a base ViewModel or a specific one so that OnBackButtonPressed() can be called

            if (vm != null) {
                var answer = vm.OnBackButtonPressed();
                if (answer) {
                    return true;
                }
            }
        }
    }

    return base.OnOptionsItemSelected(item);
}

在 iOS 上,您需要一个自定义渲染器来为您自己的自定义渲染器切换真正的后退按钮:

On iOS you need a custom renderer to switch out the real back button for your own custom one:

[assembly: ExportRenderer(typeof(Page), typeof(CustomPageRenderer))]
namespace Mobile.iOS.CustomRenderer {
    public class CustomPageRenderer : PageRenderer {

        public override void ViewWillAppear(bool animated) {
            base.ViewWillAppear(animated);

            var page = Element as CorePage;

            if (page != null) {
                if ((page).OverrideBackButton) {
                    var root = this.NavigationController.TopViewController;
                    // NOTE: this doesn't look exactly right, you need to create an image to replicate the back arrow properly
                    root.NavigationItem.SetLeftBarButtonItem(new UIBarButtonItem("< Back", UIBarButtonItemStyle.Plain, (sender, args) => {
                        var navPage = page.Parent as NavigationPage;
                        var vm = page.BindingContext as ViewModel;

                        if (vm != null) {
                            var answer = vm.OnBackButtonPressed();

                            if (!answer)
                                navPage.PopAsync();
                        } else {
                            navPage.PopAsync();
                        }
                    }), true);
                }
            }    
        }
    }
}

在 Windows Phone 上...只需覆盖 OnBackButtonPressed... 使用 WP 实际上很容易一次.

On Windows Phone... just override OnBackButtonPressed... working with WP is actually easy for once.

这篇关于Xamarin.Forms 如何禁用导航栏中的后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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