如何不通过MVVM UI模式使用默认构造函数? [英] How not to use the default constructor using the MVVM UI Pattern?

查看:50
本文介绍了如何不通过MVVM UI模式使用默认构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DI ://msdn.microsoft.com/zh-cn/magazine/dd419663.aspx"rel =" nofollow> WPF 应用程序以及

Using DI in a WPF application along with the MVVM UI Architecture Design Pattern.

设置 Window.DataContext 属性时,编译器会抱怨:

When setting the Window.DataContext property, the compiler complains:

类型[(我的视图模型类型")]不包含任何可访问的构造函数.

必须是在我的视图模型类中没有设置默认构造函数.

Which had to be that there was no default constructor set in my view model class.

ProductManagementViewModel

public class ProductManagementViewModel 
    : ViewModel<ObservableCollection<Product>, Product> {
    public ProductManagementViewModel(ObservableCollection<Product> model)
        : base(model) { }

    public Product Current { get; set; }
}

ProductManagementView.xaml

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:Models="clr-namespace:WMI.Courses.DesignPatterns.Mvvm.Models" 
    xmlns:ViewModel="clr-namespace:MyProject.ProductManagement.Management" 
    mc:Ignorable="d" 
    x:Class="MyProject.ProductManagement.Management.ProductManagementView"      
    ResizeMode="NoResize"
    Title="{Binding ViewTitle}"
    Width="800">
    <Window.DataContext>
        <ViewModel:ProductManagementViewModel />
    </Window.DataContext>
    [...]
</Window>

此外,最好使用 构造函数注入 ,因此由于我的视图模型类依赖于 ObservableCollection ,它必须通过构造函数接受它.然后,找到解决该问题的唯一方法是在类中使用默认构造函数.

Besides, it's best to use Constructor Injection, so since my View Model class depends on an ObservableCollection, it has to accept it through the constructor. And then, the only way found to solve the problem was to have a default constructor within the class.

ProductManagementViewModel

public class ProductManagementViewModel 
    : ViewModel<ObservableCollection<Product>> {
    public ProductManagementViewModel() 
        : this(new ObservableCollection<Product>()) { }
    [...]
}

这使我感到有点脏,就像我别无选择.

This makes me feel somehow dirty, and it's like I have no other choice than that.

如何不通过MVVM UI模式使用默认构造函数?

推荐答案

在您提供的示例代码中,您使用的是视图优先方法,该方法将视图模型绑定到XAML中的视图,这使您无法使用在视图模型中具有默认构造函数.获得所需内容的最快方法是简单地在后台代码中设置视图数据上下文,但是由于我认为您正在寻找更干净的解决方案,因此

In the sample code you provided, you are using a view-first approach where the view model is bound to the view in XAML, which is limiting you to having to have a default constructor in your view model. The fastest way to get what you want would be to simply set the view data context in the code-behind, but since I think you are looking for a more clean solution, this article lists a few more.

但是,我建议您考虑使用MVVM框架.它们不仅有助于解决此问题(例如,在Caliburn.Micro视图模型可以使用DI或您喜欢的任何东西独立于视图进行管理,并且基于类名进行连接),而且它们通常提供了更多有用的工具来帮助实现MVVM模式.

However, I would recommend looking into using an MVVM framework. Not only do they help solve this problem (for example in Caliburn.Micro view models can be managed independently of views, using DI or whatever you prefer, and the wiring is done based on class names) but they typically provide many more useful tools to help implement the MVVM pattern.

这篇关于如何不通过MVVM UI模式使用默认构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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