Asynchronous Windows Phone应用程序MVVM,从viewmodel中的异步方法更新UI

Asynchronous Windows Phone应用程序MVVM,从viewmodel中的异步方法更新UI,asynchronous,mvvm,win-universal-app,Asynchronous,Mvvm,Win Universal App,我有下面的视图模型 public class MainViewModel : ViewModelBase { public bool Processing { get; set; } private IEnumerable<ExternalLoginModel> _authenticationProviders; /// <summary> /// /// </summar

我有下面的视图模型

public class MainViewModel : ViewModelBase
{

    public bool Processing
    {
        get;
        set;
    }

    private IEnumerable<ExternalLoginModel> _authenticationProviders;
    /// <summary>
    /// 
    /// </summary>
    public IEnumerable<ExternalLoginModel> AuthenticationProviders
    {
        get
        {
            return _authenticationProviders;
        }
        set
        {
            _authenticationProviders = value;               
            RaisePropertyChanged(() => AuthenticationProviders);
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public MainViewModel(IAuthenticationService authService)
    {
        Processing = true;
        authService.GetSupportedAuthenticationProvidersAsync().ContinueWith(authProviders => { PopulateAuthProviders(authProviders.Result); });
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="authProviders"></param>
    private void PopulateAuthProviders(IEnumerable<ExternalLoginModel> authProviders)
    {
        AuthenticationProviders = authProviders;
        Processing = false;
    }
}
public类MainViewModel:ViewModelBase
{
公共布尔处理
{
得到;
设置
}
私有IEnumerable\u身份验证提供程序;
/// 
/// 
/// 
公共IEnumerable AuthenticationProviders
{
得到
{
返回_authenticationProviders;
}
设置
{
_authenticationProviders=值;
RaisePropertyChanged(()=>AuthenticationProviders);
}
}
/// 
/// 
/// 
公共主视图模型(IAAuthenticationService authService)
{
处理=真;
authService.GetSupportedAuthenticationProvidersAsync().ContinueWith(authProviders=>{PopulateAuthProviders(authProviders.Result);});
}
/// 
/// 
/// 
/// 
私有void PopulateAuthProviders(IEnumerable authProviders)
{
AuthenticationProviders=authProviders;
处理=假;
}
}

属性AuthenticationProviders绑定到ListView。问题在于,一旦通过异步方法(通过构造函数)填充了AuthenticationProviders,ListView就不会得到更新。我尝试了RaisePropertyChanged,但随后出现了错误应用程序调用了为不同线程编组的接口。这也很有意义,因为它实际上是从另一个线程调用的。如何解决这个问题?

这确实包含了答案

基本上你必须使用

    DispatcherHelper.Initialize 
然后,无论何时需要更新,都需要使用 DispatcherHelper.CheckBeginInvokeOnUI

这是样品

    DispatcherHelper.CheckBeginInvokeOnUI(() =>
            {
                RaisePropertyChanged(() => AuthenticationProviders);
            });

为什么不安排继续委托在调用线程上运行,即将TaskScheduler.FromSynchronizationCurrentContext()添加到ContinueWith,然后调用RaisePropertyChanged?