[WinRT] Loading ViewModel data async

Topics: Getting Started
Jan 24, 2013 at 12:22 PM


I am new to CM and I'm using it for a new WinRT project.  

I have a view model which has an ObservableCollection property which is bound to my View.  Once the ViewModel is instantiated I want to load the data for the collection asynchronously.  How would I go about doing this with CM?

Before using CM I would load data in my view models by calling an async method on the relevant view model from the ViewModelLocator.

From what I understand I need to use a coroutine which is called from the OnInitialize method of the view model.  Is this correct or is there another way in CM for WinRT?  Can I just call an async method or will that not work?


Jan 24, 2013 at 12:36 PM

OK, maybe I should have tested it first before I posted.

Seems it is as simple as marking the OnInitialize method which I override in the view model as async and then call my async LoadData method from that.  Here is a quick example of how I changed the MenuViewModel of the WinRT sample


protected override async void OnInitialize()


        private async void LoadData()
            await Task.Delay(10000);


            Samples.Add(new SampleViewModel { Title = "Setup", Subtitle = "Setting up a WinRT application.", ViewModelType = typeof(SetupViewModel) });
            Samples.Add(new SampleViewModel { Title = "Binding Conventions", Subtitle = "Binding view model properties to your view.", ViewModelType = typeof(BindingsViewModel) });
            Samples.Add(new SampleViewModel { Title = "Action Conventions", Subtitle = "Wiring view events to view model methods.", ViewModelType = typeof(ActionsViewModel) });
            Samples.Add(new SampleViewModel { Title = "Coroutines", Subtitle = "Mix IResult and Async / Await", ViewModelType = typeof(CoroutineViewModel) });
            Samples.Add(new SampleViewModel { Title = "Execute", Subtitle = "Using Execute to execute code on the UI thread.", ViewModelType = typeof(ExecuteViewModel) });
            Samples.Add(new SampleViewModel { Title = "Navigating", Subtitle = "Navigating between pages and passing parameters.", ViewModelType = typeof(NavigationViewModel) });
            Samples.Add(new SampleViewModel { Title = "Search", Subtitle = "How to integrate the Share charm in your app.", ViewModelType = typeof(SearchViewModel) });
            Samples.Add(new SampleViewModel { Title = "Settings", Subtitle = "How to use your view models in the settings charm.", ViewModelType = typeof(SettingsViewModel) });
            Samples.Add(new SampleViewModel { Title = "Share Source", Subtitle = "How to use the share charm from your view model.", ViewModelType = typeof(ShareSourceViewModel) });


Jan 24, 2013 at 8:38 PM

In your example above you shouldn't need to make OnInitialize as async, the only time a method needs to be marked as async is when you're using the await keyword within the method (like you have in LoadData).

For instance if you changed LoadData to "async Task" rather than "async void" then you could await the call of LoadData, then you'd need to use the async keyword on OnInitialize.

Jan 25, 2013 at 12:12 AM

Thank you Nigel.  Learning something new every day :)