0

I have the following ViewModel

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;
    }
}

The property AuthenticationProviders is bound to a ListView. The problem is that once the AuthenticationProviders are populated through the Async method (via) the constructor the ListView does not get updated. I tried RaisePropertyChanged but then I get the error The application called an interface that was marshalled for a different thread. This makes sense as well since its actually calling from another thread. How do I fix this?

1
  • Why aren't you scheduling the continuation delegate to run on the calling thread, i.e. add TaskScheduler.FromCurrentSynchronizationContext() to ContinueWith and then call RaisePropertyChanged ? Commented Aug 13, 2014 at 12:19

1 Answer 1

2

This literally contains the answer

http://msdn.microsoft.com/en-us/magazine/dn630646.aspx

Basically you have to use

    DispatcherHelper.Initialize 

And then whenever you need to update you need to use DispatcherHelper.CheckBeginInvokeOnUI

Given is the sample

    DispatcherHelper.CheckBeginInvokeOnUI(() =>
            {
                RaisePropertyChanged(() => AuthenticationProviders);
            });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.