I have a View Model where
protected override void Initialize()
{
this.InnerViewModel.LoadInformation();
this.IsInformationLoaded = true;
}
however, this.InnerViewModel.LoadInformation();
is very CPU and IO intensive operation. It may take a few seconds to complete.
The View of that model has binding to IsInformationLoaded
to show a 'Loading...' screen.
My current implementation, as you can imagine, doesn't show the 'Loading...' screen, instead the UI is frozen until the operation is complete.
How do I change change the Initialize
method, to make the LoadInformation
asynchronous? Please bear in mind that InnerViewModel
is also View-bound.
Edit:
The following works
protected override async void Initialize()
{
await Task.Run(() =>
{
this.InnerViewModel.LoadInformation();
});
this.IsInformationLoaded = true;
}
// In InnerViewModel
public override void LoadInformation()
{
Thread.Sleep(3000);
Application.Current.Dispatcher.Invoke(() =>
{
this.SomethingObservable.Clear();
this.SomethingObservable.Add(...something ...);
});
}
however I really want to get rid of Application.Current.Dispatcher
, if possible. Or somehow move it into the Initialize()
. I don't want the InnerModel
to know anything about how it's being executed.