CoreDispatcher.HasThreadAccess会“更改" [英] CoreDispatcher.HasThreadAccess "breaking change"

查看:55
本文介绍了CoreDispatcher.HasThreadAccess会“更改"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要接管使用MvvmCross.vNext开发的应用程序.
在尝试使用MvvmCross.V3更新它时,我发现了以下重大更改:在MainViewModel的构造函数中,我们显示LoginViewModel(ShowViewModel()). 在vNext中工作正常.
但是对于V3,LoginView不会显示.
经过长时间搜索,我发现在 MvxStoreMainThreadDispatcher.RequestMainThreadAction 中添加了以下代码:

I am taking over an application developed with MvvmCross.vNext.
While trying to update it with MvvmCross.V3, I found the following breaking change: in the constructor of the MainViewModel, we show the LoginViewModel (ShowViewModel()). It worked fine in vNext.
But with V3, the LoginView doesn't show.
After a long search, I found out that the following code, added in MvxStoreMainThreadDispatcher.RequestMainThreadAction :

        if (_uiDispatcher.HasThreadAccess)
        {
            action();
            return true;
        }

对我的麻烦负责.
如果我将其注释掉,我的应用程序将像以前一样工作,但是我猜这里的代码出于某些原因而存在...
您有什么建议吗?
我可以在不更改MvvmCross源代码的情况下强制执行以前的行为吗?
我应该重构代码来以不同的方式处理LoginView吗?
预先感谢您的评论.
菲利普(Philippe)

was responsible for my troubles.
If I comment it out, my application works as previously, but I guess this code is there for some reasons...
Do you have any suggestions ?
Can I force the previous behavior without changing MvvmCross source code?
Should I refactor the code to handle the LoginView differently ?
Thanks in advance for your comments.
Philippe

推荐答案

在尝试使用MvvmCross.V3更新它时,我发现了以下重大更改:在MainViewModel的构造函数中,我们显示LoginViewModel(ShowViewModel()).在vNext中效果很好.

While trying to update it with MvvmCross.V3, I found the following breaking change: in the constructor of the MainViewModel, we show the LoginViewModel (ShowViewModel()). It worked fine in vNext.

我认为您的构造函数导航将在任何MvvmCross版本的多个平台上中断.老实说,我认为您很幸运,以前它能奏效.

I think your constructor navigation would have broken on several platforms in any MvvmCross version. To be honest, I think you were lucky it worked before.

问题是ViewModel是在诸如ViewDidLoadOnNavigatedToOnCreate的View事件期间构造(或定位)的-这些事件通常是 在页面过渡"期间调用

The problem is that ViewModels are constructed (or located) during View events such as ViewDidLoad, OnNavigatedTo, and OnCreate - and these events are normally called during 'page transitions'

要解决此问题,您将需要将登录导航移出构造函数.

To work around this you will need to move your login navigation out of the constructor.

如何执行此操作取决于您的应用程序

How you do this depends on your app

  • 如果确实需要Home-> Login Backstack,则可以触发一些异步或时间延迟,也可以触发一些其他View事件,例如ViewDidAppear

如果您不需要该堆栈,那么我通常实现这种事情的方式是使用自定义IMvxAppStart-类似:

if you don't need that backstack then the way I normally implement this sort of thing is to use a custom IMvxAppStart - something like:

public class AppStart
    : MvxNavigatingObject
    , IMvxAppStart      
{
    public void Start(object hint = null)
    {
        var authService = Mvx.Resolve<IMySerice>();
        if (authService.IsLoggedIn)
        {
            ShowViewModel<HomeViewModel>();
        }
        else
        {
            ShowViewModel<LoginViewModel>();
        }
    }
}

(您可以在 https://github.com/slodge/MvvmCross/blob/v3/Sample%20-%20CirriousConference/Cirrious.Conference.Core/ApplicationObjects/AppStart.cs )

可以使用以下方法在App.cs启动中进行注册:

This can be registered in App.cs startup using:

RegisterAppStart(new AppStart());

这篇关于CoreDispatcher.HasThreadAccess会“更改"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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