[Winodows Phone 7控件详解]UserControl

前面介绍的Silverlight toolkit for Windows Phone 7.1控件本质上也是用户自定义控件,我们可以根据自己的需要自己定义一些控件。

下面制作一个有动画的简单的WaitingBox:

1.       创建:UserControl继承了UserControl这个控件类,也就是说控件该有的属性、方法就都有了,那自已需要的,就要自已扩展了。

xaml:

    <Popup x:Name="WaitingWnd" IsOpen="True" >
        <Grid Width="300" Height="400" Background="Transparent"   x:Name="LayoutRoot" >
        <Image Height="300" Width="300" HorizontalAlignment="Left" Name="image1" Stretch="Fill" VerticalAlignment="Top"  />
        <TextBlock Height="30" HorizontalAlignment="Left" Margin="106,314,0,0" Name="textBlock1" Text="请等待..." VerticalAlignment="Top" />
    </Grid>
    </Popup>

一般把容器控件的背景色设为:透明色(Background="Transparent")

cs:

public partial class WindowsPhoneControl1 : UserControl
    {
        DispatcherTimer timer = new DispatcherTimer();
        int count = 0;
        public double Speed { get; set; }

        public WindowsPhoneControl1()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {           
            timer.Interval = TimeSpan.FromMilliseconds(Speed);
            timer.Tick += new EventHandler(timer_Tick);
        }

        void timer_Tick(object sender, EventArgs e)
        {
            image1.Source = new BitmapImage(new Uri("Images/" + count + ".png", UriKind.Relative));
            count=(count==7?0:count+1);
        }

        public void WaitingBegin()
        {
            if (timer == null)
            {
                timer = new DispatcherTimer();
            }
            timer.Start();
            WaitingWnd.IsOpen = true;
        }

        public void WaitingEnd()
        {
            timer.Stop();
            WaitingWnd.IsOpen = false;
        }
    }

自己扩展了一个Speed属性,在使用的时候记得设置。

编译完成后,就可以在“工具栏”中看到刚刚定义好的控件。

使用:拖拽即可,设置Speed属性。

        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <my:WindowsPhoneControl1 Speed="200" HorizontalAlignment="Left" Margin="82,96,0,0" x:Name="windowsPhoneControl11" VerticalAlignment="Top" Height="400" Width="300" />
        </Grid>

windowsPhoneControl11.WaitingBegin();//

waitingBox1.WaitingEnd();

万维网是个好东西,图片来自深蓝色右手,这个Animation以前做Silverlight游戏开发的时候做过,方便就直接拿过来用了~也感谢小振的无私奉献,共同学习共同进步嘛~

 

原文地址:https://www.cnblogs.com/DebugLZQ/p/2439172.html