MVVM视图模型异步数据初始化 [英] MVVM viewmodel async data initialization

查看:443
本文介绍了MVVM视图模型异步数据初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想熟悉Windows应用商店的应用程序和MVVM模式(我是新来的这个平台)。我有一个非常简单的应用程序解析Person对象的列表从JSON文件,然后显示他们在GridView。我创建了一个PeopleViewModel类作为视图模型为我的炫魅,它处理的解析,并公开为视图对象的数组。解析方法:

I am trying to get familiar with Windows Store apps and the MVVM pattern (I'm new to this platform). I have a very simple app that parses a list of Person objects from a json file, then shows them in a gridView. I have created a PeopleViewModel class to serve as the view model for my mainpage, which handles the parsing, and exposes the array of objects for the view. The parsing method:

public async Task init()
{
    StorageFolder resourceFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
    resourceFolder = await resourceFolder.GetFolderAsync("Resources");

    StorageFile resourceFile = await resourceFolder.GetFileAsync("persons.json");
    string fileContent = await Windows.Storage.FileIO.ReadTextAsync(resourceFile);

    ObservableCollection<Person> persons = new ObservableCollection<Person>();

    JsonValue json = JsonValue.Parse(fileContent);
    int personCount = json.GetArray().Count;
    for (int i = 0; i < personCount; i++)
    {
        IJsonValue element = json.GetArray()[i];
        Person p = new Person(element);
        persons.Add(p);
    }

    _persons = persons;
}

然后在我的XAML中,我将这个类作为页面的数据方面:

Then in my XAML, I set this class as the data context of the page:

<!-- viewModel namespace defined above -->
<Page.DataContext>
    <viewModel:PeopleViewModel/>
</Page.DataContext>

由于读取一个文件是一个异步操作,我不能把它放在PeopleViewModel的默认构造函数,我必须调用从$ C $它的init()方法C-背着我的XAML文件:

Since reading a file is an async operation, I can't put it in the default constructor of PeopleViewModel, I have to call its init() method from the code-behind file of my xaml:

private async void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
{
    PeopleViewModel viewModel = this.DataContext as PeopleViewModel;
    await viewModel.init();
}

我的code运作良好,但我想知道,如果这是正确的方式来做到这一点。有没有一种方法来初始化我的视图模型与异步方法,并让我的code隐藏文件干净(或者是这个解决方案算干净)?

My code is working well, but I'm wondering if this is the right way to do it. Is there a way to initialize my viewmodel with an async method, and keep my code-behind file "clean" (or is this solution considered clean)?

推荐答案

当然有,只是从构造函数调用异步无效办法(没有计谋需要),这反过来又会现在请与等待。 (是的,最好的做法是保持codebehind干净。)

Sure there is, just call an async void method from the constructor (no await needed), which can in turn now call methods with await. (And yes, the best practice is to keep the codebehind clean.)

编辑:

因此​​,根据您的经验,我离开了一部分,说你不应该真正做到这一点。相反设置一些​​地方的事件告知您的视图模型加载的东西。基本上你应该只使用异步无效与事件处理程序。更多关于这个在这个页面的底部: http://caraulean.com/blog/2013/07/15/using-caliburn-micro-with-async-await/ (虽然我preFER MVVMlight或棱镜 - 后者甚至给你的 INavigationAware 的界面,你在你的的OnNavigatedTo 活动虚拟机也是如此。)

So based on your experience I left out the part saying that you shouldn't really do this. Instead set up something where an event notifies your viewmodel to load things. Basically you should only use async void with event handlers. More on this at the bottom of this page: http://caraulean.com/blog/2013/07/15/using-caliburn-micro-with-async-await/ (Although I prefer MVVMlight or PRISM - the latter even gives you the INavigationAware interface where you get your OnNavigatedTo events in you VM as well.)

这篇关于MVVM视图模型异步数据初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆