wpf DesignInstance 在 VS2012 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17484109/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:09:15  来源:igfitidea点击:

DesignInstance not working in VS2012

wpfmvvmvisual-studio-2012blenddesign-time-data

提问by Thomas

I just spent several hours on an issue when using VS2012, WPF 4.5 and design-time data, specifically the DesignInstance attribute.

在使用 VS2012、WPF 4.5 和设计时数据(特别是 DesignInstance 属性)时,我只是在一个问题上花了几个小时。

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.

目标:我希望在我的 WPF 项目(基于 MVVM)中,在 VS2012 和 Blend 中都有设计时数据支持,但我一生都无法使 MVVMLight 方法始终如一地工作。

So I tried to change to "just" using the built-in support for design-time data, using the markup extension provided with Blend.

因此,我尝试使用 Blend 提供的标记扩展,使用对设计时数据的内置支持更改为“仅”。

Problem: Consider the following code:

问题:考虑以下代码:

<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}"
... />

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.

VS2012 和 Blend 都报告“名称 DesignAlertsViewModel 在名称空间 clr-namespace:Nova.ViewModel 中不存在”,即使智能感知解析得很好,并且您已经检查了一千次名称空间和类名称都是正确的。

回答by IntStarFoo

I ran into this error myself in VS2013 then found this question. After searching more, I found an answer that helped me solve it.

我自己在 VS2013 中遇到了这个错误然后发现了这个问题。在搜索更多之后,我找到了一个帮助我解决它的答案。

instead of

代替

d:DataContext="{d:DesignInstance vm:DesignAlertsViewModel, IsDesignTimeCreatable=True}"

use

d:DataContext="{d:DesignInstance d:Type=vm:DesignAlertsViewModel, IsDesignTimeCreatable=True}"

I was using "...d:DesignInstance Type=vm:..." and that displays the same error described above. As soon as I add "d:" everything works.

我正在使用“...d:DesignInstance Type=vm:...”并且显示与上述相同的错误。一旦我添加“d:”一切正常。

Here is the thread I found that helped.

这是我发现有帮助的线程。

https://stackoverflow.com/a/21690404/2892400

https://stackoverflow.com/a/21690404/2892400

回答by al-bex

Check if the "Platform target" in your build configuration is set to "AnyCPU". My was set to "x64", so the designer was complaining about the missing type.

检查构建配置中的“平台目标”是否设置为“ AnyCPU”。我的设置为“ x64”,所以设计师抱怨缺少类型。

VS is running in 32 bit so maybe there is an issue with executing code from 64 bit assemblies.

VS 以 32 位运行,因此从 64 位程序集执行代码可能存在问题。

I found the solution when trying to open my UserControl in "Blend for Visual Studio". It displayed an neat exception to me.

我在“Blend for Visual Studio”中尝试打开我的 UserControl 时找到了解决方案。它向我展示了一个巧妙的例外。

P.S.: After using the accepted answer: Use "d:Type=vm:DesignAlertsViewModel" instead of "vm:DesignAlertsViewModel" my control was visible, but no example data was displayed.

PS:使用接受的答案后:使用“d:Type=vm:DesignAlertsViewModel”而不是“vm:DesignAlertsViewModel”我的控件可见,但没有显示示例数据。

回答by Thomas

Workaround: Make sure that the design-time data class is in the same namespace as the View itself. Once I moved my DesignAlertsViewModel to the View namespace, both the VS2012 and Blend designer immediately started working with the design-time data.

解决方法:确保设计时数据类与视图本身位于相同的命名空间中。一旦我将 DesignAlertsViewModel 移动到 View 命名空间,VS2012 和 Blend 设计器都立即开始使用设计时数据。

Update: This is a workaround for now, until I get to the bottom of the issues. However, in my case, this is the only way to make design-time data work consistently. Of course, if you go down this route you change the class names since - clearly - the DesignAlertsViewModel is no longer a view model. It is now simply a POCO containing design-time data. So perhaps AlertsDesignData would be a better name. I'm not overly happy with the fact my design-time data lives under my View namespace, but it works.

更新:这是目前的解决方法,直到我找到问题的根源。但是,就我而言,这是使设计时数据一致工作的唯一方法。当然,如果你沿着这条路线走,你会改变类名,因为 - 显然 - DesignAlertsViewModel 不再是一个视图模型。它现在只是一个包含设计时数据的 POCO。所以也许 AlertsDesignData 会是一个更好的名字。我对我的设计时数据存在于我的 View 命名空间下这一事实并不太满意,但它确实有效。

I also encourage you to take a look at Laurent's article from MSDN Magazine, April 2013. I like this approach, since it exercises the MVVM pattern a lot more: The design time data service/provider is injected via IOC, so your view models will get "exercised" even at design time.

我还鼓励您阅读Laurent 的文章,该文章来自 MSDN 杂志,2013 年 4 月。我喜欢这种方法,因为它更多地运用了 MVVM 模式:设计时数据服务/提供者是通过 IOC 注入的,因此即使在设计时,您的视图模型也将得到“运用”。

Update 2: After spending yet another 4 hours on this, I think the jury is still out on which approach is the better: Should we go with simple POCO data classes or the MVVMLight approach, using IOC for design-time data services? The former is simple and requires less code, the latter is more true to the MVVM approach, and exercises the real view models to some degree and could possibly reveal some bugs along the way.

更新 2:在又花了 4 个小时之后,我认为陪审团仍然没有确定哪种方法更好:我们应该使用简单的 POCO 数据类还是 MVVMLight 方法,将 IOC 用于设计时数据服务?前者简单,需要的代码更少,后者更贴近MVVM方法,在一定程度上练习了真实的视图模型,可能会在此过程中发现一些错误。

Also, I did have to restart VS a couple of times during my refactoring to the MVVMLight approach - design-time data simply disappeared and came back after a VS restart. However, right now it seems stable enough, and I cannot pinpoint this any further.

此外,在重构为 MVVMLight 方法的过程中,我确实必须重新启动 VS 几次 - 设计时数据只是消失了,并在 VS 重新启动后返回。但是,现在它似乎足够稳定,我无法进一步确定这一点。

回答by Maxence

I've found that DesignInstanceis not working in my .NET 3.5 and .NET 4.0 projects, whereas it works in >= .NET 4.5 project (the projects are the same, they share the same files by using links).

我发现这DesignInstance在我的 .NET 3.5 和 .NET 4.0 项目中不起作用,而在 >= .NET 4.5 项目中有效(项目相同,它们通过使用链接共享相同的文件)。