.net 如何将 d:DesignInstance 与没有默认构造函数的类型一起使用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8472228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use d:DesignInstance with types that don't have default constructor?
提问by VitalyB
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 没有默认构造函数,因此我发现“没有默认构造函数”。
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!
谢谢!
回答by Pavlo Glazkov
The default constructor is required for a type to be instantiated in XAML. As a workaround you can simply create a subclass of TaskVMthat will have the default contructor and use it as a design time data context.
要在 XAML 中实例化类型,需要默认构造函数。作为一种解决方法,您可以简单地创建一个TaskVM具有默认构造函数的子类,并将其用作设计时数据上下文。
<TextBlock d:DataContext="{d:DesignInstance ViewModel:DesignTimeTaskVM }"
Text="{Binding Title}" MouseLeftButtonDown="TextBlock_MouseLeftButtonDown">
</TextBlock>
Another alternative is to set d:IsDesignTimeCreatableto Falseand a substitute type will be created for you at runtime (using your TaskVM type as a "shape").
另一种选择是设置d:IsDesignTimeCreatable为False并在运行时为您创建替代类型(使用您的 TaskVM 类型作为“形状”)。
<TextBlock d:DataContext="{d:DesignInstance ViewModel:DesignTimeTaskVM, IsDesignTimeCreatable=False}"
Text="{Binding Title}" MouseLeftButtonDown="TextBlock_MouseLeftButtonDown">
</TextBlock>
回答by Daniel Rose
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 添加默认构造函数。然后它可以检查它是否处于设计时并为其属性设置适当的设计时值。
回答by Martin
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 TypeOfViewModelclass has no parameterless constructor.
So the ViewModelFactory has to pass some value, in this case null.
请注意,TypeOfViewModel该类没有无参数构造函数。所以 ViewModelFactory 必须传递一些值,在这种情况下null。
So in this scenario the TypeOfViewModelclass 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)
{
}
}

