我正在开发一个wpf应用程序,它可以加载几个混合的xaml文件。我的应用程序必须单独运行xaml的动画。我已经做了解析,创建了一个usercontrol,创建了stroryboards并将xaml放入UserControl。我的问题是,我无法运行从xaml获取的动画并添加到userControl。
另外我放置代码:
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 :(得分:0)
我发现问题是当我向我的UserControl添加故事板和画布时,它们与故事板无法找到要设置动画的对象的范围不同。我重新编写代码试图订购并修复它但没有成功!这是新代码:
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;
}