Windows Phone 7開發(fā)小技巧
1.使用Popup來實現(xiàn)自定義的彈出效果。Popup控件彈出的塊會一直在屏幕的最前方,所以使用Popup可以實現(xiàn)各種各樣的彈出框,并且給了你極大的自定義的空間,很多第三方的彈出框控件的原理其實就是使用了Popup來包裝上各種效果來實現(xiàn)的。
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.捕獲物理按鍵返回鍵,打開頁面,離開頁面。Wndows Pone有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控件什么時候滾到底的時候使用過該方法,這個方法很常用。
- //獲取***個子類型
- public static T FindChildOfType<T>(DependencyObject root) where T : class
- {
- var queue = new Queue<DependencyObject>();
- queue.Enqueue(root);
- while (queue.Count > 0)
- {
- DependencyObject current = queue.Dequeue();
- for (int i = VisualTreeHelper.GetChildrenCount(current) - 1; 0 <= i; i--)
- {
- var child = VisualTreeHelper.GetChild(current, i);
- var typedChild = child as T;
- if (typedChild != null)
- {
- return typedChild;
- }
- queue.Enqueue(child);
- }
- }
- return null;
- }
- //獲取所有的子類型
- public static List<T> FindAllChildOfType<T>(DependencyObject root) where T : class
- {
- var queue = new Queue<DependencyObject>();
- queue.Enqueue(root);
- List<T> allChild = new List<T>();
- while (queue.Count > 0)
- {
- DependencyObject current = queue.Dequeue();
- for (int i = VisualTreeHelper.GetChildrenCount(current) - 1; 0 <= i; i--)
- {
- var child = VisualTreeHelper.GetChild(current, i);
- var typedChild = child as T;
- if (typedChild != null)
- {
- allChild.Add(typedChild);
- }
- queue.Enqueue(child);
- }
- }
- return allChild;
- }
5.使用<ControlTemplate>……</ControlTemplate>來擴展控件的各種自定義化的效果,當你需要在控件上實現(xiàn)一些動畫的效果,或者在控件上再嵌入其他的一些控件都可以通過設計一個ControlTemplate來實現(xiàn)。
如實現(xiàn)一個按鈕的單擊效果:
- <Button Content="Button" Height="72" HorizontalAlignment="Left" Margin="103,197,0,0" Name="button1" VerticalAlignment="Top" Width="160">
- <Button.Template>
- <ControlTemplate>
- <Grid Background="Transparent">
- <VisualStateManager.VisualStateGroups>
- <VisualStateGroup x:Name="CommonStates">
- <VisualState x:Name="Pressed">
- <Storyboard>
- <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ButtonBackground" Storyboard.TargetProperty="Background">
- <DiscreteObjectKeyFrame KeyTime="0" Value="YellowGreen"/>
- </ObjectAnimationUsingKeyFrames>
- <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ButtonBackground" Storyboard.TargetProperty="BorderBrush">
- <DiscreteObjectKeyFrame KeyTime="0" Value="YellowGreen"/>
- </ObjectAnimationUsingKeyFrames>
- </Storyboard>
- </VisualState>
- </VisualStateGroup>
- </VisualStateManager.VisualStateGroups>
- <Border x:Name="ButtonBackground" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Margin="{StaticResource PhoneTouchTargetOverhang}">
- <ContentControl x:Name="ContentContainer" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" Padding="{TemplateBinding Padding}" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/>
- </Border>
- </Grid>
- </ControlTemplate>
- </Button.Template>
- </Button>
6.顯示和隱藏手機的頂部托盤,就是頂部那個信號和電池信息那塊東西。
- //顯示
- SystemTray.IsVisible = true;
- //隱藏
- SystemTray.IsVisible = false;
遇到一個問題:ApplicationBar的高度無法自定義,當ApplicationBarMenuItem為偶數(shù)的時候,下面還多了一大截的空間很影響美觀(為奇數(shù)的時候就不會多出這一大截的空間,不知道微軟為何要這樣設計),大家有沒有相關的解決方法呢?
原文鏈接:http://www.cnblogs.com/linzheng/archive/2011/10/27/2226990.html