Asynchronously loading Blendable sample data in MVVM Light in the view model's constructor

情到浓时终转凉″ 提交于 2019-12-02 05:13:49

Load your data on the page load event, using a Command, so you can take advantage of the await/async stuff. I don't know how this works with blend as I don't use it much.

View:

<i:Interaction.Triggers>
    <i:EventTrigger EventName="Loaded">
        <i:InvokeCommandAction Command="{Binding PageLoadedCommand}"/>
    </i:EventTrigger>   
</i:Interaction.Triggers>

ViewModel:

public RelayCommand PageLoadedCommand { get; private set; }
public MyConstructor(IService serviceInjected)
{
    PageLoadedCommand = new RelayCommand(async()=>await OnPageLoaded());
....
}

private async Task OnPageLoaded()
{
   if(ViewModelBase.IsInDesignModeStatic)
   {
       var data = await GetSampleDataAsync();
       //Do something..
   }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!