Silverlight 4.0 中的子类化 FrameworkElement - 测量通过问题

发布于 2024-11-09 14:14:33 字数 658 浏览 0 评论 0原文

我目前在 Silverlight 中遇到一个问题,我想检测元素大小的变化并对其做出反应。但是,使用 .SizeChanged 进行侦听实际上是不够的,因为在调用 .SizeChanged 中的功能之前,我经常会看到元素正在更改的大小。所以我可能有两个问题。

我的目的是使用测量通道来计算我想要在尺寸发生视觉变化之前应用的操作,以便消除这种闪烁效果。据我所知,成功做到这一点的唯一方法是创建一个 UIElement,在测量其余元素之前在测量过程中执行这些计算。

因此,我希望通过扩展 FrameworkElement 来创建一个真正轻量级的 UIElement。 但是,我无法让这个愚蠢的东西显示任何东西。我的印象是,在 FrameworkElement 级别,子类需要将内容添加到 VisualTree,但我似乎不知道如何做到这一点。

我希望避免扩展 UserControlPanel,因为它们比我需要的要重得多,而且有很多我不想要的额外功能。我只想抓住测量通道并在那里执行一些工作。

那么,是否有可能在 Silverlight 4.0 中扩展 FrameworkElement实际渲染一些内容?如果没有,是否可以通过其他方式监听/中断测量传递?

I currently have an issue in Silverlight where I want to detect the change in size for an element, and react to it. However, listening with .SizeChanged is actually not sufficient, as often I get a flash of the element at the size it's changing to, before the functionality in the .SizeChanged is called. So I have perhaps a two-fold question.

My intention is to use the Measure pass to calculate the manipulations I want to apply before the size is visually changed, so that I can eliminate this flickering effect. As far as I'm aware, the only way to do this successfully is to create a UIElement that does these calculations on the Measure pass, before Measuring the rest of these elements.

As such, I was hoping to create a really light UIElement by extending off FrameworkElement. However, I can't get the stupid thing to display anything. I'm under the impression that at the FrameworkElement level, a subclass would require adding the content to the VisualTree and I can't seem to work out how to do that.

I was hoping to avoid extending off UserControl, or Panel, as they are far heavier than what I need, with so much extra functionality I dont want. I merely want to catch the Measure pass and perform some work there.

So, is it possible to extended off FrameworkElement in Silverlight 4.0 and actually render something? If not, is it possible to listen for/interrupt the measure pass another way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

陈甜 2024-11-16 14:14:33

您无法手动将内容添加到可视化树 - 此功能仅向内置 Silverlight 控件公开。

在这种情况下,您应该从 Control 派生并指定该控件要使用的默认模板 - 在默认模板中,指定可视化树的所需内容。

要允许使用默认模板,您应该在构造函数中指定 DefaultStyleKey 并为其指定控件类型的值。例如DefaultStyleKey = GetType()

然后,您可以在例如 Themes/Generic.xaml 中指定这样的样式,

<Style TargetType="my:MyControl">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="my:MyControl">
                <Rectangle Width="100" Height="100" Fill="Red" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

或者如果您的控件是内容呈现控件,则只需从 ContentControl 派生,并且它需要照顾所有的管道 - 您只需将 Content 设置为您希望显示的任何内容,并且您的子类只需要执行测量传递覆盖逻辑。

You cannot manually add content to the visual tree - this functionality is only exposed to built-in Silverlight controls.

In such a situation, you should derive from Control and specify a default template for this control to use - in the default template, specify the desired contents of the visual tree.

To allow a default template to be used, you should specify DefaultStyleKey in your contructor and give it the value of your control type. E.g. DefaultStyleKey = GetType().

Then, you can specify a style such as this, in e.g. Themes/Generic.xaml

<Style TargetType="my:MyControl">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="my:MyControl">
                <Rectangle Width="100" Height="100" Fill="Red" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Or if your control is a content-presenting control, just derive from ContentControl and it takes care of all the plumbing - you just need to set Content to whatever you wish to display and your subclass will only need to perform the measure-pass override logic.

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