简体   繁体   English

DesignInstance在VS2013中不起作用

[英]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: 环境:

  • VS2013 SP3 VS2013 SP3
  • Net 4.0 净4.0
  • MvvmLight 5.0.2.0 MvvmLight 5.0.2.0

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在这种情况下不起作用?

Workaround 解决方法

  • remove d:DataContext from view 从视图中删除d:DataContext
  • add interface ISearchIdViewModel (it is empty) 添加接口ISearchIdViewModel (它是空的)
  • SearchIdViewModel inherits from ISearchIdViewModel SearchIdViewModel继承自ISearchIdViewModel
  • change 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM