silverlight插件|Silverlight动态加载菜单和菜单动画

时间:2017-06-02  来源:Silverlight  阅读:

Xaml代码: 容器这里改用StackPanel,外面嵌套ScrollViewer,内容超出后可以下拉

 代码如下


  
  

Xam.cs 代码:  T代表你定义的对象

 

 代码如下

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
    {
        ///


        /// 记录最后一次点击
        ///

        Canvas _LastCanvas = new Canvas();

        ///


        /// 动画图片
        ///

        Image Img_Story = new Image();

        ///


        /// 定义白色
        ///

        SolidColorBrush WhiteColorBrush = new SolidColorBrush();

        ///


        /// 定义灰色
        ///

        SolidColorBrush OtherColorBrush = new SolidColorBrush();

        public MainPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            //设置颜色
            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() + ":测试程序"; //你自己重命名
                Canvas.SetLeft(_TextBlock, 30); //30是距canvas左边,你自己可以调整
                Canvas.SetZIndex(_TextBlock, 100); //设置图层,防止文本被遮盖
                _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是距canvas左边,你自己可以调整
                    Canvas.SetZIndex(Img_Story, 0); //设置图层,防止文本被遮盖
                    _Canvas.Children.Add(this.Img_Story);
                    this._LastCanvas = _Canvas;
                    _TextBlock.Foreground = WhiteColorBrush;
                }
                else
                {
                    _TextBlock.Foreground = OtherColorBrush;
                }
                //点击事件
                _TextBlock.MouseLeftButtonDown += new MouseButtonEventHandler(_TextBlock_MouseLeftButtonDown);
                //将元素添加到容器中
                this.Panel_Element.Children.Add(_Canvas);
            }
        }

        ///


        /// 点击事件
        ///

        ///
        ///
        void _TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            TextBlock Txt_Road = sender as TextBlock;
            //检索页面元素,设置文字颜色
            foreach (FrameworkElement fe in this.Panel_Element.Children)
            {
                if (fe is Canvas)    //检索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;   //当前点击的设为白色
                        CreateStoryBoard();
                    }
                    else
                    {
                        chk.Foreground = OtherColorBrush;   //其他的改变回原来默认颜色
                    }
                }
            }
        }

        ///


        /// 创建点击动画
        ///

        private void CreateStoryBoard()
        {
            Storyboard storyboard = new Storyboard();   //实例化Storyboard
            //实例化X轴动画对象
            DoubleAnimation doubleAnimationX = new DoubleAnimation();
            doubleAnimationX.Duration = new Duration(TimeSpan.FromMilliseconds(500));   //设置动画延时时间
            doubleAnimationX.From = 0;     //设置动画初始值
            doubleAnimationX.To = 10;    //设置动画完成值
            Storyboard.SetTarget(doubleAnimationX, this.Img_Story);     //设置动画操作对象
            Storyboard.SetTargetProperty(doubleAnimationX, new PropertyPath("(Canvas.Left)"));      //设置动画操作对象的属性
            storyboard.Children.Add(doubleAnimationX);      //将动画加载到Storyboard
            //开始动画
            storyboard.Begin();
        }
    }
}

silverlight插件|Silverlight动态加载菜单和菜单动画

http://m.bbyears.com/asp/33079.html

推荐访问:silverlight安装失败 silverlight什么意思
相关阅读 猜你喜欢
本类排行 本类最新