GetAdornerLayer 神秘地返回 null

发布于 2024-09-05 19:04:52 字数 539 浏览 2 评论 0原文

我一直在我的应用程序的多个版本中使用相同的代码,没有任何问题,但我现在神秘地收到 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 NullRerefenceExceptions 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

〃安静 2024-09-12 19:04:52

就我而言,我有一个基于 Window 的类,并且 GetAdornerLayer() 返回 null。事实证明,我的派生类的 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>

In my case I had a class that is based on Window and GetAdornerLayer() returned null. It turned out that the ControlTemplate for my derived class did not contain the AdornerDecorator. Adding that as the top level in the ControlTemplate solved the issue.

<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>
风追烟花雨 2024-09-12 19:04:52

AdornerLayer.GetAdornerLayer 的文档指定:

如果没有找到装饰器层,该方法返回 null。

所以我的猜测是没有装饰层......您有任何理由相信情况不应该如此吗?您当前依赖什么保证视觉树中会有装饰器层?

The docs for AdornerLayer.GetAdornerLayer specify:

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?

若沐 2024-09-12 19:04:52

我很好奇这个问题是否真的得到了解决。 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...

咿呀咿呀哟 2024-09-12 19:04:52

这个答案可能会迟到,但无论如何......

AdornerDecorators的文档写有以下内容:

如果您传入的元素在其可视化树中没有 AdornerDecorator 作为祖先,则 GetAdornerLayer 方法将返回 null。

那么也许您的 background 元素不是 AdornerDecorator 的子元素?

This answer may come late but anyway...

In the documentation of AdornerDecorators the following is written:

The GetAdornerLayer method returns null if you pass in an element that does not have an AdornerDecorator as an ancestor in its visual tree.

So maybe your background element was not a Child of an AdornerDecorator?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文