从另一个XAMLS处理故事板
问题描述:
我正在尝试使用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();
}
}
}
感谢您的帮助!
答
如果我没有弄错,通过引用this.Parent获得的对象实际上应该是ContentCanvas对象。因此,尝试改变
this.Parent.ContentCanvas.Children.Remove(s1);
到
((Canvas)this.Parent).Children.Remove(s1);
假设ContentCanvas其实画布。
这部分解决了这个问题。现在问题似乎是他找不到“s1”,“s2”......等等,因为它们在主XAML上。但是无论如何,帮助很大! – 2009-08-16 22:40:28
是的,您肯定需要参考那些能够以这种方式去除它们的对象。如果你没有这个引用(这不是一个特别好的解决方案,但会起作用),你可以在创建它们时设置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
这将解决拆除儿童的问题。那很好!但我仍然需要使用主XAML来控制孩子的故事板。我怎样才能做到这一点?感谢您的帮助! – 2009-08-16 22:56:17