PathGeneratedInternally标志在WPF绑定中做什么?
我刚才回答了一个问题,我说两者之间没有功能上的区别PathGeneratedInternally标志在WPF绑定中做什么?,wpf,binding,Wpf,Binding,我刚才回答了一个问题,我说两者之间没有功能上的区别 {Binding TargetProperty} 及 而且,据我所知,我写的基本上是正确的。然而,一个使用构造函数,另一个设置属性的想法让我想到可能会有不同,所以我打开了reflector,看了看 构造函数中包含以下代码: public Binding(string path) { this._source = UnsetSource; if (path != null) { if (Dispatch
{Binding TargetProperty}
及
而且,据我所知,我写的基本上是正确的。然而,一个使用构造函数,另一个设置属性的想法让我想到可能会有不同,所以我打开了reflector,看了看
构造函数中包含以下代码:
public Binding(string path)
{
this._source = UnsetSource;
if (path != null)
{
if (Dispatcher.CurrentDispatcher == null)
{
throw new InvalidOperationException();
}
this._ppath = new PropertyPath(path, new object[0]);
this._attachedPropertiesInPath = -1;
}
}
路径属性如下所示:
public PropertyPath Path
{
get
{
return this._ppath;
}
set
{
base.CheckSealed();
this._ppath = value;
this._attachedPropertiesInPath = -1;
base.ClearFlag(BindingBase.BindingFlags.PathGeneratedInternally);
}
}
因此,通过属性设置路径时,PathGeneratedInternally标志将被清除。现在,这个标志没有直接公开在任何地方,但它似乎在一些地方使用:
internal void UsePath(PropertyPath path)
{
this._ppath = path;
base.SetFlag(BindingBase.BindingFlags.PathGeneratedInternally);
}
[EditorBrowsable(EditorBrowsableState.Never)]
public bool ShouldSerializePath()
{
return ((this._ppath != null) && !base.TestFlag(BindingBase.BindingFlags.PathGeneratedInternally));
}
我相信这一切都是无关紧要的,但是有没有人知道这个标志的含义,以及为什么它可能会根据您声明绑定的方式而有所不同?关键是要查看UsePath方法的引用位置。默认情况下不会设置该标志,因此清除该标志基本上是不可操作的。没有理由在构造函数中清除它,因为您知道在这种情况下没有设置它(因为对象仍在构造中) UsePath方法只在一个位置调用,该位置是ClrBindingWorker构造函数。如果你在那里看,你会看到他们自动创建一个“空白”或“空”路径,并将其传递给UsePath
我怀疑他们这样做是因为路径在内部使用时是“有效的”,即使它只是引用绑定源(这是没有给出路径时的默认行为)。如果以后在绑定上设置Path属性,则必须清除指示自动生成路径的标志。那么是否存在函数差异?
internal void UsePath(PropertyPath path)
{
this._ppath = path;
base.SetFlag(BindingBase.BindingFlags.PathGeneratedInternally);
}
[EditorBrowsable(EditorBrowsableState.Never)]
public bool ShouldSerializePath()
{
return ((this._ppath != null) && !base.TestFlag(BindingBase.BindingFlags.PathGeneratedInternally));
}