1.使用Popup来实现自定义的弹出效果。Popup控件弹出的块会一直在屏幕的最前方,所以使用Popup可以实现各种各样的弹出框,并且给了你极大的自定义的空间,很多第三方的弹出框控件的原理其实就是使用了Popup来包装上各种效果来实现的。

Popup使用的方法:
private Popup popup;
popup = new Popup();
popup.Child = new 控件类();
//打开
popup.IsOpen = true;
//关闭
popup.IsOpen = false

或者
xaml代码
<Popup x:Name="popup">
<Border>
<StackPanel>
……
</StackPanel>
</Border>
</Popup>

cs代码
//打开
popup.IsOpen = true;
//关闭
popup.IsOpen = false


2.在TextBlock控件中使用<LineBreak></LineBreak>进行换行。

<TextBlock TextWrapping="Wrap">
测试
<LineBreak></LineBreak>
<LineBreak></LineBreak>
测试
<LineBreak></LineBreak>
<LineBreak></LineBreak>
测试
</TextBlock>

 

3.捕获物理按键返回键,打开页面,离开页面。windows phone有3个物理按键,返回键,开始键,搜索键,后面两个无法在程序中捕获到。

//点击返回按钮
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
//你的代码
e.Cancel = false;
base.OnBackKeyPress(e);
}
//从其他页面进入该页面
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
//你的代码
base.OnNavigatedTo(e);
}
//离开当前的页面
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
//你的代码
base.OnNavigatedFrom(e);
}


4.获取父控件里面的子控件的方法。之前在判断ListBox控件什么时候滚到底的时候使用过该方法,这个方法很常用。

 

  1. //获取第一个子类型   
  2.         public static T FindChildOfType<T>(DependencyObject root) where T : class  
  3.         {  
  4.             var queue = new Queue<DependencyObject>();  
  5.             queue.Enqueue(root);  
  6.             while (queue.Count > 0)  
  7.             {  
  8.                 DependencyObject current = queue.Dequeue();  
  9.                 for (int i = VisualTreeHelper.GetChildrenCount(current) - 1; 0 <= i; i--)  
  10.                 {  
  11.                     var child = VisualTreeHelper.GetChild(current, i);  
  12.                     var typedChild = child as T;  
  13.                     if (typedChild != null)  
  14.                     {  
  15.                         return typedChild;  
  16.                     }  
  17.                     queue.Enqueue(child);  
  18.                 }  
  19.             }  
  20.             return null;  
  21.         }  
  22.  
  23.         //获取所有的子类型  
  24.         public static List<T> FindAllChildOfType<T>(DependencyObject root) where T : class  
  25.         {  
  26.             var queue = new Queue<DependencyObject>();  
  27.             queue.Enqueue(root);  
  28.             List<T> allChild = new List<T>();  
  29.             while (queue.Count > 0)  
  30.             {  
  31.                 DependencyObject current = queue.Dequeue();  
  32.                 for (int i = VisualTreeHelper.GetChildrenCount(current) - 1; 0 <= i; i--)  
  33.                 {  
  34.                     var child = VisualTreeHelper.GetChild(current, i);  
  35.                     var typedChild = child as T;  
  36.                     if (typedChild != null)  
  37.                     {  
  38.                         allChild.Add(typedChild);  
  39.                     }  
  40.                     queue.Enqueue(child);  
  41.                 }  
  42.             }  
  43.             return allChild;  
  44.         } 

5. 使用<ControlTemplate>……</ControlTemplate>来扩展控件的各种自定义化的效果,当你需要在控件上实现一些动画的效果,或者在控件上再嵌入其他的一些控件都可以通过设计一个ControlTemplate来实现。

如实现一个按钮的单击效果:

 

  1. <Button Content="Button" Height="72" HorizontalAlignment="Left" Margin="103,197,0,0" Name="button1" VerticalAlignment="Top" Width="160"> 
  2.                 <Button.Template> 
  3.                     <ControlTemplate> 
  4.                         <Grid Background="Transparent"> 
  5.                             <VisualStateManager.VisualStateGroups> 
  6.                                 <VisualStateGroup x:Name="CommonStates"> 
  7.                                     <VisualState x:Name="Pressed"> 
  8.                                         <Storyboard> 
  9.                                             <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ButtonBackground" Storyboard.TargetProperty="Background"> 
  10.                                                 <DiscreteObjectKeyFrame KeyTime="0" Value="YellowGreen"/> 
  11.                                             </ObjectAnimationUsingKeyFrames> 
  12.                                             <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ButtonBackground" Storyboard.TargetProperty="BorderBrush"> 
  13.                                                 <DiscreteObjectKeyFrame KeyTime="0" Value="YellowGreen"/> 
  14.                                             </ObjectAnimationUsingKeyFrames> 
  15.                                         </Storyboard> 
  16.                                     </VisualState> 
  17.                                 </VisualStateGroup> 
  18.                             </VisualStateManager.VisualStateGroups> 
  19.                             <Border x:Name="ButtonBackground" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"  Background="{TemplateBinding Background}" Margin="{StaticResource PhoneTouchTargetOverhang}"> 
  20.                                 <ContentControl x:Name="ContentContainer" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" Padding="{TemplateBinding Padding}" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/> 
  21.                             </Border> 
  22.                         </Grid> 
  23.                     </ControlTemplate> 
  24.                 </Button.Template> 
  25.             </Button> 

6.显示和隐藏手机的顶部托盘,就是顶部那个信号和电池信息那块东西。
//显示
SystemTray.IsVisible = true;
//隐藏
SystemTray.IsVisible = false;

 

遇到一个问题:ApplicationBar的高度无法自定义,当ApplicationBarMenuItem为偶数的时候,下面还多了一大截的空间很影响美观(为奇数的时候就不会多出这一大截的空间,不知道微软为何要这样设计),大家有没有相关的解决方法呢?

 

Windows Phone 7 开发小技巧 _Windows Phone 7  

 

Windows Phone 7 开发小技巧 _开发小技巧 _02