首页 文章

防止在android上的xamarin表单中的后退按钮关闭

提问于
浏览
15

我想通过按下android上的xamarin表单中的硬件后退按钮来阻止关闭应用程序 .

我想,你可以使用应用程序中的硬件后退按钮(工作正常)进行导航,但是当达到导航堆栈中的第一页时,不想退出 .

我试图在xamarin表单中使用OnSleep事件,但在这里我无法取消退出 .

我也尝试在android中捕获后退按钮:

public override void OnBackPressed()
    {
        //base.OnBackPressed();
    }

但是当使用xamarin表单时,我不知道当前显示的是哪个页面 . 所以我不知道是否允许导航

6 回答

  • 10

    它适用于评估NavigationStack(当您使用NavigationPage时) .

    在我的Activity中,我重写了OnBackPressed

    public override void OnBackPressed()
    {
        if(App.Instance.DoBack)
        {
            base.OnBackPressed();
        }
    }
    

    在我的xamarin表单应用程序(App.Instance(它是一个单例))中,我将像这样评估当前页面的NavigationStack .

    public bool DoBack
    {
        get
        {
            NavigationPage mainPage = MainPage as NavigationPage;
            if (mainPage != null)
            {
                return mainPage.Navigation.NavigationStack.Count > 1;
            }
            return true;                
        }
    }
    

    当NavigationStack中只剩下一页时,我不会调用base.OnBackPressed,这样我就不会关闭App了 .

    ![测试]

  • 0

    这就是Xamarin Forms MasterDetail页面场景代码的样子......

    public bool DoBack
        {
            get
            {
                MasterDetailPage mainPage = App.Current.MainPage as MasterDetailPage;
    
                if (mainPage != null)
                {    
                    bool canDoBack = mainPage.Detail.Navigation.NavigationStack.Count > 1 || mainPage.IsPresented;
    
                    // we are on a top level page and the Master menu is NOT showing
                    if (!canDoBack)
                    {
                        // don't exit the app just show the Master menu page
                        mainPage.IsPresented = true;
                        return false; 
                    }
                    else
                    {
                        return true;
                    }                    
                }
                return true;
            }
        }
    
  • 0

    只需在页面中给你一个空白的电话,你想要阻止,比如

    protected override bool OnBackButtonPressed()
    {
    return true;
    }
    

    这将阻止XF-Droid中的后退按钮 .

  • 5

    这是一个适用于Android的解决方案 .
    在应用程序类中引入一个计数器,并使用每个 OnStart 递增计数器,并在计数器为 1 时以每个 OnStop 递减计数器,您将知道您处于上次活动中 .

    不用说,使用基本活动实现,这样您就不必复制过去的样板代码 .

  • 0

    提出的解决方案可以很好地工作,但我不喜欢“静态属性暴露”来解决问题 . 更重要的是,我不喜欢使用“属性作为方法”解决方案,特别是涉及到很多逻辑时 .

    这里的主要问题是我们如何处理来自Xamarin.Forms.Application类的OnBackButton()方法 .

    那么以更优雅的方式做同样的事情呢?

    首先,您需要像这样扩展Xamarin.Forms.Application类:

    namespace YourNameSpace
    {
        public class Application : Xamarin.Forms.Application
        {
            #region Public Methods
    
            public bool HandleBackButton()
            {
                return OnBackPressed();
            }
    
            #endregion
    
            #region Application Methods
    
            protected virtual bool OnBackPressed()
            {
                return false;
            }
    
            #endregion
        }
    }
    

    您的App实现现在将使用此类作为基类 . 记得相应地修改你的xaml和你的xaml.cs:

    <common:Application xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:common="clr-namespace:YourNameSpace"
                 x:Class="YourNameSpace.App">
    </common:Application>
    

    在App.cs实现中,您现在可以覆盖OnBackButton()方法 .

    public partial class App : YourNameSpace.Application
    {
        #region Constructors
    
        public App()
        {
            InitializeComponent();
        }
    
        #endregion
    
        #region App Methods
    
        protected override bool OnBackPressed()
        {
            // Handle when the back button is pressed
            return false;
        }
    
        #endregion
    }
    

    然后你需要改变一下你的Activity类实现 .

    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        #region Constants and Fields
    
        private App _app;
    
        #endregion
    
        #region Activity Methods
    
        protected override void OnCreate(Bundle bundle)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;
    
            base.OnCreate(bundle);
    
            global::Xamarin.Forms.Forms.Init(this, bundle);
    
            _app = new App();
            LoadApplication(_app);
        }
    
        public override void OnBackPressed()
        {
            if(!_app.HandleBackButton())
                base.OnBackPressed();
        }
    
        #endregion
    }
    
  • 16

    扩展Chris的答案,因为现在没有 App.Instance ,并且无法在平台代码中以静态方式访问App .

    1. App.xaml.cs

    public  bool DoBack
            {
                get
                {
                    return MainPage.Navigation.NavigationStack.Count>1;
                }
            }
    

    2.MainActivity.cs under Android project

    • 声明类变量App

    App app;

    119101 _119101_更改 LoadApplication(new App());

    app = new App();
    LoadApplication(app);

    • 和覆盖方法 OnBackPressed()
    public override void OnBackPressed()
    {
        if (app.DoBack)
        {
            base.OnBackPressed();
        }
    
    }
    

相关问题