在 Silverlight 3.0 中结合 .NET RIA 服务和 MVVM [英] Combining .NET RIA Services and MVVM in Silverlight 3.0

查看:51
本文介绍了在 Silverlight 3.0 中结合 .NET RIA 服务和 MVVM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Silverlight 3.0 中使用 .NET RIA 服务和 MVVM 时,RIA 服务的元数据类型和 MVVM 模式的 ViewModel 之间有区别吗?这些是同一件事还是应该分开?

When using .NET RIA Services and MVVM in Silverlight 3.0 is there a difference between the Metadata type from RIA Services and the ViewModel from the MVVM pattern? Are these the same thing or should they be keep separate?

元数据类型是部分实体类的密封内部类.那里似乎没有适当的分离,但元数据类型也可以用验证属性装饰,使其看起来像一个 ViewModel.

The metadata type is a sealed internal class to the partial Entity class. There doesn't seem to be a proper separation there but the metadata type can also be decorated with attributes for Validation which makes it look like a ViewModel.

我四处搜索,但没有看到任何关于此的任何详细信息.

I've searched around but I didn't see anything that talks about this in any detail.

推荐答案

同意 ChuckJ - 通常 DomainContext 构成视图模型的一部分.例如,假设我有一个允许对产品目录进行搜索的搜索页面.以下是我的结构:

Agree with ChuckJ - generally the DomainContext forms part of a view model. For example, say I had a search page that allowed searching against a product catalog. Here is how I'd structure things:

在服务器上:

class Catalog : DomainService {
    IQueryable<Product> GetProducts(string keyword) { ... }
}

生成的域上下文:

class Catalog : DomainContext {
    EntityList<Product> Products { get; }
    void LoadProducts(string keyword);
}

我会写的视图模型:

class SearchViewModel {
    Catalog _catalog = new Catalog();

    public IEnumerable<Product> Results {
        get { return _catalog.Products; }
    }

    public void Search(string keyword) {
        _catalog.Products.Clear();
        _catalog.LoadProducts(keyword);
    }
}

最后在我的 xaml 中,我将 UserControl 的 DataContext 设置为 SearchViewModel 的实例,并将 ItemsControl 绑定到 Results 属性.我会使用您选择的 ViewModel 模式将按钮单击绑定到 Search(这实际上是 SearchViewModel 公开的命令).我个人喜欢与 Silverlight.FX 合作的内容,如下所示:

And then finally in my xaml, I'd set my UserControl's DataContext to be an instance of SearchViewModel, and bind an ItemsControl to the Results property. I'd use the ViewModel pattern of your choice to bind a button click to Search (which is effectively a command that SearchViewModel exposes). I personally like something that I have working with Silverlight.FX as in:

<Button Content="Search"
  fxui:Interaction.ClickAction="$model.Search(keywordTextBox.Text)" />

和最初显示的此处.

正如 Chuck 提到的,我的视图模型中可能确实有其他状态,例如,SelectedProduct 可能会双向绑定到我的 xaml 中 ListBox 的 SelectedItem,然后绑定相同的 SelectedProduct 作为 aDataForm 显示所选产品的详细信息.

As Chuck mentions I might indeed have other state in my view model, for example, the SelectedProduct that might be two-way bound to the SelectedItem of a ListBox in my xaml, and then bind the same SelectedProduct as the DataContext of a DataForm to show details of a selected product.

希望有帮助!我很快就会在我的博客上更多地写博客.

Hope that helps! I'll be blogging about this some more on my blog soon.

这篇关于在 Silverlight 3.0 中结合 .NET RIA 服务和 MVVM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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