Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何不使用使用MVVMUI模式的默认构造函数?_C#_.net_Wpf_Mvvm_Dependency Injection - Fatal编程技术网

C# 如何不使用使用MVVMUI模式的默认构造函数?

C# 如何不使用使用MVVMUI模式的默认构造函数?,c#,.net,wpf,mvvm,dependency-injection,C#,.net,Wpf,Mvvm,Dependency Injection,与UI架构设计模式一起在应用程序中使用 设置Window.DataContext属性时,编译器会抱怨: 类型[(“我的视图模型类型”)]不包括任何可访问的构造函数 这必须是在我的视图模型类中没有设置默认构造函数 ProductManagementViewModel public class ProductManagementViewModel : ViewModel<ObservableCollection<Product>, Product> { pu

与UI架构设计模式一起在应用程序中使用

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

类型[(“我的视图模型类型”)]不包括任何可访问的构造函数

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

ProductManagementViewModel

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

    public Product Current { get; set; }
}
<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>
public class ProductManagementViewModel 
    : ViewModel<ObservableCollection<Product>> {
    public ProductManagementViewModel() 
        : this(new ObservableCollection<Product>()) { }
    [...]
}
公共类ProductManagementViewModel
:视图模型{
公共产品管理视图模型(ObservableCollection模型)
:base(model){}
公共产品当前{get;set;}
}
ProductManagementView.xaml

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

    public Product Current { get; set; }
}
<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>
public class ProductManagementViewModel 
    : ViewModel<ObservableCollection<Product>> {
    public ProductManagementViewModel() 
        : this(new ObservableCollection<Product>()) { }
    [...]
}

[...]
此外,最好使用它,因为我的视图模型类依赖于,它必须通过构造函数接受它。然后,解决这个问题的唯一方法就是在类中有一个默认构造函数

ProductManagementViewModel

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

    public Product Current { get; set; }
}
<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>
public class ProductManagementViewModel 
    : ViewModel<ObservableCollection<Product>> {
    public ProductManagementViewModel() 
        : this(new ObservableCollection<Product>()) { }
    [...]
}
公共类ProductManagementViewModel
:视图模型{
公共产品管理视图模型()
:此(新的ObservableCollection()){}
[...]
}
这让我觉得有点脏,就好像我别无选择


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

在您提供的示例代码中,您使用的是视图优先方法,其中视图模型绑定到XAML中的视图,这限制了您必须在视图模型中使用默认构造函数。获得所需内容的最快方法是在代码中简单地设置视图数据上下文,但由于我认为您正在寻找一个更干净的解决方案,因此列出了更多的解决方案


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

链接文章中提供的列表确实有帮助。我最终使用了选项2,以及2和4中的一部分。