Runtime Alternative to XamlReader.Load in Silverlight 2 Beta 2

This Sample was update for Silverlight 2 Beta 2 ( the beta 1 example is here )

I can see XamlReader.Load has it's uses for adding FrameworkElements at runtime, but I really hate the way the code looks. Additionally, I don't like the idea of managing a bunch of String.Format statements for my XAML. Things get even more complex when you want to take advantage of events. Luckily, there is an alternative.

Here is an example of adding elements (ellipse, storyboard, doubleanimation) at runtime, and attaching events so that they are removed when the storyboard ends:

public Page()
        {
            InitializeComponent();
            this.MouseLeftButtonUp += new MouseButtonEventHandler(Page_MouseLeftButtonUp);
            updateElementCount();
        }

        public int spriteCount { get; set; }

        void Page_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Point point = e.GetPosition(this);
            Ellipse ellipse = new Ellipse();
            SolidColorBrush mySolidColorBrush = new SolidColorBrush();
            DoubleAnimation animation = new DoubleAnimation();
            Storyboard storyBoard = new Storyboard();
            
            spriteCount += 1;
            string spriteName =string.Format("SpriteEllipse{0}", spriteCount);
            ellipse.SetValue(Ellipse.NameProperty, spriteName);
            mySolidColorBrush.Color = Color.FromArgb(255, 0, 0, 0);
            ellipse.Fill = mySolidColorBrush;
            ellipse.Width = 30;
            ellipse.Height = 30;
            ellipse.SetValue(Canvas.TopProperty, (point.Y - ellipse.Width/2));
            ellipse.SetValue(Canvas.LeftProperty, (point.X - ellipse.Width/2));
            LayoutRoot.Children.Add(ellipse);
            
            animation.Duration = new TimeSpan(0,0,3);
            animation.From = 1;
            animation.To = 0;
            animation.SetValue(Storyboard.TargetNameProperty, spriteName);
            animation.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath(String.Format("{0}.Opacity", spriteName)));

            storyBoard.Completed+=new EventHandler(storyBoard_Completed);
            storyBoard.SetValue(Storyboard.TargetNameProperty, spriteName);
            
            storyBoard.Children.Add(animation);
            LayoutRoot.Resources.Add(string.Format("{0}_storyBoard", spriteName), storyBoard);
            storyBoard.Begin();

            updateElementCount();
        }

        void storyBoard_Completed(object sender, EventArgs e)
        {
            Storyboard storyBoard = (Storyboard)sender;
            string spriteName = storyBoard.GetValue(Storyboard.TargetNameProperty).ToString();
            FrameworkElement sprite = (FrameworkElement)LayoutRoot.FindName(spriteName);
            LayoutRoot.Children.Remove(sprite);
            LayoutRoot.Resources.Remove(string.Format("{0}_storyBoard", spriteName));
            updateElementCount();
        }

        void updateElementCount()
        {
            this.txtChildren.Text = string.Format("Children.Count = {0}", LayoutRoot.Children.Count());
            this.txtResources.Text = string.Format("Resources.Count = {0}",  LayoutRoot.Resources.Count);
        }
Published Saturday, June 07, 2008 7:37 PM by RogerGuess

Comments

# Runtime Alternative to XamlReader.Load in Silverlight

Sunday, June 08, 2008 5:31 PM by Learning Silverlight

Update: This was a Silverlight Beta 1 example. Beta 2 now has updates to make this even easier. Here

# Runtime Alternative to XamlReader.Load in Silverlight

Wednesday, June 11, 2008 10:46 PM by RogerGuess

Update: This was a Silverlight Beta 1 example. Beta 2 now has updates to make this even easier. Here

# re: Runtime Alternative to XamlReader.Load in Silverlight 2 Beta 2

Thursday, June 12, 2008 11:17 AM by Frank Wu

Your example is too simple, we need XamlReader.Load, because it can support complex application. If you hate managing a bunch of String, we can read element one by one just like xmlreader, but XamlReader does not support it.

# re: Runtime Alternative to XamlReader.Load in Silverlight 2 Beta 2

Friday, July 18, 2008 12:46 PM by John "Z-Bo" Zabroski

Clever, and very cleanly written code.

However, You *have* to use XamlReader.Load to programmatically assign ControlTemplate and DataTemplate objects.  I think this is what Frank Wu means by "support[ing] complex applicationSleep".

# re: Runtime Alternative to XamlReader.Load in Silverlight 2 Beta 2

Saturday, November 15, 2008 4:19 PM by ijaxakxcq

KJTKsz  <a href="opyihbctuetj.com/.../a>, [url=http://rgpzncazbkcd.com/]rgpzncazbkcd[/url], [link=http://bygqstucypwo.com/]bygqstucypwo[/link], http://wrchbuixgbvv.com/

Leave a Comment

(required) 
(required) 
(optional)
(required) 
Powered by Community Server (Non-Commercial Edition), by Telligent Systems