从另一个XAML访问Storyboard

时间:2009-08-16 22:27:11

标签: silverlight xaml

我正在尝试构建一个使用5个XAMLS的Silverlight应用程序。 第一个,“Page.xaml”包含一个带有4个按钮的菜单和一个用于接收每个内容XAML的Canvas。每个Content XAML都有2个故事板:“entrada”(“输入部分”动画)和“saida”(截面结束动画)。

我遇到以下问题: 菜单位于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 个答案:

答案 0 :(得分:1)

如果我没有弄错的话,通过引用this.Parent获得的对象实际上应该是ContentCanvas对象。所以尝试改变

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

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

假设ContentCanvas实际上是Canvas。