MVVM、ObservableCollection、异步等 [英] MVVM, ObservableCollection, async, etc

查看:34
本文介绍了MVVM、ObservableCollection、异步等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我进入了我的教育的下一阶段,并且遇到了一些与我使用 SQLite 相关的障碍(这是一个通用应用程序,我目前的重点是在 Windows Phone 方面)解决方案).我的问题在某种程度上独立于 SQLite,但我会以它为例.

So, I'm on to the next phase of my education and have reached a bit of a blocker related to my use of SQLite (this is for a universal app, with my current focus being on the Windows Phone side of that solution). My question is somewhat independent of SQLite but I will use it as an example.

我正在将 SQLite 作为我的应用程序的数据库(基于此处和其他地方的各种建议和评论).具体来说,我正在设计我的视图 -> 视图模型 -> 模型,但我不确定传递 ObservableCollection 的合适模式.

I am looking at SQLite as the database for my app (based on various suggestions and comments here and elsewhere). Specifically, I am designing my view -> viewmodel -> model and I'm not sure of an appropriate pattern for passing around ObservableCollection.

让我从模型开始.我正在调用 SQLite-net 的 QueryAsync() 方法.因此,深埋在模型中的我对 QueryAsync() 的调用有任何等待.其所在的方法(为了简单起见,我们称其为 GetData())用 async 标记.所以,这就是模型.

Let me start at the model. I am making a call to SQLite-net's QueryAsync() method. So, buried deep in the model I have any await on the call to QueryAsync(). The method in which that lives (let's call it GetData(), for simplicity) is marked with async. So, that's the model.

在视图级别,我需要绑定到视图模型的属性.我们将该属性称为 GetDataVM().由于它是一个属性,我使用了 getter - 而且,据我所知,我不能在 getter 中使用 await.这是真的吗?鉴于模型中的异步调用 - QueryAsync() - 看来我需要等待,对吗?

Up at the view level I need to bind to a property of the viewmodel. Let's call that property GetDataVM(). Since it's a property I use a getter - and, as far as I can tell, I can't use await in getters. Is that true? Given the asynchronous call in the model - QueryAsync() - it seems I need an await, right?

确定我在这里有一些基本假设是错误的.但我试图理解的基本原则是,当该属性调用模型中包含异步方法的方法时,我视图中的控件必须绑定到什么.

I am sure I have some basic assumptions wrong here. But the basic principle I am trying to understand is what a control in my view must bind to when that property calls a method in the model that includes an async method.

我觉得这不是特别容易解释 :) 但是,退一步说,我想要的在概念上非常简单.我想要一个控件绑定到一个 viewmodel 属性,该属性反过来从模型中检索数据,该模型从 SQLite 中检索数据.

I'm not finding this particularly easy to explain :) But, stepping right back, what I want is very simple, conceptually. I want a control to bind to a viewmodel property that, in turn, retrieves data from the model, which retrieves data from SQLite.

我很困惑:)

任何帮助将不胜感激(可能从澄清有关我正在努力实现的目标的问题开始:)).

Any help would be most appreciated (probably starting with clarifying questions about what the heck I'm trying to achieve :)).

谢谢.

推荐答案

我有一个 有关此主题的 MSDN 文章.

它的要点是:正如其他人所指出的,读取属性应该是立即操作,而异步操作(一般来说)不是立即操作.

The gist of it is this: as others have noted, a property read should be an immediate operation, whereas an asynchronous operation is (generally speaking) not immediate.

因此,您首先需要做的是决定在加载数据时您的 UI 会是什么样子.当你的虚拟机加载时,它应该在加载"状态下初始化它的数据,当数据到达时,虚拟机应该更新到就绪"状态(或错误"状态,如果操作失败).

So, what you first need to do is decide what your UI will look like while the data is loading. When your VM loads, it should initialize its data in that "loading" state, and when the data arrives, the VM should update to the "ready" state (or to an "error" state if the operation failed).

在我的 MSDN 文章中,我介绍了一种 NotifyTaskCompletion 类型,它几乎只是一个围绕 Task 的数据绑定友好包装器.这允许您通过 XAML 绑定进行状态转换.

In my MSDN article, I introduce a NotifyTaskCompletion<T> type that is pretty much just a data-binding-friendly wrapper around Task<T>. This allows you to do the state transitions via XAML bindings.

这篇关于MVVM、ObservableCollection、异步等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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