将视图模型连接到其视图的首选方式是什么?

发布于 2024-10-26 19:20:14 字数 208 浏览 0 评论 0原文

过去,在使用 MVVM 时,我将每个视图创建为 DataTemplate 到其相应的视图模型来处理连接它们。我刚刚开始使用 MVVM Light,并注意到他们有 ViewModelLocator。我看过的其他几个工具包包含了一些这种变体,但是这与使用 DataTemplate 相比有什么好处呢?

连接视图和视图模型的最佳实践是什么?

In the past while working with MVVM I've created every View as a DataTemplate to it's corresponding viewmodel to handle connecting them. I just started using MVVM Light, and noticed they have the ViewModelLocator. Several other toolkits I've looked at include some variation of this, but what benefit does this bring over using DataTemplate?

Which is the best practice for connecting your Views and ViewModels?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄意 2024-11-02 19:20:14

有两种不同的方法,而不是一种“正确的方法”。

ViewModelLocator 或类似方法所帮助的方法是开发 MVVM 的“视图优先”方法。这意味着您从设计器中的视图开始,然后构建与之匹配的视图模型。从逻辑上讲,视图通常会创建其他视图,并且 ViewModel 通常通过某种形式的定位器加载并为给定视图填充。消息传递或服务用于将适当的模型挂接到新生成的 ViewModel 中。

这样做的优点是更容易进行视觉设计,尤其是在使用 Blend 时。

另一种方法是“ViewModel-First”。这样,您就生成了 ViewModel,然后使用 DataTemplates 来填充视图。 ViewModels 将组合/创建其他 VM,直接设置适当的 Model。从程序员的角度来看,这通常(IMO)要干净得多,因为事情可以直接工作。然而,从设计者的角度来看,它通常更难以设计和使用,因为设计时数据更难以生成等。

这两种方法都完全有效,并且具有很大的优点和缺点。出于各种原因,不同的 MVVM 纯粹主义者倾向于选择一种方法而不是另一种方法 - 通常是从代码整洁的角度还是从设计人员的角度来实现这一点。

There are two different approaches, and not one "right way".

The approach that a ViewModelLocator or similar helps with is a "View-First" approach to developing MVVM. By this, it means you start with your View in the designer, and then build the ViewModel to match. Logically, Views often create other Views, and the ViewModel is typically loaded via some form of locator and populated for a given View. Messaging or services are used to hook appropriate models into the newly generated ViewModels.

This has the advantage of being a bit easier to design visually, especially when working with Blend.

The other approach is to work "ViewModel-First". By this, you generate your ViewModels, and then use DataTemplates to have the View populate. ViewModels will compose/create other VMs, directly setting the appropriate Model. This is typically (IMO) much, much cleaner from a programmer's perspective, as things just work directly. However, it's typically more difficult to design and work with from a designer's point of view, as design-time data is more difficult to generate, etc.

Both approaches are perfectly valid, and have strong advantages and disadvantages. Different MVVM purists tend to prefer one approach over the other for various reasons - typically whether they're approaching this from a code cleanliness point of view or a designer point of view.

月寒剑心 2024-11-02 19:20:14

我的看法是:

  1. 你有一个适合所有 ViewModel 的好地方。
  2. MVVMLight 提供了一个很好的清理机制。
  3. 您可以在标记中静态连接 ViewModel。 --- 如果您没有意识到 ViewModel 在创建 UI 实例后立即被实例化并设置为 DataContext,这有时可能会出现问题。

对于(3)(如何使用 ViewModelLocator):

  1. 确保您已安装代码片段。
  2. 打开 ViewModelLocator.cs 并输入 mvvmlocatorproperty。在智能感知中选择它并双击 TAB 以使代码片段起作用。将其更改为适当的属性。
  3. 在您的 xaml 中,您将像这样使用它:

    ;
        
    
    

对于此属性:

    public HomePageViewModel HomePage
    {
        get
        {
            return HomePageStatic;
        }
    }

The way I see it:

  1. You have one nice place for all the ViewModels.
  2. There is a nice cleanup mechanism MVVMLight Provides.
  3. You can connect your ViewModels in your markup statically. --- This could sometimes be a problem if you don't realize that your ViewModel is instantiated and set to the DataContext as soon as you create an instance of the UI.

For (3) (How to use the ViewModelLocator) :

  1. Make sure you have the snippets installed.
  2. Open ViewModelLocator.cs and type mvvmlocatorproperty. Select it in the intellisense and double TAB for the snippet to work. Change it to the appropriate property.
  3. In your xaml you will use it like this:

    <Window.DataContext>
        <Binding Path="HomePage" Source="{StaticResource Locator}"/>
    </Window.DataContext>
    

For this property:

    public HomePageViewModel HomePage
    {
        get
        {
            return HomePageStatic;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文