2009-08-16 78 views
1

我正在嘗試使用5個XAMLS構建Silverlight應用程序。 第一個「Page.xaml」包含一個帶有4個按鈕的菜單和一個Canvas來接收每個內容的XAML。每個內容XAML有2個故事板:「entrada」(「輸入部分」動畫)和「說出」(結束動畫的部分)。從另一個XAMLS處理故事板

我遇到了以下問題: 菜單位於Page.xaml中。我希望每個按鈕在單擊時開始「說出」故事板,並且當故事板完成播放時,它會加載另一個XAML的新內容(由菜單選取)。當我嘗試這樣做時,Visual Studio會一直告訴我,對於每個內容XAML,「ContentCanvas」在當前上下文中不存在「。

這裏是我的Page.xaml.cs:

using System; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Ink; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 

namespace SilverlightPagingSystemProject 
{ 
    public partial class Page : UserControl 
    { 
     String secao = "home"; 
     Section1 s1 = new Section1(); 
     Section2 s2 = new Section2(); 
     Section3 s3 = new Section3(); 

     public Page() 
     { 
      // Required to initialize variables 
      InitializeComponent(); 
      Link1.MouseLeftButtonDown += new MouseButtonEventHandler(Link1_MouseLeftButtonDown); 
      Link2.MouseLeftButtonDown += new MouseButtonEventHandler(Link2_MouseLeftButtonDown); 
      Link3.MouseLeftButtonDown += new MouseButtonEventHandler(Link3_MouseLeftButtonDown); 
     } 

     private void Link1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
     { 
      if (secao == "home") 
      { 
       ContentCanvas.Children.Remove(s1); 
       ContentCanvas.Children.Remove(s2); 
       ContentCanvas.Children.Remove(s3); 
       ContentCanvas.Children.Add(s1); 
      } 
      else 
      { 
       ContentCanvas.saida.Begin(); 
      } 
     } 

     private void Link2_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
     { 
      if (secao == "home") 
      { 
       ContentCanvas.Children.Remove(s1); 
       ContentCanvas.Children.Remove(s2); 
       ContentCanvas.Children.Remove(s3); 
       ContentCanvas.Children.Add(s2); 
      } 
      else 
      { 
       ContentCanvas.saida.Begin(); 
      } 
     } 

     private void Link3_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
     { 
      if (secao == "home") 
      { 
       ContentCanvas.Children.Remove(s1); 
       ContentCanvas.Children.Remove(s2); 
       ContentCanvas.Children.Remove(s3); 
       ContentCanvas.Children.Add(s3); 
      } 
      else 
      { 
       ContentCanvas.saida.Begin(); 
      } 
     } 
    } 
} 

這是我的部分XAML。他們都是一樣的。

using System; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Ink; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 

namespace SilverlightPagingSystemProject 
{ 
    public partial class Section3 : UserControl 
    { 
     public Section3() 
     { 
      // Required to initialize variables 
      InitializeComponent(); 
      Section3LayoutRoot.Loaded += new RoutedEventHandler(Section1LayoutRoot_Loaded); 
      saida.Completed += new EventHandler(saida_Completed); 
     } 

     void saida_Completed(object sender, EventArgs e) 
     { 
      this.Parent.ContentCanvas.Children.Remove(s1); 
      this.Parent.ContentCanvas.Children.Remove(s2); 
      this.Parent.ContentCanvas.Children.Remove(s3); 
      this.Parent.ContentCanvas.Children.Add(secao); 
     } 

     void Section1LayoutRoot_Loaded(object sender, RoutedEventArgs e) 
     { 
      entrada.Begin(); 
     } 
    } 
} 

感謝您的幫助!

回答

1

如果我沒有弄錯,通過引用this.Parent獲得的對象實際上應該是ContentCanvas對象。因此,嘗試改變

this.Parent.ContentCanvas.Children.Remove(s1); 

((Canvas)this.Parent).Children.Remove(s1); 

假設ContentCanvas其實畫布。

+0

這部分解決了這個問題。現在問題似乎是他找不到「s1」,「s2」......等等,因爲它們在主XAML上。但是無論如何,幫助很大! – 2009-08-16 22:40:28

+0

是的,您肯定需要參考那些能夠以這種方式去除它們的對象。如果你沒有這個引用(這不是一個特別好的解決方案,但會起作用),你可以在創建它們時設置s1,s2,s3的Name屬性,然後迭代Children集合,檢查每個屬性的名稱獲取你需要的參考資料。或者,如果這3個部分是ContentCanvas的唯一子項,只需使用((Canvas)this.Parent).Children.Remove(((Canvas)this.Parent).Children [0])((Canvas)this.Parent)。 (((帆布)this.Parent).Children [1])等。 – 2009-08-16 22:45:40

+0

這將解決拆除兒童的問題。那很好!但我仍然需要使用主XAML來控制孩子的故事板。我怎樣才能做到這一點?感謝您的幫助! – 2009-08-16 22:56:17

相關問題