2012-04-16 60 views
0

我是Silverlight的新手。Frame ContentLoaded event

我已經創建了一種使用頁面加載內容的框架的主頁面。由於我當時處理多個UserControl(只顯示一個,但我想保持之前打開的狀態),所以我正在設置Content屬性而不是Navigate方法。這樣我可以分配一個UserControl(已經創建,而不是一個新的,因爲它將使用Uri導航到UserControl)。

現在我想拍照作爲從時其內容改變的幀所示here。如果我在內容設置時立即執行此操作,UserControl將不會顯示在圖片中,因爲它需要幾秒鐘的時間。框架具有Navigated事件,但不會用屬性Content觸發(它只是在使用Navigate方法時觸發,如名稱所示)。

我怎麼能知道當新的內容加載?

如果它有助於我使用Silverligh 5.

回答

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()); 
    } 
}