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

前端 未结 1 419
广开言路
广开言路 2021-01-24 04:11

I have a Windows Phone 8.1 MVVM Light project and I am struggling to keep it Blendable.

As I see it I have a few options. I can load different view mode

相关标签:
1条回答
  • 2021-01-24 04:44

    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..
       }
    }
    
    0 讨论(0)
提交回复
热议问题