我刚刚花了几个小时来解决使用 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:”一切就正常了。
这是我发现有帮助的线程。
关于wpf - DesignInstance 在 VS2012 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17484109/