Windows Phone 7 开发小技巧

简介:

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为偶数的时候,下面还多了一大截的空间很影响美观(为奇数的时候就不会多出这一大截的空间,不知道微软为何要这样设计),大家有没有相关的解决方法呢?

 

 

 

 

 


本文转自linzheng 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1078471

相关文章
|
9月前
|
IDE 关系型数据库 开发工具
使用Visual Basic进行Windows窗体开发
【4月更文挑战第27天】本文介绍了使用Visual Basic进行Windows窗体(WinForms)开发的步骤,从搭建开发环境到创建、设计用户界面,再到编写事件驱动的代码和数据绑定。Visual Basic结合WinForms提供了一种易学易用的桌面应用开发方案。通过调试、优化、部署和维护,开发者可以构建专业应用程序。随着技术发展,掌握最新UI设计和开发工具对于保持竞争力至关重要。本文为初学者提供了基础指导,鼓励进一步探索和学习。
268 0
|
4月前
|
监控 关系型数据库 MySQL
PowerShell 脚本编写 :自动化Windows 开发工作流程
PowerShell 脚本编写 :自动化Windows 开发工作流程
161 0
|
4月前
|
Ubuntu Linux Python
如何利用wsl-Ubuntu里conda用来给Windows的PyCharm开发
如何在WSL(Windows Subsystem for Linux)的Ubuntu环境中使用conda虚拟环境来为Windows上的PyCharm开发设置Python解释器。
378 0
|
5月前
|
存储 安全 程序员
Windows任务管理器开发原理与实现
Windows任务管理器开发原理与实现
|
7月前
|
Linux Apache C++
FFmpeg开发笔记(三十五)Windows环境给FFmpeg集成libsrt
该文介绍了如何在Windows环境下为FFmpeg集成SRT协议支持库libsrt。首先,需要安装Perl和Nasm,然后编译OpenSSL。接着,下载libsrt源码并使用CMake配置,生成VS工程并编译生成srt.dll和srt.lib。最后,将编译出的库文件和头文件按照特定目录结构放置,并更新环境变量,重新配置启用libsrt的FFmpeg并进行编译安装。该过程有助于优化直播推流的性能,减少卡顿问题。
170 2
FFmpeg开发笔记(三十五)Windows环境给FFmpeg集成libsrt
|
6月前
|
开发者 C# Windows
WPF与游戏开发:当桌面应用遇见游戏梦想——利用Windows Presentation Foundation打造属于你的2D游戏世界,从环境搭建到代码实践全面解析新兴开发路径
【8月更文挑战第31天】随着游戏开发技术的进步,WPF作为.NET Framework的一部分,凭借其图形渲染能力和灵活的UI设计,成为桌面游戏开发的新选择。本文通过技术综述和示例代码,介绍如何利用WPF进行游戏开发。首先确保安装最新版Visual Studio并创建WPF项目。接着,通过XAML设计游戏界面,并在C#中实现游戏逻辑,如玩家控制和障碍物碰撞检测。示例展示了创建基本2D游戏的过程,包括角色移动和碰撞处理。通过本文,WPF开发者可更好地理解并应用游戏开发技术,创造吸引人的桌面游戏。
306 0
|
6月前
|
开发者 iOS开发 C#
Uno Platform 入门超详细指南:从零开始教你打造兼容 Web、Windows、iOS 和 Android 的跨平台应用,轻松掌握 XAML 与 C# 开发技巧,快速上手示例代码助你迈出第一步
【8月更文挑战第31天】Uno Platform 是一个基于 Microsoft .NET 的开源框架,支持使用 C# 和 XAML 构建跨平台应用,适用于 Web(WebAssembly)、Windows、Linux、macOS、iOS 和 Android。它允许开发者共享几乎全部的业务逻辑和 UI 代码,同时保持原生性能。选择 Uno Platform 可以统一开发体验,减少代码重复,降低开发成本。安装时需先配置好 Visual Studio 或 Visual Studio for Mac,并通过 NuGet 或官网下载工具包。
617 0
|
8月前
|
网络安全 C++ Windows
【Windows驱动开发】(主机)VS2017+(虚拟机)win10系统------双机调试
【Windows驱动开发】(主机)VS2017+(虚拟机)win10系统------双机调试
|
8月前
|
Windows
【Windows驱动开发】注册表的基本操作(创建、打开、修改、读取、枚举)(附源码)
【Windows驱动开发】注册表的基本操作(创建、打开、修改、读取、枚举)(附源码)
|
8月前
|
编解码 Windows
FFmpeg开发笔记(二十九)Windows环境给FFmpeg集成libxvid
XviD是开源MPEG-4视频编码器,与DivX相似但后者非开源。早期MP4常使用XviD或DivX编码,现已被H.264取代。在Windows上集成FFmpeg的XviD编解码库libxvid,需访问<https://labs.xvid.com/source/>下载源码,解压后在MSYS环境中配置、编译和安装。之后重新配置FFmpeg,启用libxvid并编译安装。详细步骤包括configure命令、make和make install。成功后,通过`ffmpeg -version`检查是否启用libxvid。更多音视频开发技术可参考《FFmpeg开发实战:从零基础到短视频上线》。
156 0
FFmpeg开发笔记(二十九)Windows环境给FFmpeg集成libxvid