0

我是 Silverlight 的新人。

我使用带有加载内容的框架的 Page 创建了一种母版页。当我当时处理多个用户控件时(只显示一个,但我想保持之前打开的状态),我正在设置 Content 属性而不是 Navigate 方法。这样我就可以分配一个 UserControl(已经创建,而不是一个新的,因为它将使用带有 Uri 的 Navigate 到 UserControl)。

现在我想在内容发生变化时从框架中拍摄一张如此处所示的照片如果我在内容设置后立即执行此操作,则 UserControl 将不会显示在图片中,因为它需要几秒钟。框架具有导航事件,但它不会通过属性 Content 触发(它只是在使用方法 Navigate 时触发,正如它的名字所说)。

我如何知道新内容何时加载?

如果有帮助,我正在使用 Silverligh 5。

4

1 回答 1

0

我有一个解决方案,但我真的不喜欢它,所以我仍在寻找其他方法。

public class CustomFrame : Frame
{
    private readonly RoutedEventHandler loadedDelegate;

    public static readonly DependencyProperty UseContentInsteadNavigationProperty =
        DependencyProperty.Register("UseContentInsteadNavigation", typeof (bool), typeof (CustomFrame), new PropertyMetadata(true));

    public bool UseContentInsteadNavigation
    {
        get { return (bool)GetValue(UseContentInsteadNavigationProperty); }
        set { SetValue(UseContentInsteadNavigationProperty, value); }
    }

    public CustomFrame()
    {
        this.loadedDelegate = this.uc_Loaded;
    }

    public new object Content
    {
        get { return base.Content; }
        set
        {
            if (UseContentInsteadNavigation)
            {
                FrameworkElement fe = (FrameworkElement)value;
                fe.Loaded += loadedDelegate;
                base.Content = fe;
            }
            else
            {
                base.Content = value;
            }
        }
    }

    void uc_Loaded(object sender, RoutedEventArgs e)
    {
        ((UserControl)sender).Loaded -= loadedDelegate;
        OnContentLoaded();
    }

    public delegate void ContentLoadedDelegate(Frame sender, EventArgs e);
    public event ContentLoadedDelegate ContentLoaded;

    private void OnContentLoaded()
    {
        if (ContentLoaded != null)
            ContentLoaded(this, new EventArgs());
    }
}
于 2012-04-17T18:27:08.163 回答