0

I'm new at Silverlight.

I've created a sort of master page using a Page with a frame where the content is loaded. As I handle multiple UserControls at the time (only one is shown, but I want to keep the state of the opened before) I'm setting Content property instead of Navigate method. That way I can assign a UserControl (already created, not a new one as it would be using Navigate with the Uri to the UserControl).

Now I want to take a picture as shown here from the frame when its content changes. If I do it immediately when the content set, the UserControl won't be shown in the picture because it takes a few secs. Frames have the event Navigated, but it doesn't fire with property Content (it just fires when the method Navigate is used, as it name says).

How can I know when new Content is loaded?

If it helps I'm using Silverligh 5.

Community
  • 1
  • 1
Diego
  • 16,436
  • 26
  • 84
  • 136

1 Answers1

0

I've a solution but I don't really like it, so I'm still looking for other ways.

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());
    }
}
Diego
  • 16,436
  • 26
  • 84
  • 136