Windows Phone 7开发小技巧

移动开发
本文介绍了六种Windows Phone 7开发小技巧,其中包括使用Popup来实现自定义的弹出效果、捕获物理按键返回键,打开页面,离开页面等等。

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

Popup使用的方法:

  1. private Popup popup;  
  2. popup = new Popup();  
  3. popup.Child = new 控件类();  
  4. //打开  
  5. popup.IsOpen = true;  
  6. //关闭  
  7. popup.IsOpen = false 

或者(xaml代码)

  1. <Popup x:Name="popup"> 
  2.     <Border> 
  3.        <StackPanel> 
  4.             ……  
  5.         </StackPanel> 
  6.     </Border> 
  7. </Popup> 

或者(cs代码)

  1. //打开  
  2. popup.IsOpen = true;  
  3. //关闭  
  4. popup.IsOpen = false 

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

  1. <TextBlock TextWrapping="Wrap"> 
  2.           测试            
  3.    <LineBreak></LineBreak> 
  4.    <LineBreak></LineBreak> 
  5.           测试  
  6.    <LineBreak></LineBreak> 
  7.    <LineBreak></LineBreak> 
  8.           测试  
  9. </TextBlock> 

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

  1. //点击返回按钮  
  2. protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)    
  3. {  
  4.     //你的代码  
  5.     e.Cancel = false;             
  6.     base.OnBackKeyPress(e);    
  7. }   
  8. //从其他页面进入该页面  
  9. protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)  
  10. {  
  11.      //你的代码  
  12.      base.OnNavigatedTo(e);  
  13. }  
  14. //离开当前的页面  
  15. protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)  
  16. {  
  17.    //你的代码  
  18.    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.显示和隐藏手机的顶部托盘,就是顶部那个信号和电池信息那块东西。

  1. //显示  
  2. SystemTray.IsVisible = true;  
  3. //隐藏  
  4. SystemTray.IsVisible = false

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

 

 

原文链接http://www.cnblogs.com/linzheng/archive/2011/10/27/2226990.html

责任编辑:王晓东 来源: 博客园
相关推荐

2013-04-17 10:45:26

Windows PhoWindows Pho

2012-06-25 16:14:26

Windows Pho

2013-07-30 11:18:37

Windows PhoWindows Pho

2011-06-07 11:35:38

Windows Pho

2010-12-14 18:48:49

微软

2010-04-08 17:40:23

Windows Pho

2010-08-13 08:21:11

Windows Pho

2010-03-09 10:51:15

Windows Pho

2013-07-31 13:36:07

Windows PhoVS调试技巧Windows Pho

2010-07-21 14:42:15

Windows Pho

2011-03-30 11:21:41

Windows Pho开发大赛

2013-04-17 10:24:29

Windows Pho

2009-11-06 18:58:21

Windows 7双系统卸载

2010-12-16 10:06:31

Windows Pho

2012-08-02 10:16:39

Windows Pho

2010-08-10 13:21:41

Windows PhoWindows Pho

2010-12-21 10:02:48

SilverlightWindows Pho

2012-06-13 13:01:57

Windows Pho

2010-11-03 15:10:04

SilverlightSilverlightWindows Pho

2010-08-16 09:21:35

Windows Pho
点赞
收藏

51CTO技术栈公众号