How not to use the default constructor using the MVVM UI Pattern?

做~自己de王妃 提交于 2019-12-12 16:51:33

问题


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

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

The type [("my view model type")] does not include any accessible constructors.

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>

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.

How not to use the default constructor using the MVVM UI Pattern?


回答1:


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.

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.



来源:https://stackoverflow.com/questions/28269166/how-not-to-use-the-default-constructor-using-the-mvvm-ui-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!