问题描述
在使用VS2012,WPF 4.5和设计时数据(特别是DesignInstance属性)时,我在一个问题上花了几个小时.
I just spent several hours on an issue when using VS2012, WPF 4.5 and design-time data, specifically the DesignInstance attribute.
目标:我想在VS2012和Blend中的WPF项目(基于MVVM)中提供设计时数据支持,而我一生都无法使用MVVMLight方法一致地工作.
Goal: I wanted to have design-time data support in my WPF project (MVVM-based), both in VS2012 and Blend, and I could not for the life of me make the MVVMLight approach work consistently.
因此,我尝试使用Blend随附的标记扩展名,使用对设计时数据的内置支持,将其更改为"just".
So I tried to change to "just" using the built-in support for design-time data, using the markup extension provided with Blend.
问题:考虑以下代码:
<Window x:Class="Nova.View.AlertsView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Nova.View"
xmlns:vm="clr-namespace:Nova.ViewModel"
mc:Ignorable="d"
DataContext="{Binding Alerts, Source={StaticResource Locator}}"
d:DataContext="{d:DesignInstance vm:DesignAlertsViewModel, IsDesignTimeCreatable=True}"
... />
VS2012和Blend两者都报告名称DesignAlertsViewModel在名称空间clr-namespace:Nova.ViewModel中不存在",即使intellisense可以很好地解决它,并且您已经检查了数千次,名称空间和类名称都是正确.
Both VS2012 and Blend reports "the name DesignAlertsViewModel does not exist in the namespace clr-namespace:Nova.ViewModel", even though intellisense resolves it just fine, and you have checked a thousand times that the namespace and class name are both correct.
推荐答案
我自己在VS2013中遇到了此错误,然后发现了这个问题.搜索更多之后,我找到了一个可以解决问题的答案.
I ran into this error myself in VS2013 then found this question. After searching more, I found an answer that helped me solve it.
代替
d:DataContext="{d:DesignInstance vm:DesignAlertsViewModel, IsDesignTimeCreatable=True}"
使用
d:DataContext="{d:DesignInstance d:Type=vm:DesignAlertsViewModel, IsDesignTimeCreatable=True}"
我正在使用"... d:DesignInstance Type = vm:...",并且显示了与上述相同的错误.一旦我添加"d:",一切都会正常.
I was using "...d:DesignInstance Type=vm:..." and that displays the same error described above. As soon as I add "d:" everything works.
这是我发现有帮助的线程.
Here is the thread I found that helped.
https://stackoverflow.com/a/21690404/2892400
这篇关于DesignInstance在VS2012中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!