如何使用没有默认构造函数的类型的设计实例?

时间:2022-08-27 16:10:16

I am binding a textbox to an object, like so:

我将一个文本框绑定到一个对象,比如:

  <TextBlock d:DataContext="{d:DesignInstance ViewModel:TaskVM }" 
             Text="{Binding Title}" MouseLeftButtonDown="TextBlock_MouseLeftButtonDown">
  </TextBlock>

Now I am wondering how to make it display mock data during design. I've tried doing something like that:

现在我想知道如何让它在设计期间显示模拟数据。我试过这样做:

  <TextBlock Text="{Binding Path=Title}" MouseLeftButtonDown="TextBlock_MouseLeftButtonDown">
    <d:DesignProperties.DataContext>
       <ViewModel:TaskVM Title="Mock"/>
    </d:DesignProperties.DataContext>
  </TextBlock>

However, since TaskVM has no default ctor, I am getting a "No default constructor" found.

但是,由于TaskVM没有默认的ctor,所以我得到了一个“没有默认构造函数”。

I know that when I use d:DataContext="{d:DesignInstance ViewModel:TaskVM }" it creates a mock data type. Is there a way for me to set the properties of this mock type?

我知道,当我使用d:DataContext="{d:DesignInstance ViewModel:TaskVM}"时,它会创建一个模拟数据类型。是否有办法设置这个模拟类型的属性?

Thanks!

谢谢!

3 个解决方案

#1


37  

The default constructor is required for a type to be instantiated in XAML. As a workaround you can simply create a subclass of TaskVM that will have the default contructor and use it as a design time data context.

在XAML中实例化的类型需要默认构造函数。作为一个解决方案,您可以简单地创建一个TaskVM的子类,它将拥有默认的contructor,并将其用作设计时数据上下文。

<TextBlock d:DataContext="{d:DesignInstance ViewModel:DesignTimeTaskVM }" 
           Text="{Binding Title}" MouseLeftButtonDown="TextBlock_MouseLeftButtonDown">
</TextBlock>

Another alternative is to set d:IsDesignTimeCreatable to False and a substitute type will be created for you at runtime (using your TaskVM type as a "shape").

另一种方法是设置d:IsDesignTimeCreatable为False,并在运行时为您创建一个替代类型(使用TaskVM类型为“shape”)。

<TextBlock d:DataContext="{d:DesignInstance ViewModel:DesignTimeTaskVM, IsDesignTimeCreatable=False}" 
           Text="{Binding Title}" MouseLeftButtonDown="TextBlock_MouseLeftButtonDown">
</TextBlock>

#2


3  

You could add a default constructor to your VM. It could then check if it is in design time and set appropriate design-time values for its properties.

您可以向VM添加默认构造函数。然后,它可以检查它是否在设计时,并为其属性设置适当的设计时值。

#3


2  

Another alternative would be to use a static class to hold the view model and call that class from the XAML. Here is an example:

另一种方法是使用静态类来保存视图模型,并从XAML调用该类。这是一个例子:

The xaml uses a view model factory to create the design data context:

xaml使用视图模型工厂来创建设计数据上下文:

xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DataContext="{x:Static local:ViewModelFactory.ViewModel}"


The static ViewModelFactory constructs the view model in its constructor and stores it in a public property where it can be accessed from outside (from the XAML):

静态ViewModelFactory在其构造函数中构造视图模型,并将其存储在可以从外部访问的公共属性中(从XAML):

public static class ViewModelFactory
{
    /// <summary>
    /// Static constructor.
    /// </summary>
    static ViewModelFactory()
    {
        ViewModel = new TypeOfViewModel(null);

        // further configuration of ViewModel
    }

    public static TypeOfViewModel ViewModel
    {
        get; set;
    }
}



Please note that the TypeOfViewModel class has no parameterless constructor. So the ViewModelFactory has to pass some value, in this case null.

请注意,TypeOfViewModel类没有无参数的构造函数。ViewModelFactory必须传递一些值,在这个例子中是null。

So in this scenario the TypeOfViewModel class would need to be implemented in a way that it is aware that during design time the passed in dependency is null.

所以在这种情况下,TypeOfViewModel类需要以一种方式来实现,它知道在设计时,依赖项的传递是空的。

public class TypeOfViewModel
{
    /// <summary>
    /// Constructor.
    /// </summary>
    /// <param name="dependency">May be null at design time</param>
    public TypeOfViewModel(SomeDependentClass dependency)
    {

    }
}

#1


37  

The default constructor is required for a type to be instantiated in XAML. As a workaround you can simply create a subclass of TaskVM that will have the default contructor and use it as a design time data context.

在XAML中实例化的类型需要默认构造函数。作为一个解决方案,您可以简单地创建一个TaskVM的子类,它将拥有默认的contructor,并将其用作设计时数据上下文。

<TextBlock d:DataContext="{d:DesignInstance ViewModel:DesignTimeTaskVM }" 
           Text="{Binding Title}" MouseLeftButtonDown="TextBlock_MouseLeftButtonDown">
</TextBlock>

Another alternative is to set d:IsDesignTimeCreatable to False and a substitute type will be created for you at runtime (using your TaskVM type as a "shape").

另一种方法是设置d:IsDesignTimeCreatable为False,并在运行时为您创建一个替代类型(使用TaskVM类型为“shape”)。

<TextBlock d:DataContext="{d:DesignInstance ViewModel:DesignTimeTaskVM, IsDesignTimeCreatable=False}" 
           Text="{Binding Title}" MouseLeftButtonDown="TextBlock_MouseLeftButtonDown">
</TextBlock>

#2


3  

You could add a default constructor to your VM. It could then check if it is in design time and set appropriate design-time values for its properties.

您可以向VM添加默认构造函数。然后,它可以检查它是否在设计时,并为其属性设置适当的设计时值。

#3


2  

Another alternative would be to use a static class to hold the view model and call that class from the XAML. Here is an example:

另一种方法是使用静态类来保存视图模型,并从XAML调用该类。这是一个例子:

The xaml uses a view model factory to create the design data context:

xaml使用视图模型工厂来创建设计数据上下文:

xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DataContext="{x:Static local:ViewModelFactory.ViewModel}"


The static ViewModelFactory constructs the view model in its constructor and stores it in a public property where it can be accessed from outside (from the XAML):

静态ViewModelFactory在其构造函数中构造视图模型,并将其存储在可以从外部访问的公共属性中(从XAML):

public static class ViewModelFactory
{
    /// <summary>
    /// Static constructor.
    /// </summary>
    static ViewModelFactory()
    {
        ViewModel = new TypeOfViewModel(null);

        // further configuration of ViewModel
    }

    public static TypeOfViewModel ViewModel
    {
        get; set;
    }
}



Please note that the TypeOfViewModel class has no parameterless constructor. So the ViewModelFactory has to pass some value, in this case null.

请注意,TypeOfViewModel类没有无参数的构造函数。ViewModelFactory必须传递一些值,在这个例子中是null。

So in this scenario the TypeOfViewModel class would need to be implemented in a way that it is aware that during design time the passed in dependency is null.

所以在这种情况下,TypeOfViewModel类需要以一种方式来实现,它知道在设计时,依赖项的传递是空的。

public class TypeOfViewModel
{
    /// <summary>
    /// Constructor.
    /// </summary>
    /// <param name="dependency">May be null at design time</param>
    public TypeOfViewModel(SomeDependentClass dependency)
    {

    }
}