UI Theme Release with View Model in MVVMCross

I use MVVMCross with my cross-platform application for Windows Phone and Android. In the main model of the main project, I do some background work using TPL, and I want to make sure that in the callback, when I make changes to the properties of the view model, which will cause the user interface to change, that the code runs on the UI, how can I achieve this ?

For the code, that's how he likes it.

    private MvxGeoLocation _currentLocation;
    private Task<MvxGeoLocation> GetCurrentLocation()
    {
        return Task.Factory.StartNew(() =>
            {
                while (_currentLocation == null && !LocationRetrievalFailed)
                {
                }
                return _currentLocation;
            });
    }

    var location = await GetCurrentLocation();
    if (LocationRetrievalFailed)
    {
        if (location == null)
        {
            ReverseGeocodingRequestFailed = true;
            return;
        }
        // Show toast saying that we are using the last known location
    }
    Address = await GooglePlaceApiClient.ReverseGeocoding(location);
+3
source share
1 answer

Have you tried IMvxMainThreadDispatcher?

var dispatcher = Mvx.Resolve<IMvxMainThreadDispatcher>();
dispatcher.RequestMainThreadAction(()=> { .... });

More on implementation:

https://github.com/MvvmCross/MvvmCross/search?q=IMvxMainThreadDispatcher&type=Code

I usually don’t think you need it.

, async .

, ?

+12

All Articles