Xamarin的Android MvvmCross防止后退按钮返回到以前查看 [英] Xamarin MvvmCross Android Prevent Back Button returning to previous View

查看:1365
本文介绍了Xamarin的Android MvvmCross防止后退按钮返回到以前查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建以下状况做了解决方案:

I'm trying to create a neat solution for the following situation:

我已经建立,需要证书来验证用户的应用程序。每当API要重新认证用户,我想迫使用户返回到登录视图。此功能的伟大工程,但是当用户按下设备上的后退按钮时,以前的视图。我希望看到,当用户按下后退按钮退出应用程序。

I've build an app that requires credentials to authenticate the user. Whenever the API wants to reauthenticate the user I would like to force the user back to the login-view. This functionality works great, but when the user presses the back button on the device, the previous view is shown. I would like to see that when the user presses the back button the application exits.

MvvmCross具有使用的 MvxPresentationHint 。我创建了一个CustomAndroidViewPresenter和创建以下MvxAndroidSetup:

MvvmCross has the option of using MvxPresentationHint. I created a CustomAndroidViewPresenter and created the following MvxAndroidSetup:

public class Setup : MvxAndroidSetup
{
    private CustomAndroidViewPresenter _presenter;
    public Setup(Context applicationContext)
        : base(applicationContext)
    {
        _presenter = new CustomAndroidViewPresenter(applicationContext);
    }

    protected override IMvxAndroidViewPresenter CreateViewPresenter()
    {
        Mvx.RegisterSingleton(_presenter);
        return _presenter;
    }
}



我知道,你应该调用Finish();一项活动,以防止后退按钮导航回未经验证的视图。但铸造ApplicationContext来抛出异常。

I know that you should call Finish(); on an activity to prevent the backbutton to navigate back to the unauthenticated view. But casting the applicationContext to throws an exception.

public CustomAndroidViewPresenter(Context context)
{
    _context = context;
}

public override void ChangePresentation(MvxPresentationHint hint)
{
    if (hint is LoginOnlyBackStackHint)
    {
        var context = Application.Context;
        if (context is Activity)
        {
            // Context is NOT activity
        }
        try
        {
            Activity x = (Activity) context;
            x.Finish();
            // Exception is thrown
        }
        catch (Exception ex)
        {

        }
    }
    base.ChangePresentation(hint);
}



是否有人有任何想法如何实现这一目标?

Does someone have any ideas how to achieve this?

在预先感谢。

推荐答案

我有同样的问题。只是实现自定义的演示中,设定一些意向性的标志,如果你想才达到这样的:

I had the same problem. Just implement a custom Presenter, that sets some intent flags, if you want to achive this:

public class CustomAndroidPresenter : MvxAndroidViewPresenter 
    {
        public override void Show(MvxViewModelRequest request)
        {
            if (request != null && request.PresentationValues != null)
            {
                if (request.PresentationValues.ContainsKey("MyCustomFlag"))
                {
                    // Get intent from request and set flags to clear backstack.
                    var intent = base.CreateIntentForRequest(request);
                    intent.SetFlags(ActivityFlags.ClearTask | ActivityFlags.NewTask);

                    base.Show(intent);
                    return;
                }
            }

            base.Show(request);
        }
    }



然后,你需要设置这个主持人在设置类

Then you need to set this presenter in your setup class:

protected override IMvxAndroidViewPresenter CreateViewPresenter()
        {
            var presenter = new CustomAndroidPresenter();
            Mvx.RegisterSingleton<IMvxViewPresenter>(presenter);
            return presenter;
        }



然后显示一个视图模型(如您的登录视图),只要将定制标志键代码演示者知道,他应该设置INTCN-标志:

And then to show a viewmodel (like your login-view) just set your custom-flag-key-code that the presenter knows that he should set the inten-flags:

protected void ShowViewModel<TViewModel>(bool clearbackstack) where TViewModel : MvxViewModel
        {
            if (clearbackstack)
            {
                var presentationBundle = new MvxBundle(new Dictionary<string, string> { { "MyCustomFlag", "" } });
                ShowViewModel<TViewModel>(presentationBundle: presentationBundle);
                return;
            }

        // Normal start
        ShowViewModel<TViewModel>();
    }

要显示视图模型(无背导航),只要用下面的代码:

To show a viewmodel (without back-navigation) just use following code:

ShowViewModel<MyViewModel>(true);



然后,当你按下后退按钮,你不能导航回到以前的,因为backstack活动被清零。

And then, when you press the back-button you cant navigate back to your previous activity because the backstack is cleared.

这篇关于Xamarin的Android MvvmCross防止后退按钮返回到以前查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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