Introduction
In this article we will show an animation of a moving ball in a Windows Phone application through the ApplicationBar code buttons start and stop. In this animation we will add a storyboard_Completed event that will move the ellipse and also check for collisions against the layoutroots dimensions. When a collision happens the direction will be reversed. The way that the start and stop buttons work are, when the user clicks the start button the IsBouncing property of the control is true and the ball begins to move in the phone screen and simply when the stop button is clicked the Isbouncing property is set to false and the ball will be stop. To move the ball in the phone's screen we will create a method startMove() in code behind; the speed and direction of the ball will be set randomly.
Step 1 : Open Visual Studio.- Select new -> project
- Select your preferred language
- Select Silverlight for Windows Phone application
- Name the project
- Now name of project is "MovingBallApplication"


Step 2: Now make some modification on MainPage.xaml page.
- Modify the ContentGrid, change it to a canvas and add an ellipse
- Uncomment the sample app bar code and change it to the following, to add a click event and button images
- Add a UserControl.Resources for Storyboard control
Step 3 : The whole code of the MainPage.xaml page is:
Code
<phone:PhoneApplicationPage
x:Class="MovingBallApplication.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="696"
shell:SystemTray.IsVisible="True">
<UserControl.Resources>
<!-- Game loop -->
<Storyboard x:Name="ellipseStoryboard" Completed="ellipseStoryboard_Completed">
</Storyboard>
</UserControl.Resources>
<!--LayoutRoot contains the root grid where all other page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="24,24,0,12">
<TextBlock x:Name="ApplicationTitle" Text="Animation" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="Moving Ball" Margin="-3,-8,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
<Canvas x:Name="ContentCanvas" Grid.Row="1">
<Ellipse Canvas.Left="350" Canvas.Top="150" Width="100" Height="100" Name="ellipseObject" Canvas.ZIndex="1" >
<Ellipse.Fill>
<LinearGradientBrush StartPoint='0.1,0.06' EndPoint='0.5,0.6'>
<GradientStop Color='#FFFFFFFF' Offset='0'/>
<GradientStop Color="#FFBF1BB2" Offset='1'/>
</LinearGradientBrush>



SheikAbdullah LatheepPosted May 22, 2012, 8:18 AM
This code is not working for me.. pls share the original source code