GetAdornerLayer神秘返回null [英] GetAdornerLayer mysteriously returning null

查看:512
本文介绍了GetAdornerLayer神秘返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用相同的代码位对我没有任何问题的应用程序的多个版本,但我现在神秘接收 NullRerefenceException s的以下内容:

I've been using the same bit of code for several versions of my app with no problems, but I'm now mysteriously receiving NullRerefenceExceptions with the following:

this.Loaded += delegate {
    deleteBrush = new DeleteBrushAdorner( background );
    AdornerLayer al = AdornerLayer.GetAdornerLayer( background );
    al.Add( deleteBrush ); // null ref here??
};



背景只是一个边框元素。

我的两个上这可能是导致它的想法是)切换到.NET 4.0,和b)将实例上述元素(这是用户控件)在的ItemsControl

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.

推荐答案

有关 AdornerLayer.GetAdornerLayer 该文档指定

如果没有发现装饰器层,该方法返回null

If no adorner layers are found, the method returns null.

所以我的猜测是,没有任何装饰器层...你有什么理由认为这不应该是这样的?什么保证你目前依靠会有可视化树装饰器层?

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?

这篇关于GetAdornerLayer神秘返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆