Step 5. Replace the Page.xaml control with the following XAML:

 

<UserControl x:Class="ShootBalloonForBlog.Page"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Width="800" Height="600">

    <Canvas x:Name="LayoutRoot" Background="White">

        <MediaElement x:Name="BackMusic" Source="Symphony_No_3.wma" AutoPlay="False"></MediaElement>

        <Button Name ="ButtonPlay"  Canvas.Left="650" Canvas.Top="50" Height="20" Width="50" Content="Play" Click="Button_Click_PLay"></Button>

        <Button Canvas.Left="650" Canvas.Top="100" Height="20" Width="50" Content="Stop" Click="Button_MouseLeftButtonDown_StopTest"></Button>

 

        <TextBlock Canvas.Left="650" Canvas.Top="200" Height="20" Width="30" Text="Level" FontSize="11"></TextBlock>

        <TextBox Name="Textlevel" Canvas.Left="690" Canvas.Top="200" Height="20" Width="30" Text="1" FontSize="11"></TextBox>

 

        <TextBlock Canvas.Left="650" Canvas.Top="250" Height="20" Width="30" Text="Score:" FontSize="11"></TextBlock>

        <TextBlock Name="TextScore" Canvas.Left="690" Canvas.Top="250" Height="20" Width="200" Text="0" FontSize="11"></TextBlock>

 

    </Canvas>

</UserControl>

 

Step 6. Replace the Page.xaml.cs with the following code:

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

 

namespace ShootBalloonForBlog

{

    public partial class Page : UserControl

    {

        public Page()

        {

            InitializeComponent();

        }

 

        private Storyboard m_sb = null;

        private int m_score = 0;

 

        private int m_currentLevel = 1;

        private int DisplayLevel

        {

            get

            {

                int level;

                try

                {

                    level = Convert.ToInt32(Textlevel.Text);

                }

                catch

                {

                    level = 1;

                }

                Textlevel.Text = level.ToString();

                return level;

            }

        }

 

        private void AnimateBallonTest()

        {

            m_score = 0;

            TextScore.Text = m_score.ToString();

 

            m_sb = new Storyboard();

            m_sb.Completed += Storyboard_Completed;

 

            RepeatBehavior repeatOnce = new RepeatBehavior(1);

 

            m_currentLevel = DisplayLevel;

 

            //Generate random ballons

            Random random = new Random();

 

            for (int i = 0; i < 60 * m_currentLevel; i++)

            {

                double scaleFactor = (double)(random.Next(100, 1000)) / 1000;    // controls the size of the balloon .1 to 1

               

                // calculate and set our balloon color using an extension method that help keep the saturation up

                Color myColor = Color.FromArgb((byte)random.Next(100, 255),

                    (byte)random.Next(0, 255), (byte)random.Next(0, 255), (byte)random.Next(0, 255));

                myColor.A = (byte)(255 - (byte)(100 * scaleFactor));

               

                // create a new balloon

                Balloon balloon1 = new Balloon(scaleFactor, myColor);

 

                LayoutRoot.Children.Add(balloon1);

                int startingTop = random.Next(500, 2000);

                int startingLeft = random.Next(1, 10) * 55;

                int travelTime = random.Next(10, 20);   //the maximum is 20 seconds

                Duration duration = new Duration(TimeSpan.FromSeconds(travelTime));

                Duration duration1Sec = new Duration(TimeSpan.FromSeconds(1));

 

                SetPropertyDoubleAnimToStoryBoardBeta2(m_sb, duration, repeatOnce, balloon1, new PropertyPath("Opacity"), 1, 0.5);

                SetPropertyDoubleAnimToStoryBoardBeta2(m_sb, duration, repeatOnce, balloon1, new PropertyPath("(Canvas.Left)"), startingLeft, startingLeft);

                SetPropertyDoubleAnimToStoryBoardBeta2(m_sb, duration, repeatOnce, balloon1, new PropertyPath("(Canvas.Top)"), startingTop, -120); //max height of balloon is 120

 

                balloon1.Text.Text = BalloonAction.GetRandomAction(random);

 

                balloon1.MouseLeftButtonDown += ClickOnBalloon;

            }

            // Make the Storyboard a resource.

            if (!LayoutRoot.Resources.Contains("ResourceStoryBoard1"))

            {

                LayoutRoot.Resources.Add("ResourceStoryBoard1", m_sb);

            }

 

            // Begin the animation.

            m_sb.Begin();

 

            BackMusic.Play();

        }

 

 

        private void Storyboard_Completed(object sender, EventArgs e)

        {

            TextScore.Text += " Final";

            ButtonPlay.IsEnabled = true;

            BackMusic.Stop();

            clearBalloons();

 

            LayoutRoot.Resources.Remove("ResourceStoryBoard1");

        }

 

        private void ClickOnBalloon(object sender, MouseButtonEventArgs e)

        {

            Balloon ballon = (Balloon)sender;

            m_score = BalloonAction.EvaluateAciton(ballon.Text.Text, m_score);

            TextScore.Text = m_score.ToString();

            ballon.Visibility = Visibility.Collapsed;

        }

 

        private void clearBalloons()

        {

            int i = 0;

            while (i < LayoutRoot.Children.Count)

            {

                UIElement element = LayoutRoot.Children[i];

                if (element.GetType() == Type.GetType("ShootBalloonForBlog.Balloon"))

                {

                    LayoutRoot.Children.Remove(element);

                    continue;

                }

                i++;

            }

        }

 

        private void Button_MouseLeftButtonDown_StopTest(object sender, RoutedEventArgs e)

        {

            if (m_sb != null)

            {

                clearBalloons();

                m_sb.Stop();

 

                BackMusic.Stop();

                //LayoutRoot.Children.RemoveAt(2);

            }

 

            ButtonPlay.IsEnabled = true;

        }

 

        private void SetPropertyDoubleAnimToStoryBoardBeta2(Storyboard sb, Duration duration,

                    RepeatBehavior repeateBehavior, DependencyObject target,

                    PropertyPath property, double fromValue, double toValue)

        {

            DoubleAnimation myDoubleAnimation1 = new DoubleAnimation();

            myDoubleAnimation1.Duration = duration;

            sb.Children.Add(myDoubleAnimation1);

            myDoubleAnimation1.RepeatBehavior = repeateBehavior;

            Storyboard.SetTarget(myDoubleAnimation1, target);

 

            Storyboard.SetTargetProperty(myDoubleAnimation1, property);

            myDoubleAnimation1.From = fromValue;

            myDoubleAnimation1.To = toValue;

        }

 

        private void Button_Click_PLay(object sender, RoutedEventArgs e)

        {

            ButtonPlay.IsEnabled = false;

            AnimateBallonTest();

        }

    }

}

 

Step 7. Now you should be able to run this application inside Silverlight 2 Beta 2.  Enjoy

 

Updated 10/03/2008: Thanks for the comments.  I uploaded the project file here:

 http://xqiu.members.winisp.net/download/ShootBalloonForBlog.zip