
In questo esempio, vediamo come aggiungere semplicemente degli elementi di layout Silverlight di diverso tipo a run-time e gestirne delle semplici animazioni, nell'esempio è presente anche un controllo mediaelement per la riproduzione di un file audio mp3 con la possibilità di mettere in mute la riproduzione.
Il codice e fornito così com'è, senza commenti, indentazioni etc..
Per me è stato molto utile, spero lo sia anche per voi.
In this example, we add layout elements in grid control at run-time.
We add simple animations too and a media element for .mp3 playback with option to mute/unmute audio.

This is tested with Microsoft Silverlight 3.0:
XAML (page.xaml):
(see image)
C# (page.xaml.cs):
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.Resources;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace SimpleImageAnimation
{
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
}
private void img2_MouseLeftButtonDown(Object sender, MouseEventArgs e)
{
//TransTimeline.Begin();
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
MainControl.Width = 690;
MainControl.Height = 100;
LayoutRoot.Width = 690;
LayoutRoot.Height = 100;
Canvas newCanvas = new Canvas();
newCanvas.Name = "TextMessages";
Canvas icoCanvas = new Canvas();
icoCanvas.Name = "Icons";
icoCanvas.VerticalAlignment = VerticalAlignment.Top;
icoCanvas.HorizontalAlignment = HorizontalAlignment.Right;
icoCanvas.Width = 22;
icoCanvas.Height = 22;
LayoutRoot.Children.Add(newCanvas);
Image newimage1 = new Image();
newimage1.Name = "img1";
newimage1.Source = new BitmapImage(new Uri("Images/sound_off.jpg", UriKind.RelativeOrAbsolute));
newimage1.MouseLeftButtonDown += new MouseButtonEventHandler(newimage1_MouseLeftButtonDown);
newimage1.Width = 22;
newimage1.Height = 22;
icoCanvas.Children.Add(newimage1);
LayoutRoot.Children.Add(icoCanvas);
TextBlock newText1 = new TextBlock();
newText1.Name = "Message1";
newText1.Text = "Text1";
newText1.FontSize = 48;
newText1.Foreground = new SolidColorBrush(Colors.LightGray);
Canvas.SetLeft(newText1, 250);
TextBlock newText2 = new TextBlock();
newText2.Name = "Message2";
newText2.Text = "Text2";
newText2.FontSize = 36;
newText2.Foreground = new SolidColorBrush(Colors.Black);
Canvas.SetLeft(newText2, 200);
Canvas.SetTop(newText2, 50);
TextBlock newText3 = new TextBlock();
newText3.Name = "Message3";
newText3.Text = "Text3";
newText3.FontSize = 36;
newText3.Foreground = new SolidColorBrush(Colors.DarkGray);
Canvas.SetTop(newText3, 30);
Canvas.SetLeft(newText3, 10);
TextBlock newText4 = new TextBlock();
newText4.Name = "Message4";
newText4.Text = "Text4";
newText4.FontSize = 36;
newText4.Foreground = new SolidColorBrush(Colors.Gray);
Canvas.SetTop(newText4, 0);
newCanvas.Children.Add(newText1);
newCanvas.Children.Add(newText2);
newCanvas.Children.Add(newText3);
newCanvas.Children.Add(newText4);
Duration duration = new Duration(TimeSpan.FromSeconds(15));
Duration duration1 = new Duration(TimeSpan.FromSeconds(3));
Duration duration2 = new Duration(TimeSpan.FromSeconds(10));
DoubleAnimation myDoubleAnimation1 = new DoubleAnimation();
DoubleAnimation myDoubleAnimation2 = new DoubleAnimation();
DoubleAnimation myDoubleAnimation3 = new DoubleAnimation();
DoubleAnimation TextAnimation1 = new DoubleAnimation();
DoubleAnimation TextAnimation2 = new DoubleAnimation();
DoubleAnimation TextAnimation3 = new DoubleAnimation();
DoubleAnimation TextAnimation4 = new DoubleAnimation();
myDoubleAnimation1.Duration = duration1;
myDoubleAnimation2.Duration = duration1;
TextAnimation1.Duration = duration2;
TextAnimation2.Duration = duration2;
TextAnimation3.Duration = duration2;
TextAnimation4.Duration = duration2;
Storyboard sb = new Storyboard();
sb.Duration = duration;
sb.RepeatBehavior = RepeatBehavior.Forever;
sb.Children.Add(TextAnimation1);
sb.Children.Add(TextAnimation2);
sb.Children.Add(TextAnimation3);
sb.Children.Add(TextAnimation4);
//sb.Children.Add(myDoubleAnimation1);
//sb.Children.Add(myDoubleAnimation2);
//Storyboard.SetTarget(myDoubleAnimation1, newimage1);
//Storyboard.SetTarget(myDoubleAnimation2, newimage2);
Storyboard.SetTarget(TextAnimation1, newText1);
Storyboard.SetTarget(TextAnimation2, newText2);
Storyboard.SetTarget(TextAnimation3, newText3);
Storyboard.SetTarget(TextAnimation4, newText4);
//Storyboard.SetTargetProperty(myDoubleAnimation1, new PropertyPath("(Opacity)"));
//Storyboard.SetTargetProperty(myDoubleAnimation2, new PropertyPath("(Opacity)"));
Storyboard.SetTargetProperty(TextAnimation1, new PropertyPath("(Canvas.Left)"));
Storyboard.SetTargetProperty(TextAnimation2, new PropertyPath("(Opacity)"));
Storyboard.SetTargetProperty(TextAnimation3, new PropertyPath("(Canvas.Left)"));
Storyboard.SetTargetProperty(TextAnimation4, new PropertyPath("(Opacity)"));
//myDoubleAnimation1.From = 0.0;
//myDoubleAnimation1.To= 1.0;
//myDoubleAnimation1.BeginTime = TimeSpan.FromSeconds(3);
//myDoubleAnimation2.From = 0.5;
//myDoubleAnimation2.To = 0.0;
//myDoubleAnimation2.BeginTime = TimeSpan.FromSeconds(0);
TextAnimation1.From = 310;
TextAnimation1.To = 210;
TextAnimation1.BeginTime = TimeSpan.FromSeconds(0);
TextAnimation2.From = 0.0;
TextAnimation2.To = 1.0;
TextAnimation2.BeginTime = TimeSpan.FromSeconds(1);
TextAnimation3.From = 10;
TextAnimation3.To = 90;
TextAnimation3.BeginTime = TimeSpan.FromSeconds(2);
TextAnimation4.From = 0.0;
TextAnimation4.To = 1.0;
TextAnimation4.BeginTime = TimeSpan.FromSeconds(3);
LayoutRoot.Resources.Add("unique_id", sb);
sb.Begin();
Uri af = new Uri("http://www.yoururl.eu/audio.mp3", UriKind.Absolute);
me.Source = af;
me.Volume = 0.5;
}
void newimage1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (me.IsMuted)
me.IsMuted = false;
else
me.IsMuted = true;
}
}
}
Code is provided as is! Sorry for not indenting,not comment etc...
We found it very useful, let us know if solved something to you...