2011-06-22 103 views
1

我正在加載幾個在混合中製作的xaml文件的wpf應用程序。我的應用程序必須運行xaml的動畫individualy。我已經做了解析,創建一個usercontrol,創建stroryboards並將xaml放入UserControl。我的問題是,我不能運行從xaml採取的動畫,並添加到userControl。我想加載一個xaml從我的應用程序運行它的動畫

此外,我放置代碼:

private void FillMenuWindow(ScreeningArea area) 
    { 
     string lData = null; 
     bool isCanvasAdded = false; 
     UserControl menuUserControl = new UserControl(); 
     Canvas XAMLWindowCanvas = new Canvas(); 
     XDocument xmlFile = new XDocument(); 
     IEnumerable<XElement> elementList; 
     MemoryStream stream = null; 
     int initSide = 0; 
     menuStoryboardList.Clear(); 
     if (area.CurrentItem != null) 
     { 
      SimpleDelegate resumedDelegate = delegate 
      { 
       string nameCounter; 
       string myStream; 
       string mystrName; 
       xmlFile = XDocument.Load(VideoMenuSystem.DataPath + "\\" + area.CurrentItem.ItemName + ".xaml");      
       StreamReader menuReader = new StreamReader(VideoMenuSystem.DataPath + "\\" + area.CurrentItem.ItemName + ".xaml"); 

       myStream = menuReader.ReadToEnd(); 
       TextToFile(myStream, VideoMenuSystem.DataPath + "\\" + "xamlFile.txt",false); 
       menuUserControl = new UserControl(); 
       //this.RegisterName("Test", menuUserControl); 
       elementList = xmlFile.Root.Descendants();      

       foreach (XElement el in elementList) 
       { 
        mystrName = el.Name.LocalName; 
        switch (mystrName) 
        { 
         case "Storyboard": 
          lData = el.ToString(); 
          TextToFile(lData, VideoMenuSystem.DataPath + "\\" + "storyBoardFile.txt", true); 
          stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(lData)); 
          myStoryboard.Children.Add(XamlReader.Load(stream) as Storyboard);         
          break; 
         case "Canvas": 
          lData = el.ToString(); 
          if (lData != null) 
          { 
           stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(lData)); 
           XAMLWindowCanvas.Children.Add(XamlReader.Load(stream) as UIElement);          
           foreach (ScreeningArea sa in Screens) 
           { 
            if (sa == area) 
            { 
             initSide = menuSize.Width + screenSeparator; 
             break; 
            } 
           } 
           XAMLWindowCanvas.Margin = new Thickness(left, 0, 0, 0);          
           menuUserControl.Content = XAMLWindowCanvas; 
           left += initSide; 
          } 
          isCanvasAdded = true; 
          break; 
        } 
        if (isCanvasAdded) 
         break; 
       } 
       nameCounter = DateTime.Now.Ticks.ToString(); 
       String myNameCounter = nameCounter.Substring(nameCounter.Length - 10); 
       menuUserControl.Resources.Add("SB_" + myNameCounter, myStoryboard); 
       menuStoryboardList.Add(myStoryboard); 
       playbackArea.Children.Add(menuUserControl); 
       playbackArea.Loaded += new RoutedEventHandler(fireAnimation);      
      }; 
      Dispatcher.BeginInvoke(resumedDelegate); 
     } 
    } 

回答

0

我想通了,問題是,當我加入故事板和帆布給我的用戶,他們並不在同一個範圍內,之所以故事板無法找到對象動畫。我重新編寫代碼嘗試訂購併修復它,但沒有成功!這是新代碼:

private UserControl ReadMenuObj(ScreeningArea area) 
{ 
    bool isCanvasAdded = false; 
    int initSide = 0; 
    string myStream; 
    string mystrName; 
    string nameCounter; 
    string lData = null; 
    UserControl localUC = new UserControl(); 
    XDocument xmlFile = new XDocument(); 
    Storyboard localSB = new Storyboard(); 
    Canvas XAMLWindowCanvas = new Canvas(); 
    IEnumerable<XElement> elementList; 
    StreamReader menuReader; 
    MemoryStream stream = null; 

    xmlFile = XDocument.Load(VideoMenuSystem.DataPath + "\\" + area.CurrentItem.ItemName + ".xaml"); 
    menuReader = new StreamReader(VideoMenuSystem.DataPath + "\\" + area.CurrentItem.ItemName + ".xaml"); 

    myStream = menuReader.ReadToEnd(); 
    TextToFile(myStream, VideoMenuSystem.DataPath + "\\" + "xamlFile.txt", false); 
    elementList = xmlFile.Root.Descendants(); 

    myStoryboard = new Storyboard(); 
    foreach (XElement el in elementList) 
    { 
     mystrName = el.Name.LocalName; 
     switch (mystrName) 
     { 
      case "Storyboard": 
       lData = el.ToString(); 
       TextToFile(lData, VideoMenuSystem.DataPath + "\\" + "storyBoardFile.txt", true); 
       stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(lData)); 
       localSB = XamlReader.Load(stream) as Storyboard; 
       for (int i = 0; i < localSB.Children.Count; i++) 
       { 
        Timeline tm = (Timeline)localSB.Children.ElementAt(i); 
        myStoryboard.Children.Add(tm); 
       } 
       //myStoryboard.Children.Add(localSB); 
       break; 
      case "Canvas": 
       lData = el.ToString(); 
       XAMLWindowCanvas = new Canvas(); 
       if (lData != null) 
       { 
        TextToFile(lData, VideoMenuSystem.DataPath + "\\" + "xamlFile2.txt", true); 
        stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(lData)); 
        DependencyObject rootObject = XamlReader.Load(stream) as DependencyObject; 
        XAMLWindowCanvas = rootObject as Canvas; 
        //XAMLWindowCanvas.Children.Add(XamlReader.Load(stream) as UIElement); 
        foreach (ScreeningArea sa in Screens) 
        { 
         if (sa == area) 
         { 
          initSide = menuSize.Width + screenSeparator; 
          break; 
         } 
        } 
        XAMLWindowCanvas.Margin = new Thickness(left, 0, 0, 0); 
        //XAMLWindowCanvas.Loaded += new RoutedEventHandler(fireAnimation); 
        XAMLWindowCanvas.MouseDown += new MouseButtonEventHandler(fireAnimation); 
        localUC.Content = XAMLWindowCanvas; 
        int cnvConut = XAMLWindowCanvas.Children.Count; 
        left += initSide; 
       } 
       isCanvasAdded = true; 
       break; 
     } 
     if (isCanvasAdded) 
      break; 
    } 
    menuStoryboardList.Add(myStoryboard); 
    nameCounter = DateTime.Now.Ticks.ToString(); 
    String myNameCounter = nameCounter.Substring(nameCounter.Length - 10); 
    localUC.Resources.Add("SB_" + myNameCounter, myStoryboard); 
    Canvas cnv = (Canvas)localUC.FindName("Combo_3"); 
    Storyboard storyboard = (Storyboard)localUC.Resources["CombonAnim"]; 
    return localUC; 
} 
相關問題