简体   繁体   English

在Silverlight中更改控件的内容时触发的事件

[英]Events fired when you change the contents of a control in Silverlight

Assuming I change the contents of a control using a XamlReader and add the UIElement to the container of a control, what events are supposed to fire? 假设我使用XamlReader更改了控件的内容并将UIElement添加到控件的容器中,应该触发什么事件? There are times where the SizeChanged will fire, LayoutUpdated changing.. though there are other times where neither of these occur despite having changing the contents of a control. 尽管有时更改了控件的内容,但有时会触发SizeChanged,而LayoutUpdated会发生更改。

In my case, I am generating a thumbnail view of what's currently in view on a page. 就我而言,我正在生成页面上当前视图的缩略图视图。 The user can change the content of the page and thus the thumbnail should update accordingly. 用户可以更改页面的内容,因此缩略图应相应更新。 Though, wiring to the LayoutUpdated, Loaded, SizeChanged aren't always reliable for when the contents have changed. 但是,当内容更改时,布线到LayoutUpdated,Loaded,SizeChanged并不总是可靠的。

I would just call my InvalidateThumbnail which uses a writeablebitmap, but it's too quick after setting the content and as a result I will get a blank thumbnail. 我只是打电话给我的InvalidateThumbnail,它使用了writeablebitmap,但是设置内容后太快了,结果我会得到一个空白的缩略图。

At the moment, my hack (cringes) was to wait a few milliseconds before the UI is done rendering the actual new content and I can reliably create the thumbnail. 目前,我的技巧是等待几毫秒,然后UI才完成渲染实际的新内容,并且我可以可靠地创建缩略图。 I'd rather just trigger on an event every time though. 我宁愿每次都触发一个事件。

Possible? 可能? What events should I look at? 我应该看什么活动? I've seen CompositeTarget.Rendering but that's not what I want. 我看过CompositeTarget.Rendering,但这不是我想要的。

Since content is a dependency property, you can use two way databinding and handle when the bound property changes. 由于content是依赖项属性,因此可以使用两种方式进行数据绑定并在绑定属性更改时进行处理。 Here is an example 这是一个例子

XAML XAML

<Grid x:Name="LayoutRoot">
    <StackPanel>
        <ContentControl x:Name="ContentControl" Content="{Binding ContentProperty, Mode=TwoWay}"/>
        <Button Click="Button_Click" Content="Change Content"/>
    </StackPanel>
</Grid>

Code Behind 背后的代码

public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();
        ContentControl.DataContext = new SomeObject();

    }
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        ContentControl.Content = XamlReader.Load("<TextBlock xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" Text=\"Hello\"/>");
    }
}

public class SomeObject
{
    private object _contentProperty = null;
    public object ContentProperty
    {
        get
        {
            return _contentProperty;
        }
        set
        {
            _contentProperty = value;
            MessageBox.Show("Content Changed");
        }
    }
}

Although the events are occurring on their own I can use the ContentControl and use TwoWay databinding to ensure that when xaml is updated, I can insert the new xaml so that should work. 尽管事件是独立发生的,但我可以使用ContentControl并使用TwoWay数据绑定来确保更新xaml时,可以插入新的xaml,以使其正常工作。

On the other half of my question, I did find out some of the issues surrounding when the visualtree is available and what the control lifecycle refers to. 在我的问题的另一半,我确实发现了可视树何时可用以及控件生命周期所指的一些问题。

http://blogs.msdn.com/devdave/archive/2008/10/11/control-lifecycle.aspx [ http://blogs.msdn.com/silverlight_sdk/archive/2008/10/24/loaded-event-timing-in-silverlight.aspx] http://blogs.msdn.com/devdave/archive/2008/10/11/control-lifecycle.aspx [ http://blogs.msdn.com/silverlight_sdk/archive/2008/10/24/loaded-event-定时功能于silverlight.aspx]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM