现在的位置: 首页 > 综合 > 正文

Windows Phone 7 开发小技巧

2013年01月29日 ⁄ 综合 ⁄ 共 3764字 ⁄ 字号 评论关闭

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控件什么时候滚到底的时候使用过该方法,这个方法很常用。

        //获取第一个子类型 
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>来扩展控件的各种自定义化的效果,当你需要在控件上实现一些动画的效果,或者在控件上再嵌入其他的一些控件都可以通过设计一个ControlTemplate来实现。

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

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

 

抱歉!评论已关闭.