[英]DesignInstance does not work in VS2013
Visual Studio does not show design time data with DesignInstance attribute. Visual Studio不显示具有DesignInstance属性的设计时数据。 I have checked
DesignInstance
with/without MVVM Light. 我已经检查过带有/不带MVVM Light的
DesignInstance
。 I have spend a lot of time to fix the issue (checked similar queestions on StackOverflow too) but DesignInstance
simply does not work. 我花了很多时间来解决这个问题(也检查了StackOverflow上类似的问题),但
DesignInstance
根本不起作用。
Project: 项目:
SearchIdView
. SearchIdView
。 SearchIdViewModel
- real View Model. SearchIdViewModel
- 真实视图模型。 DesignSearchIdViewModel
- inherits from SearchIdViewModel
and contains design time data (properties are assigned in constructor). DesignSearchIdViewModel
- 继承自SearchIdViewModel
并包含设计时数据(属性在构造函数中分配)。 Environment: 环境:
SearchIdView.xaml SearchIdView.xaml
<Window x:Class="App1.View.SearchIdView"
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:ignore="http://www.ignore.com"
xmlns:design="clr-namespace:App1.Design"
mc:Ignorable="d ignore"
DataContext="{Binding SearchId, Source={StaticResource Locator}}"
d:DataContext="{d:DesignInstance d:Type=design:DesignSearchIdViewModel,IsDesignTimeCreatable=True}"
>
<Grid>
<TextBlock Text="{Binding Test}" />
</Grid>
SearchIdViewModel.cs SearchIdViewModel.cs
Property from SearchIdViewModel
来自
SearchIdViewModel
属性
public const string TestPropertyName = "Test";
private string _test;
public string Test
{
get
{
return _test;
}
set
{
Set(TestPropertyName, ref _test, value);
}
}
Do you have any idea why DesignInstance
does not work in this case? 您是否知道为什么
DesignInstance
在这种情况下不起作用?
d:DataContext
from view d:DataContext
ISearchIdViewModel
(it is empty) ISearchIdViewModel
(它是空的) SearchIdViewModel
inherits from ISearchIdViewModel
SearchIdViewModel
继承自ISearchIdViewModel
ViewModelLocator
(below) ViewModelLocator
(下面) ViewModelLocator.cs ViewModelLocator.cs
public class ViewModelLocator
{
static ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
if (ViewModelBase.IsInDesignModeStatic)
{
SimpleIoc.Default.Register<ISearchIdViewModel,Design.DesignSearchIdViewModel>();
}
else
{
SimpleIoc.Default.Register<ISearchIdViewModel, SearchIdViewModel>();
}
}
public SearchIdViewModel SearchId
{
get { return (SearchIdViewModel) ServiceLocator.Current.GetInstance<ISearchIdViewModel>(); }
}
}
Your d:DesignInstance
declaration is malformed. 你的
d:DesignInstance
声明格式不正确。 You specify the property name d:Type
instead of Type
, so the property is not assigned correctly. 您指定属性名称
d:Type
而不是Type
,因此未正确分配属性。 Either replace d:Type
with Type
, or leave the property name off entirely and let it be inferred as the default property. 将
d:Type
替换为Type
,或者完全保留属性名称,并将其推断为默认属性。
d:DataContext="{d:DesignInstance d:Type=design:DesignSearchIdViewModel,
IsDesignTimeCreatable=True}"
Should become: 应该成为:
d:DataContext="{d:DesignInstance Type=design:DesignSearchIdViewModel,
IsDesignTimeCreatable=True}"
Or, alternatively: 或者,或者:
d:DataContext="{d:DesignInstance design:DesignSearchIdViewModel,
IsDesignTimeCreatable=True}"
(line wrapping added for readability) (为了便于阅读,添加了换行)
Another cause that might make d:DesignInstance not to work is that all data must be properties not just public variables of mock class! 可能使d:DesignInstance无法工作的另一个原因是所有数据必须是属性,而不仅仅是mock类的公共变量! I know that it was not your problem, but it should be checked if for someone it does not work.
我知道这不是你的问题,但是应该检查一下它是否不起作用。
Will not work with: 不适用于:
public class MockFile
{
public FilePRJO FilePRJO = new FilePRJO();
}
But it will work with: 但它适用于:
public class MockFile
{
public FilePRJO _filePRJO = new FilePRJO();
public FilePRJO FilePRJO
{
get
{
return _filePRJO;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.