Windows Phone 开发小技巧

1、设置应用程序的图标

右击Wp7应用程序的解决方案,选择属性,设置Icon为自己想要的图片,如下图

并且要确保图片的build方式为Content,如下图所示:

2、 Windows Phone 7中的启动画面

默认情况下,Windows Phone 7 应用程序会使用如下的默认图片作为程序的启动画面,我们可以自己设置Windows Phone 7中的启动画面,可以设置为:1.使用自己的图片作为启动画面 2. 带动画效果的启动画面 3.不使用启动画面

对于1,我们只要将SplashScreenImage替换为自己的图片即可。而对于3,如果不想使用启动画面,我们只需将SplashScreenImage图片移出项目即可。下面着重讲一下如果实现动画启动画面.

这里使用BackgroundWorker类来实现,BackgroundWorker类是开辟一个后台线程来处理一些操作而同时你的UI也能继续响应用户操作的类。更多的关于BackgroundWorker类大家可以Google其用法。在WP7的渲染线程中,如果你想要一个持续响应的用户界面,那么BackgroundWorker类会变得很有用。你可以监听你想要做的操作的进程的事件以及操作完成的信号。我们使用RunWorkerAsync开启后台操作。
注意:我们不应该在BackgroundWorker类的DoWork去操作用户界面。我们可以在ProgressChanged 和RunWorkerCompleted事件中去操作与用户界面相关的操作。关于更多的可以参考MSDN http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker%28VS.95%29.aspx

下面开始创建动画的启动动画的步骤
1. 创建WP7项目,并添加名为AnimatedSplashScreen.xaml的用户控件(UserControl)
2. 在MainPage.xaml.cs添加如下的命名空间,并且编辑后置代码如下

using System.Threading;

using System.Windows.Controls.Primitives;

public partial class MainPage : PhoneApplicationPage
{
BackgroundWorker backroungWorker;
Popup myPopup;
// Constructor
public MainPage()
{
InitializeComponent();
myPopup = new Popup() { IsOpen = true, Child = new AnimatedSplashScreen() };
backroungWorker = new BackgroundWorker();
RunBackgroundWorker();

}

private void RunBackgroundWorker()
{
backroungWorker.DoWork += ((s, args) =>
{
Thread.Sleep(5000);
});

backroungWorker.RunWorkerCompleted += ((s, args) =>
{
this.Dispatcher.BeginInvoke(() =>
{
this.myPopup.IsOpen = false;
}
);
});
backroungWorker.RunWorkerAsync();
}
}

3. 编辑AnimatedSplashScreen.xaml前台代码如下

<StackPanel x:Name="LayoutRoot" Background="Black" Height="800" Width="480">
<TextBlock Text="WindowsPhoneGeek Sample Splash Screen" x:Name="text" Foreground="Green" FontSize="65" TextWrapping="Wrap" Margin="0,20,0,0"/>
<Image Source="logo.png" x:Name="logoImage" Stretch="None" Margin="0,0,0,50">
<Image.Projection>
<PlaneProjection/>
</Image.Projection>
</Image>
<toolkit:PerformanceProgressBar IsIndeterminate="True" Foreground="Green"/>
</StackPanel>

并且添加如下的动画资源

<UserControl.Resources>
<Storyboard x:Key="flippingAnimation" >
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.RotationX)" Storyboard.TargetName="logoImage">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:1" Value="1"/>
<EasingDoubleKeyFrame KeyTime="0:0:2" Value="360"/>
</DoubleAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="text">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<SolidColorBrush Color="White"/>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
<DiscreteObjectKeyFrame KeyTime="0:0:2">
<DiscreteObjectKeyFrame.Value>
<SolidColorBrush Color="Green"/>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</UserControl.Resources>

4. 最后编辑其后置代码如下

public AnimatedSplashScreen()
{
InitializeComponent();
Storyboard flippingAnimation = this.Resources["flippingAnimation"]as Storyboard;
flippingAnimation.Begin();
}

3、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


4、在TextBlock控件中使用<LineBreak></LineBreak>进行换行。

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

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

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


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

       //获取第一个子类型
        public static TFindChildOfType<T>(DependencyObject root) whereT : class
        {
            varqueue = 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 asT;
                    if (typedChild != null)
                    {
                        return typedChild;
                    }
                    queue.Enqueue(child);
                }
            }
            returnnull;
        }

        //获取所有的子类型
        public staticList<T> FindAllChildOfType<T>(DependencyObject root) where T : class
        {
            varqueue = 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 asT;
                    if (typedChild != null)
                    {
                       allChild.Add(typedChild);
                    }
                    queue.Enqueue(child);
                }
            }
            returnallChild;
        }

7、使用<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>
                                           <ObjectAnimationUsingKeyFramesStoryboard.TargetName="ButtonBackground"Storyboard.TargetProperty="Background">
                                               <DiscreteObjectKeyFrameKeyTime="0" Value="YellowGreen"/>
                                           </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ButtonBackground" Storyboard.TargetProperty="BorderBrush">
                                               <DiscreteObjectKeyFrameKeyTime="0" Value="YellowGreen"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <Border x:Name="ButtonBackground" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBindingBorderThickness}"  Background="{TemplateBindingBackground}" Margin="{StaticResource PhoneTouchTargetOverhang}">
                                <ContentControl x:Name="ContentContainer" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" Padding="{TemplateBindingPadding}"Content="{TemplateBindingContent}"ContentTemplate="{TemplateBinding ContentTemplate}"/>
                            </Border>
                        </Grid>
                    </ControlTemplate>
                </Button.Template>
            </Button>

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Windows Phone是由微软推出的移动操作系统,它提供了丰富的开发工具和教程,使开发者可以轻松地创建各种类型的应用程序。下面是Windows Phone开发教程的一些步骤: 1. 准备开发环境:首先,你需要安装Windows Phone SDK,并且拥有一台运行Windows 8或更高版本的电脑。然后,你需要注册一个开发者账户,这样你就可以在Windows Phone商店中发布你的应用程序了。 2. 学习开发语言:Windows Phone应用程序主要使用C#语言进行开发,因此你需要熟悉C#语言的基本语法和特性。同时,你也需要了解XAML标记语言,用于设计应用程序的用户界面。 3. 使用Visual Studio开发工具:Windows Phone开发主要使用Visual Studio作为集成开发环境,它提供了丰富的工具和模板,帮助你创建各种类型的应用程序,包括游戏、商务应用和社交应用等。 4. 学习应用程序的生命周期:你需要了解Windows Phone应用程序的生命周期和事件模型,以便正确地处理应用程序的启动、挂起和恢复等状态。 5. 学习应用程序的数据存储和互联网连接:在开发Windows Phone应用程序时,你可能需要将数据保存到本地数据库或者通过网络连接获取数据,因此你需要学习使用本地存储和网络连接的相关知识。 总之,学习Windows Phone开发需要你掌握C#语言和XAML标记语言,熟悉Visual Studio开发工具,并且了解应用程序的生命周期和数据存储等知识。通过不断的实践和学习,你可以成为一名优秀的Windows Phone开发者。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值