Phone7提供的控件都是一些基本的控件,这远远是无法满足开发需要的,这就需要用到这个用户控件,来自定义所需要的控件。比如:TreeView、Waiting等都是Phone7中没有的。另外这也是与silverlight不的地方,在silverlight中,所有的页面都是继承自UserControl,而Phone7的页面却是继承自PhoneApplicationPage。
下面制作一个有动画的简单的WaitingBox:
1. 创建:UserControl继承了UserControl这个控件类,也就是说控件该有的属性、方法就都有了,那自已需要的,就要自已扩展了。
2. 示例XAML:
<Popup x:Name="WaitingWnd" IsOpen="False">
<Grid x:Name="LayoutRoot" Background="Transparent">
<TextBlock Height="44" HorizontalAlignment="Left" Margin="0,144,0,0" Name="textBlock1" Text="请等待….." VerticalAlignment="Top" Foreground="Gray" Width="200" FontSize="32" TextAlignment="Center"/>
<Image Height="150" HorizontalAlignment="Left" Margin="22,6,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="150" />
</Grid>
</Popup>
3. 示例代码:
public partial class WaitingBox : UserControl
{
DispatcherTimer Timer = null;
int Count = 0;
public double Speed { get; set; }
public WaitingBox()
{
InitializeComponent();
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
Timer = new DispatcherTimer();
Timer.Interval = TimeSpan.FromMilliseconds(Speed);
Timer.Tick += new EventHandler(Timer_Tick);
}
void Timer_Tick(object sender, EventArgs e)
{
this.image1.Source = new BitmapImage(new Uri("Images/" + Count + ".png", UriKind.Relative));
Count =( Count == 7 ? 0 : Count+1);
}
public void WaitingBegin()
{
Timer.Start();
WaitingWnd.IsOpen = true;
}
public void WaitingEnd()
{
Timer.Stop();
WaitingWnd.IsOpen = false;
}
}
4. 调用:
<my:WaitingBox Height="200" HorizontalAlignment="Left" Margin="152,147,0,0" x:Name="waitingBox1" VerticalAlignment="Top" Width="200" Speed="100"/>
waitingBox1.WaitingBegin();
waitingBox1.WaitingEnd();