GetAdornerLayer 神秘地返回 null
我一直在我的应用程序的多个版本中使用相同的代码,没有任何问题,但我现在神秘地收到 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
)放置在 ItemsControl
中。
奇怪的是,这种情况并不总是发生,而且很难预测它何时会发生,所以它并不可靠。
I've been using the same bit of code for several versions of my app with no problems, but I'm now mysteriously receiving NullRerefenceException
s with the following:
this.Loaded += delegate {
deleteBrush = new DeleteBrushAdorner( background );
AdornerLayer al = AdornerLayer.GetAdornerLayer( background );
al.Add( deleteBrush ); // null ref here??
};
background
is just a Border
element.
My two thoughts on what could be causing it are a) switching to .NET 4.0, and b) placing instances of the above element (which is a UserControl
) in an ItemsControl
.
Oddly this doesn't happen all the time, and it's hard to predict when it will happen, so it's not reliable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
就我而言,我有一个基于
Window
的类,并且GetAdornerLayer()
返回 null。事实证明,我的派生类的ControlTemplate
不包含AdornerDecorator
。将其添加为ControlTemplate
中的顶层解决了该问题。In my case I had a class that is based on
Window
andGetAdornerLayer()
returned null. It turned out that theControlTemplate
for my derived class did not contain theAdornerDecorator
. Adding that as the top level in theControlTemplate
solved the issue.AdornerLayer.GetAdornerLayer 的文档指定:
所以我的猜测是没有装饰层......您有任何理由相信情况不应该如此吗?您当前依赖什么保证视觉树中会有装饰器层?
The docs for AdornerLayer.GetAdornerLayer specify:
So my guess is that there are no adorner layers... do you have any reason to believe that this shouldn't be the case? What guarantee are you currently relying on that there will be an adorner layer in the visual tree?
我很好奇这个问题是否真的得到了解决。 AdornerDecorator 为其下方的元素提供一个 AdornerLayer —— 所有内容都将位于其下方。它是一个装饰器,这意味着它有一个孩子,即内容。该内容是通过 AdornerLayer 提供的。因此,如果您在 XAML 中放置一个 AdornerDecorator 并且子级是边框,则边框确实有一个 AdornerLayer。
此外,Window 将 AdornerDecorator 定义为可视化树的顶部,因此 Window 中的任何元素都将在其上方有一个 AdornerLayer。所以,如果您上面的内容在窗口中......
I'm curious as to whether or not this was really solved. An AdornerDecorator provides an AdornerLayer for element below it -- and everything will be below it. It is a decorator, meaning it has a Child that is the content. That content is being provided with an AdornerLayer. So, if you put an AdornerDecorator in your XAML and the child is the border, the border does have an AdornerLayer.
Furthermore, Window defines an AdornerDecorator as the top of the visual tree so any element in a Window will have an AdornerLayer above it. So, if your conent above was in a Window...
这个答案可能会迟到,但无论如何......
在 AdornerDecorators的文档写有以下内容:
那么也许您的
background
元素不是AdornerDecorator
的子元素?This answer may come late but anyway...
In the documentation of AdornerDecorators the following is written:
So maybe your
background
element was not a Child of anAdornerDecorator
?