在视图模型的构造函数中异步加载 MVVM Light 中的 Blendable 示例数据 [英] Asynchronously loading Blendable sample data in MVVM Light in the view model's constructor
问题描述
我有一个 Windows Phone 8.1 MVVM Light 项目,我正在努力保持它的可混合性.
据我所知,我有几个选择.我可以根据 ViewModelBase.IsInDesignModeStatic
在 ViewModelLocator
构造函数中是否为真加载不同的视图模型,或者我可以在视图中测试 ViewModelBase.IsInDesignModeStatic
模型构造函数并适当地加载数据.
如果 ViewModelBase.IsInDesignModeStatic
为真,我需要从文件加载数据.这是我的代码:
public async TaskGetSampleDataAsync(){if (_DeserializedThingsSampleDataSource == null){var dataUri = new Uri(_SampleDataJSONFile);var file = await StorageFile.GetFileFromApplicationUriAsync(dataUri);var jsonText = await FileIO.ReadTextAsync(file);_DeserializedThingsSampleDataSource = JsonConvert.DeserializeObject(jsonText);}返回 _DeserializedThingsSampleDataSource;}
当我调用该方法时,我需要标记调用await
,从而标记调用方法async
.但是构造函数不能被标记为async
.
或者我可以提供一个 ContinueWith 延续而不是等待异步代码的返回.但是 Blend 在 ContinueWith 完成之前加载页面.
鉴于示例数据已加载到视图模型或定位器服务构造函数,并且它必须从文件(异步活动)加载数据,我该如何在 MVVM Light 中执行此操作,以便Blend 中是否提供示例数据?
(注意我找到的其他答案,例如这个,不要使用 MVVM Light.)
在 页面加载 事件上加载数据,使用命令,以便您可以利用 await/async 东西.我不知道它如何与混合一起使用,因为我不经常使用它.
<块引用>查看:
<i:EventTrigger EventName="已加载"><i:InvokeCommandAction Command="{Binding PageLoadedCommand}"/></i:EventTrigger></i:Interaction.Triggers>
<块引用>
视图模型:
public RelayCommand PageLoadedCommand { get;私人订制;}公共 MyConstructor(IService serviceInjected){PageLoadedCommand = new RelayCommand(async()=>await OnPageLoaded());....}私有异步任务 OnPageLoaded(){如果(ViewModelBase.IsInDesignModeStatic){var data = await GetSampleDataAsync();//做一点事..}}
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 models depending on whether ViewModelBase.IsInDesignModeStatic
is true in the ViewModelLocator
constructor, or I can test ViewModelBase.IsInDesignModeStatic
in the view model constructor and load data appropriately.
If ViewModelBase.IsInDesignModeStatic
is true I need to load data from file. Here's my code:
public async Task<ThingsSampleDataSource> GetSampleDataAsync()
{
if (_DeserializedThingsSampleDataSource == null)
{
var dataUri = new Uri(_SampleDataJSONFile);
var file = await StorageFile.GetFileFromApplicationUriAsync(dataUri);
var jsonText = await FileIO.ReadTextAsync(file);
_DeserializedThingsSampleDataSource = JsonConvert.DeserializeObject<ThingsSampleDataSource>(jsonText);
}
return _DeserializedThingsSampleDataSource;
}
When I call that method I need to mark the call await
and thus the calling method async
. But constructors cannot be marked async
.
Or I can provide a ContinueWith continuation instead of awaiting return of the asynchronous code. But Blend loads the page before the ContinueWith is complete.
Given that sample data is loaded in the view model or the locator service constructors and it has to load data from a file, an asynchronous activity, how do I do this in MVVM Light so that the sample data is available in Blend?
(N.B. other answers I have found, for example this one, do not use MVVM Light.)
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..
}
}
这篇关于在视图模型的构造函数中异步加载 MVVM Light 中的 Blendable 示例数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!