我一直在我的应用程序的多个版本中使用相同的代码,没有任何问题,但我现在神秘地收到NullRerefenceException
具有以下内容:
this.Loaded += delegate {
deleteBrush = new DeleteBrushAdorner( background );
AdornerLayer al = AdornerLayer.GetAdornerLayer( background );
al.Add( deleteBrush ); // null ref here??
};
background
只是一个Border
元素。
我对可能导致它的原因的两个想法是a)切换到.NET 4.0,b)放置上述元素的实例(这是一个UserControl
) in an ItemsControl
.
奇怪的是,这种情况并不总是发生,而且很难预测它何时会发生,所以它并不可靠。
就我而言,我有一个基于的课程Window
and GetAdornerLayer()
返回空值。事实证明,ControlTemplate
因为我的派生类不包含AdornerDecorator
。将其添加为顶层ControlTemplate
解决了这个问题。
<Style TargetType="my:MyWindow" BasedOn="{StaticResource {x:Type Window}}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="my:MyWindow">
<AdornerDecorator>
<DockPanel ...>
</DockPanel>
</AdornerDecorator>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)