ReSharper的WPF错误:"无法解析符号QUOT; MyVariable的"由于未知的DataContext"
我在Visual Studio 2012中使用WPF XAML + + MVVM时遇到此错误。
I am experiencing this error when using WPF + XAML + MVVM in Visual Studio 2012.
无法解析符号MyVariable的因不明的DataContext
Cannot resolve symbol "MyVariable" due to unknown DataContext
解决方案是什么?
由ReSharper的XAML设计为WPF时所产生这个错误,并表示该XAML找不到包含运行时绑定的类。这通常表明的DataContext
未正确设置。
This error is produced by ReSharper when designing XAML for WPF, and indicates that the XAML cannot find the class that contains run-time bindings. This usually indicates that the DataContext
is not set properly.
此错误意味着:
- 智能感知为XAML并不在设计时以及工作;
- 一个不能自动从XAML到C#类在设计时使用Ctrl键,点击
结合
在XAML浏览; - 当我们选择一个属性查找使用实例,也不会带来了在XAML惯例,以及在C#;
- 设计者不能不能显示从自定义的C#类的实时数据。
- Intellisense for XAML does not work as well at design time;
- One cannot auto navigate from the XAML to the C# class at design time using a Ctrl-Click on the
binding
in the XAML; - When we select "Find Usages" on a property, it will not bring up the usages in the XAML as well as the C#;
- The designer cannot not show live data from a custom C# class.
有关我们这些想MVVM,这个错误表示视图找不到视图模型。
For those of us that think in MVVM, this error indicates that the View cannot find the ViewModel.
通过某种网络教程去了解数据绑定是如何工作的。推荐微软数据绑定概述。
Go through some sort of web tutorial to understand how DataBinding works. Recommend Microsoft Data Binding Overview.
如果使用ReSharper的,pressing对违规DataContext的Alt-Enter组合会弹出一个菜单,可帮助您插入正确的DataContext到您的XAML。
If using ReSharper, pressing Alt-Enter on the offending DataContext will bring up a menu that helps you to insert the correct DataContext into your XAML.
我用这个来正确地解决问题。
I used this to correctly resolve the issue.
在Visual Studio中的属性面板中,您可以选择所选择的控制数据上下文:
In the "Properties" pane of Visual Studio, you can select the data context for the selected control:
掺合物也可使用设置数据上下文。在Blend中打开你的.sln文件,选择设计元素,然后在属性中选择新建:
Blend can also be used to set the data context. Open up your .sln file in Blend, select the design element, then in the properties select "New":
DevEx preSS也可以帮助解决在XAML为你这个错误,使用它的向导。
DevExpress can also help to resolve this error in the XAML for you, using its wizard.
在XAML中,选择父元素要设置(通常是整个窗体)的数据上下文,然后在设计师选择操作三角形。
In the XAML, select the parent element you want to set the data context for (usually the entire form), then in the designer select the action triangle.
然后,浏览与C#code类。
Then, browse to the class with the C# code.
的提示:除非你添加一个参数的构造函数的类的类是不可见的
<UserControl x:Class="DemoAllocation.MyActualView"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
后XAML
<UserControl
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:Implementation="clr-namespace:DemoAllocation.ViewModel.Implementation" x:Class="DemoAllocation.MyActualView"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.DataContext>
<Implementation:MyActualViewModel/>
</UserControl.DataContext>
提示6
如果你不能看到在WPF设计的智能标签,确认他们没有在某个时候关闭:
Hint 6
If you cannot see the Smart Tags on the WPF designer, check that they have not been switched off at some point:
一个可以添加调用code对弹出启动一个片段每次一个消息框,有是有约束力的错误。这已经被证明是非常有用的。
One can add call a snippet of code on startup that pops up a message box every time there is a binding error. This has turned out to be very useful.
在情况下,上述的网络链路出现故障,这里是code:
In case the aforementioned web link goes down, here is the code:
public partial class Window1 : Window
{
public Window1()
{
BindingErrorTraceListener.SetTrace();
InitializeComponent();
}
}
方法:
using System.Diagnostics;
using System.Text;
using System.Windows;
namespace SOTC_BindingErrorTracer
{
public class BindingErrorTraceListener : DefaultTraceListener
{
private static BindingErrorTraceListener _Listener;
public static void SetTrace()
{ SetTrace(SourceLevels.Error, TraceOptions.None); }
public static void SetTrace(SourceLevels level, TraceOptions options)
{
if (_Listener == null)
{
_Listener = new BindingErrorTraceListener();
PresentationTraceSources.DataBindingSource.Listeners.Add(_Listener);
}
_Listener.TraceOutputOptions = options;
PresentationTraceSources.DataBindingSource.Switch.Level = level;
}
public static void CloseTrace()
{
if (_Listener == null)
{ return; }
_Listener.Flush();
_Listener.Close();
PresentationTraceSources.DataBindingSource.Listeners.Remove(_Listener);
_Listener = null;
}
private StringBuilder _Message = new StringBuilder();
private BindingErrorTraceListener()
{ }
public override void Write(string message)
{ _Message.Append(message); }
public override void WriteLine(string message)
{
_Message.Append(message);
var final = _Message.ToString();
_Message.Length = 0;
MessageBox.Show(final, "Binding Error", MessageBoxButton.OK,
MessageBoxImage.Error);
}
}
}
解决方案8
使用免费的实用工具探听。
有一个非常好的功能,可以通过使用绑定错误的控件进行过滤。这使您可以直接导航到视觉与绑定错误。
There is a really nice feature that allows you to filter by controls with binding errors. This allows you to navigate straight to the visual with the binding error.
开始探听之后:
- 单击并拖动第二目标图标在你运行应用程序。
- 按住Ctrl + Shift键。
- 当你将鼠标移到正在运行的应用程序,无论是控制鼠标下将被用红色标出。
- 松开鼠标,和Snoop会弹出一个窗口,显示在视觉树中的所有XAML的。
有实际上是两个完全独立的DataContexts:设计时间
和运行时间
There are actually two completely separate DataContexts: design time
and run time
.
大部分的previous解决方案都集中在设置运行时间
的DataContext。
Most of the previous solutions are focused on setting the run time
DataContext.
一旦您设置了设计时间
的DataContext,在Visual Studio或者Blend中的XAML preVIEW将表明您的自定义的C#类提供的自定义数据。
Once you set the design time
DataContext, the XAML preview in Visual Studio or Blend will show custom data provided by your custom C# class.
如果使用混合,这种自定义数据也可以从一个XML文件中读取,但我preFER从我自己的C#类提供。
If using Blend, this custom data can also be read from an XML file, but I prefer to provide it from my own C# class.
要设置设计时间
的DataContext,请参见:
To set the design time
DataContext, see:
- Adam Prescott: Design-Time Data Binding in WPF
- See UI changes in design view with WPF & XAML and data binding?
或者,将它添加到任何元素(这将新的了类 MyClass的
在设计时,让智能感知将工作):
Or, add this to any element (this will new up the class MyClass
at design time, so Intellisense will work):
d:DataContext="{d:DesignInstance d:Type=viewModel:MyClass, IsDesignTimeCreatable=True}"
和这头:
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
在幕后,当您设置设计时间
的DataContext:
Behind the scenes, when you set the design time
DataContext:
- Visual Studio设计(或混合)将自动实例化你点它在类的新实例。如果您创建一个静态类,这也适用。
- 然后,在XAML preVIEW,因为你正在编辑的XAML,它会从你的C#类显示实时数据。
- 这使得设计的真快,你可以在设计时的实时数据工作,你没有运行程序所有的时间来看看它的外观。
请注意,该XAML preVIEW如果您使用的是用户控件才会出现。如果preFER使用的DataTemplates,没问题:你可以创建一个临时用户控制,包括DataTemplate中,并设置设计时间
的DataContext在静态类点。 code最大静态类,以便它创建您的视图模型的一个新实例(即要绑定到类)。例如,你的静态类可以从数据库中读取数据,填充视图模型的属性,你可以使用实时数据从XAML设计时的数据库工作。
Note that the XAML preview only appears if you are using a User Control. If you prefer to use DataTemplates, no problem: you can create a temporary User Control that includes the DataTemplate, and set the design time
DataContext to point at a static class. Code up the static class so that it creates a new instance of your ViewModel (i.e. the class you want to bind to). For example, your static class could read data from a database, populate the properties of the ViewModel, and you could work with live data from the database at XAML design time.
此技术也适用非常顺利地使用依赖注入,比如统一或MEF。你必须要在其中抓住适当的类了依赖注入容器,并完成所有设置一个静态类指向你的设计时间
的DataContext。然后,您可以看到在XAML preVIEW设计时的实时数据。上述链接演示如何工作(完成与XAML设计时现场滴答作响的时钟的YouTube视频!)。
This technique also works perfectly well with Dependency Injection, such as Unity or MEF. You have to point your design time
DataContext at a static class which grabs the appropriate classes out of the dependency injection container, and sets everything up. You can then see live data at design time in the XAML preview. The aforementioned links demo how this works (complete with YouTube videos of a live ticking clock at XAML design time!).
不用说,这种技术的工作原理非常顺利地使用MVVM模式,同时还与MVVM +依赖注入。对于那些你不熟悉MVVM,它的一个伟大的方式来产生优雅,清洁,维护和易于ALTER项目。微软混合本身使用MVVM模式完全写。
Needless to say, this technique works perfectly well with the MVVM pattern, and also with MVVM + Dependency Injection. For those of you unfamiliar with MVVM, its a great way to produce elegant, clean, maintainable and easy-to-alter projects. Microsoft Blend itself is entirely written using the MVVM pattern.