wpf - DesignInstance 在 VS2012 中不起作用

标签 wpf mvvm visual-studio-2012 blend design-time-data

我刚刚花了几个小时来解决使用 VS2012、WPF 4.5 和设计时数据(特别是 DesignInstance 属性)时出现的问题。

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

因此,我尝试使用 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 都报告“命名空间 clr-namespace:Nova.ViewModel 中不存在名称 DesignAlertsViewModel”,尽管智能感知可以很好地解析它,并且您已经检查了一千次,命名空间和类名称都是正确。

最佳答案

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

而不是

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

使用

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

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

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

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

关于wpf - DesignInstance 在 VS2012 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17484109/

相关文章:

wpf - 使用 WPF 绑定(bind)传递两个命令参数

java - @AndroidEntryPoint 基类必须扩展 ComponentActivity,(支持)Fragment、View、Service 或 BroadcastReceiver

visual-studio-2012 - 排除 NuGet 中某个类型的所有文件

c# - Windows 8 商店应用程序和 WCF 服务

css - 如何在 Web Essentials LESS 编译中忽略某些 LESS 文件? ( Bootstrap )

c# - 从按钮打开窗口而不创建新的 windowFoo 实例

c# - 更改绑定(bind)属性时,ListView 分组不会更新

c# - 将列表框绑定(bind)到 WPF 中字符串类型的 Observable 集合

Android 数据绑定(bind)/MVVM 模式 : computing view color based off Enum

c# - 在 WPF 双向绑定(bind)中,如何检查触发绑定(bind)更改的是 UI 元素还是 ViewModel?