Silverlight dynamically loads menus and menu animations

Source: Internet
Author: User

This article shares the code snippet of the Silverlight dynamic loading menu. If you need it, you can refer to it.

Xaml code: StackPanel is used for containers. ScrollViewer is nested outside. If the content is exceeded, drop-down is allowed.

The Code is as follows: Copy code

<ScrollViewer x: Name = "scrolls" VerticalScrollBarVisibility = "Auto" HorizontalScrollBarVisibility = "Auto">
<StackPanel x: Name = "Panel_Road" Margin = "0, 10">
</StackPanel>
</ScrollViewer>

Xam. cs code: T represents the object you defined

 

The Code is as follows: Copy code

Using System;
Using System. Windows;
Using System. Windows. Controls;
Using System. Windows. Input;
Using System. Windows. Media;
Using System. Windows. Media. Animation;
Using Microsoft. Phone. Controls;
Using System. Windows. Media. Imaging;

Namespace PhoneTest1
{
Public partial class MaainPage: PhoneApplicationPage
{
/// <Summary>
/// Record the last click
/// </Summary>
Canvas _ LastCanvas = new Canvas ();

/// <Summary>
/// Animated Image
/// </Summary>
Image Img_Story = new Image ();

/// <Summary>
/// Definition white
/// </Summary>
SolidColorBrush WhiteColorBrush = new SolidColorBrush ();

/// <Summary>
/// Defines gray
/// </Summary>
SolidColorBrush OtherColorBrush = new SolidColorBrush ();

Public MainPage ()
{
InitializeComponent ();
This. Loaded + = new RoutedEventHandler (MainPage_Loaded );
}

Void MainPage_Loaded (object sender, RoutedEventArgs e)
{
// Set the color
WhiteColorBrush. Color = Color. FromArgb (255,255,255,255 );
OtherColorBrush. Color = Color. FromArgb (255, 61, 61, 61 );
Boolean _ IsFirstAddElement = false;
For (int I = 0; I <10; I ++)
{
Canvas _ Canvas = new Canvas ();
_ Canvas. Height = 60;
TextBlock _ TextBlock = new TextBlock ();
_ TextBlock. Text = I. ToString () + ": Test Program"; // rename it by yourself
Canvas. SetLeft (_ TextBlock, 30); // 30 is left of the canvas. you can adjust it yourself.
Canvas. SetZIndex (_ TextBlock, 100); // sets the layer to prevent text from being overwritten.
_ TextBlock. FontSize = 30;
_ Canvas. Children. Add (_ TextBlock );
If (! _ IsFirstAddElement)
{
_ IsFirstAddElement = true;
This. Img_Story.Source = new BitmapImage (new Uri ("/PhoneTest1; component/Image/Test. PNG", UriKind. RelativeOrAbsolute ));
Canvas. SetLeft (Img_Story, 0); // 30 is left of the canvas. you can adjust it yourself.
Canvas. SetZIndex (Img_Story, 0); // sets the layer to prevent text from being overwritten.
_ Canvas. Children. Add (this. Img_Story );
This. _ LastCanvas = _ Canvas;
_ TextBlock. Foreground = WhiteColorBrush;
}
Else
{
_ TextBlock. Foreground = OtherColorBrush;
}
// Click an event
_ TextBlock. MouseLeftButtonDown + = new MouseButtonEventHandler (_ TextBlock_MouseLeftButtonDown );
// Add the element to the container
This. Panel_Element.Children.Add (_ Canvas );
}
}

/// <Summary>
/// Click Event
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Void _ TextBlock_MouseLeftButtonDown (object sender, MouseButtonEventArgs e)
{
TextBlock Txt_Road = sender as TextBlock;
// Retrieve the page element and set the text color
Foreach (FrameworkElement fe in this. Panel_Element.Children)
{
If (fe is Canvas) // retrieves TextBlock
{
Canvas _ Canvas = (Canvas) fe;
TextBlock chk = (TextBlock) _ Canvas. Children [0];
If (chk. Equals (Txt_Road ))
{
_ LastCanvas. Children. Remove (this. Img_Story );
_ Canvas. Children. Add (this. Img_Story );
This. _ LastCanvas = _ Canvas;
Chk. Foreground = WhiteColorBrush; // set the current click to white
CreateStoryBoard ();
}
Else
{
Chk. Foreground = OtherColorBrush; // other changes back to the original default color
}
}
}
}

/// <Summary>
/// Create a click Animation
/// </Summary>
Private void CreateStoryBoard ()
{
Storyboard storyboard = new Storyboard (); // instantiate Storyboard
// Instantiate the X axis animation object
DoubleAnimation doubleAnimationX = new DoubleAnimation ();
DoubleAnimationX. Duration = new Duration (TimeSpan. FromMilliseconds (500); // sets the animation delay time.
DoubleAnimationX. From = 0; // sets the animation initial value.
DoubleAnimationX. To = 10; // sets the animation completion value.
Storyboard. SetTarget (doubleAnimationX, this. Img_Story); // sets the animation operation object
Storyboard. SetTargetProperty (doubleAnimationX, new PropertyPath ("(Canvas. Left)"); // you can specify attributes of an animation operation object.
Storyboard. Children. Add (doubleAnimationX); // load the animation to the Storyboard
// Start the animation
Storyboard. Begin ();
}
}
}

Related Article

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.