Silverlight Code Creation Animation

Source: Internet
Author: User

C # 3.0 syntax is used in the code

The effect is a red rectangle moving from the lower-right corner to the upper-left corner

Just an example that shows how to create animations dynamically in code

MainPage.xaml

<UserControl  x:Class="Hongcing.Silverlight.Create_And_Run_Animation"
xmlns="http://schemas. microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft. com/winfx/2006/xaml">
<Canvas  Loaded="LayoutRoot_Loaded" />
</UserControl>

MainPage.xaml.cs

1 using System;
2 using System.Windows;
3 using System.Windows.Controls;
4 using System.Windows.Media;
5 using System.Windows.Media.Animation;
6 using System.Windows.Shapes;
7
8 namespace Hongcing.silverlight
9 {
Ten public partial class Create_and_run_animation:usercontrol
11 {
Public Create_and_run_animation ()
13 {
InitializeComponent ();
15}
16
-private void Layoutroot_loaded (object sender, RoutedEventArgs e)
18 {
var redrectangle = new Rectangle
20 {
Width = 300,
Height = 200,
Fill = new SolidColorBrush (colors.red),
Stroke = new SolidColorBrush (colors.black)
25};
26
(sender as Panel). Children.add (Redrectangle);
28
var leftanimation = new DoubleAnimation
30 {
Duration = new Duration (Timespan.fromseconds (5)),
From = 700,
to = 0
34};
35
var topanimation = new DoubleAnimation
37 {
Duration = Leftanimation.duration,
From 350,
to = 0
41};
42
Storyboard.settarget (Leftanimation, Redrectangle);
Storyboard.settarget (Topanimation, Redrectangle);
45
46//property path can also be used with new PropertyPath ("(Canvas.Left)"), New PropertyPath ("(canvas.top)")
Storyboard.settargetproperty (Leftanimation, New PropertyPath (Canvas.leftproperty));
Storyboard.settargetproperty (Topanimation, New PropertyPath (Canvas.topproperty));
49
50//This is not added to the resource, but it starts the animation directly.
Wuyi new Storyboard {Children = {leftanimation, topanimation}}. Begin ();
52}
53}
54}
55

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.