我是否应该以其他方式考虑取消Xamarin Forms中的“后退"按钮 [英] Should I be thinking differently about cancelling the back button in Xamarin Forms

查看:430
本文介绍了我是否应该以其他方式考虑取消Xamarin Forms中的“后退"按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于Prism的Xamarin Forms应用程序,该应用程序包含一个包装在导航页面中的编辑页面,因此Android和iOS上的左上方都有一个后退按钮.为了避免用户不小心单击后退"按钮(特别是在Android上)而意外丢失正在进行的编辑,我们希望提示他们确认是否确实要取消.

I have a Prism based Xamarin Forms app that contains an edit page that is wrapped in a Navigation page so there is a back button at top left on both Android and iOS. To avoid the user accidentally losing an edit in progress by accidentally clicking the back button (in particular on Android) we want to prompt them to confirm that they definitely want to cancel.

事实是,这似乎并没有融入Xamarin形式.您可以在导航页面中覆盖OnBackButtonPressed,但这仅在Android上被称为硬件/软件后退按钮.有文章详细介绍了拦截Android左上角实际箭头按钮的技术(涉及覆盖Android MainActivity中的OnOptionsItemSelected),但是在iOS上我不确定这是否有可能.

Thing is, this seems like something that is not baked in to Xamarin forms. You can override OnBackButtonPressed in a navigation page, but that only gets called for the hardware/software back button on Android. There are articles detailing techniques to intercept the actual arrow button at the top left on Android (involving overriding OnOptionsItemSelected in the Android MainActivity), but on iOS I'm not sure it is even possible.

所以我忍不住想知道我是否会以错误的方式进行操作?我是否应该以这种方式拦截左上方/硬件/软件后退按钮?似乎是一件很平常的事情(例如,在内置联系人"应用中的android中编辑新联系人时按回车键,您会得到提示),但确实感觉我在某种程度上正在与系统抗争.

So I can't help but wonder if I am going about this the wrong way? Should I not be intercepting the top left / hardware / software back button in this way? Seems like a pretty common thing to do (e.g. press back when editing a new contact in the android built in Contacts app and you get a prompt) but it really feels like I am fighting the system here somehow.

关于此问题,以前有很多问题,最相关的问题是

There are previous questions around this, most relevant appears to be How to intercept Navigation Bar Back Button Clicked in Xamarin Forms? - but I am looking for some broad brush suggestions for an approach here. My objective is to show the user a page with the <- arrow at top left for Android, "Cancel" for iOS but I would like to get some views about the best way to go about it that does not involve me fighting against prism / navigation pages / xamarin forms and (where possible) not breaking the various "best practices" on Android and iOS.

推荐答案

沿着与您相同的路径走,并被告知阻止用户返回,我决定在之后显示警报他们点击后退按钮(在ContentPage.OnDisappearing()内),上面写着您要保存您的工作吗?.

After going down the same path as you and being told not to prevent users from going back, I decided on showing an alert after they tap the back button (within ContentPage.OnDisappearing()) that says something like Would you like to save your work?.

如果采用这种方法,请确保使用Application.MainPage.DisplayAlert()而不是仅使用this.DisplayAlert(),因为此时ContentPage可能不可见.

If you go with this approach, be sure to use Application.MainPage.DisplayAlert() instead of just this.DisplayAlert() since your ContentPage might not be visible at that point.

当他们单击后退"按钮时,这是我当前处理保存工作的方式(我合并了很多代码并更改了一些内容):

Here is how I currently handle saving work when they click the back button (I consolidated a good bit of code and changed some things):

protected override async void OnDisappearing() {
    base.OnDisappearing();

    // At this point the page is gone or is disappearing, but all properties are still available

    #region Auto-save Check and Execution

    /*
     * Checks to see if any edits have been made and if a save is not in progress, if both are true, it asks if they want to save, if yes, it checks for validation errors.
     *  If it finds them, it marks it as such in the model before saving the model to the DB and showing an alert stating what was done
     */
    if(!_viewModel.WorkIsEdited || _viewModel.SaveInProgress) { //WorkIsEdited changes if they enter/change data or focus on certain elements such as a Picker
        return;
    }

    if(!await Application.Current.MainPage.DisplayAlert("ALERT", "You have unsaved work! Would you like to save now?", "Yes", "No")) {
        return;
    }

    if(await _viewModel.SaveClaimErrorsOrNotAsync()) { //The return value is whether validation succeeds or not, but it gets saved either way
        App.SuccessToastConfig.Message = "Work saved successfully. Try saving it yourself next time!";
        UserDialogs.Instance.Toast(App.SuccessToastConfig);
    } else if(await Application.Current.MainPage.DisplayAlert("ERROR", "Work saved successfully but errors were detected. Tap the button to go back to your work.", "To Work Entry", "OK")) {
        await Task.Delay(200);  //BUG: On Android, the alert above could still be displayed when the page below is pushed, which prevents the page from displaying //BUG: On iOS 10+ currently the alerts are not fully removed from the view hierarchy when execution returns (a fix is in the works)

        await Application.Current.MainPage.Navigation.PushAsync(new WorkPage(_viewModel.SavedWork));
    }

    #endregion
}

这篇关于我是否应该以其他方式考虑取消Xamarin Forms中的“后退"按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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