views:

68

answers:

1

Hello. I'm trying to build a Silverlight application where I use 5 XAMLS. The first one, "Page.xaml" contains a menu with 4 buttons and a Canvas to receive the each content XAML. Each Content XAML has 2 storyboards: "entrada" (the "enter section" animation) and "saida" (the section ending animation).

I am experiencing the following problem: The menu is in Page.xaml. I want each button to, when clicked, begin the "saida" storyboard, and, when the storyboard finishes playing, it loads the new content of another XAML (picked by the menu). When I try to do it, Visual Studio keeps telling me that "'ContentCanvas' does not exist in the current context" for each content XAML.

Here is my 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();
            }
        }
    }
}

And here is my sections XAML. All of them are the same.

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();
        }
    }
}

Thanks for the help!

+1  A: 

If I'm not mistaken, the object obtained via the reference this.Parent should actually be the ContentCanvas object. So try changing

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

to

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

assuming ContentCanvas is in fact a Canvas.

Simon Fox
This partially solved the problem. Now the problem seems to be that he can't find "s1", "s2"... and so on, since they are on the main XAML. But great help anyway!
Bruno Schweller
Yes you will definitely need a reference to those objects to be able to remove them that way. If you don't have that reference (this is not a particularly nice solution but will work) you could just set the Name property of s1, s2, s3 when they are created and then iterate over the Children collection check the names of each to get the references you require. Or if those 3 sections are the only children of ContentCanvas just simply use ((Canvas)this.Parent).Children.Remove(((Canvas)this.Parent).Children[0]) ((Canvas)this.Parent).Children.Remove(((Canvas)this.Parent).Children[1]) etc.
Simon Fox
That will solve the problem of removing the children. That was nice! But I still need to control the Child's Storyboard using the main XAML. How can I achieve that? Thanks for the great help!
Bruno Schweller
Maybe you should think about re-architechting? Can you not just define the Storyboards once in the top level Page and when a button is clicked set the storyboards target to the Section you require then start the animation.
Simon Fox
Well, my problem is I want to have an application that moves as smoothly as it could. I would like, if possible, all the transitions work animated. Can you imagine some way to do it? I'm from a flash background, where that was an easy task to accomplish :(
Bruno Schweller
Define the animations you want to apply to each section in your main Page (the one you have posted source for), when a button is clicked, set the target "Section" for the animation and then start the animation. Each of your sections don't need to know anything about how they are bought in and out of view. Hope this is of some help.
Simon Fox
That worked! Thanks a lot!
Bruno Schweller