【问题标题】:Acessing Storyboards from another XAMLS从另一个 XAML 访问情节提要
【发布时间】:2009-08-16 22:27:10
【问题描述】:

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

我遇到以下问题: 菜单位于 Page.xaml 中。我希望每个按钮在单击时开始“saida”故事板,并且当故事板完成播放时,它会加载另一个 XAML 的新内容(由菜单选择)。当我尝试这样做时,Visual Studio 不断告诉我“'ContentCanvas' 在当前上下文中不存在”对于每个内容 XAML。

这是我的 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();
        }
    }
}

感谢您的帮助!

【问题讨论】:

    标签: silverlight xaml


    【解决方案1】:

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

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

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

    假设 ContentCanvas 实际上是一个 Canvas。

    【讨论】:

    • 这部分解决了问题。现在问题似乎是他找不到“s1”、“s2”……等等,因为它们在主 XAML 上。但无论如何都有很大的帮助!
    • 是的,您肯定需要引用这些对象才能以这种方式删除它们。如果您没有该引用(这不是一个特别好的解决方案,但会起作用),您可以在创建 s1、s2、s3 时设置它们的 Name 属性,然后遍历 Children 集合检查每个的名称获取您需要的参考资料。或者,如果这 3 个部分是 ContentCanvas 的唯一子项,只需简单地使用 ((Canvas)this.Parent).Children.Remove(((Canvas)this.Parent).Children[0]) ((Canvas)this.Parent)。 Children.Remove(((Canvas)this.Parent).Children[1]) 等
    • 这将解决删除孩子的问题。那太好了!但我仍然需要使用主 XAML 来控制孩子的故事板。我怎样才能做到这一点?感谢您的大力帮助!
    • 也许你应该考虑重新架构?你能不能只在顶层页面中定义一次故事板,当点击一个按钮时,将故事板目标设置为你需要的部分,然后开始动画。
    • 好吧,我的问题是我希望有一个可以尽可能流畅地移动的应用程序。如果可能的话,我希望所有的过渡都动画化。你能想象一些方法吗?我来自 Flash 背景,这很容易完成:(
    猜你喜欢
    • 2012-06-28
    • 2013-03-28
    • 2011-11-16
    • 1970-01-01
    • 2012-11-05
    • 2017-07-19
    • 2012-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多