原文:与众不同 windows phone (1) - Hello Windows Phone

[索引页]

[源码下载]

与众不同 windows phone (1) - Hello Windows Phone

作者:webabcd

介绍
与众不同 windows phone 7.5 (sdk 7.1)

  • 使用 Silverlight 开发 Windows Phone 应用程序
  • 使用 XNA 开发 Windows Phone 应用程序
  • 使用 Silverlight 和 XNA 组合开发 Windows Phone 应用程序(在 Silverlight 中融入 XNA)

示例
1、使用 Silverlight 开发 Windows Phone App 的 Demo
MainPage.xaml

<phone:PhoneApplicationPage
x:Class="Silverlight.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True"> <StackPanel>
<!--按钮-->
<Button Name="btn" Content="hello webabcd" />
</StackPanel> </phone:PhoneApplicationPage>

MainPage.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls; namespace Silverlight
{
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{
InitializeComponent(); this.Loaded += new RoutedEventHandler(MainPage_Loaded);
} void MainPage_Loaded(object sender, RoutedEventArgs e)
{
// 弹出 MessageBox 信息
btn.Click += delegate { MessageBox.Show("hello webabcd"); };
}
}
}

2、使用 XNA 开发 Windows Phone App 的 Demo
Game1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media; namespace XNA
{
// 启动时先 Initialize,再 LoadContent,退出时 UnloadContent
public class Game1 : Microsoft.Xna.Framework.Game
{
// 图形设备(显卡)管理器,XNA 在游戏窗口上做的所有事情都要通过此对象
GraphicsDeviceManager graphics; // 精灵绘制器
SpriteBatch spriteBatch; // 2D 纹理对象
Texture2D sprite; public Game1()
{
graphics = new GraphicsDeviceManager(this); // 指定游戏窗口的宽和高,不设置的话会花屏
graphics.PreferredBackBufferWidth = this.Window.ClientBounds.Width;
graphics.PreferredBackBufferHeight = this.Window.ClientBounds.Height; Content.RootDirectory = "Content"; // 两次绘制的间隔时间,本例为每 1/30 秒绘制一次,即帧率为 30 fps。此属性默认值为 60 fps
TargetElapsedTime = TimeSpan.FromSeconds(1f / ); // 当禁止在锁屏状态下运行应用程序空闲检测(默认是开启的)时,将此属性设置为 1 秒钟,可减少锁屏启动应用程序时的耗电量。此属性默认值为 0.02 秒
InactiveSleepTime = TimeSpan.FromSeconds();
} /// <summary>
/// 游戏运行前的一些初始化工作
/// </summary>
protected override void Initialize()
{
base.Initialize();
} /// <summary>
/// 加载游戏所需用到的资源,如图像和音效等
/// </summary>
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice); // 将图片 Image/Son 加载到 Texture2D 对象中
sprite = Content.Load<Texture2D>("Image/Son");
} /// <summary>
/// 手工释放对象,游戏退出时会自动调用此方法
/// 注:XNA 会自动进行垃圾回收
/// </summary>
protected override void UnloadContent()
{ } /// <summary>
/// Draw 之前的逻辑计算
/// </summary>
/// <param name="gameTime">游戏的当前时间对象</param>
protected override void Update(GameTime gameTime)
{
// 用户按返回键则退出应用程序
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit(); base.Update(gameTime);
} /// <summary>
/// 在游戏窗口上进行绘制
/// </summary>
/// <param name="gameTime">游戏的当前时间对象</param>
protected override void Draw(GameTime gameTime)
{
// 清除游戏窗口上的所有对象,然后以 CornflowerBlue 颜色作为背景
GraphicsDevice.Clear(Color.CornflowerBlue); // SpriteBatch.Draw() - 用于绘制图像,其应在 SpriteBatch.Begin() 和 SpriteBatch.End() 之间调用
spriteBatch.Begin();
spriteBatch.Draw(sprite, new Vector2((this.Window.ClientBounds.Width - sprite.Width) / , (this.Window.ClientBounds.Height - sprite.Height) / ), Color.White);
spriteBatch.End(); base.Draw(gameTime);
}
}
}

3、使用 Silverlight 和 XNA 组合开发 Windows Phone App 的 Demo(在 Silverlight 中融入 XNA)
GamePage.xaml

<phone:PhoneApplicationPage
x:Class="Combine.GamePage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d" d:DesignHeight="800" d:DesignWidth="480"
shell:SystemTray.IsVisible="False"> <StackPanel Orientation="Vertical"> <!--
4 个按钮,用于控制 sprite 的 上/下/左/右 移动
--> <Button Name="btnUp" Content="上" Click="btnUp_Click" />
<Button Name="btnDown" Content="下" Click="btnDown_Click" />
<Button Name="btnLeft" Content="左" Click="btnLeft_Click" />
<Button Name="btnRight" Content="右" Click="btnRight_Click" />
</StackPanel> </phone:PhoneApplicationPage>

AppServiceProvider.cs

using System;
using System.Collections.Generic; namespace Combine
{
/// <summary>
/// Implements IServiceProvider for the application. This type is exposed through the App.Services
/// property and can be used for ContentManagers or other types that need access to an IServiceProvider.
/// </summary>
public class AppServiceProvider : IServiceProvider
{
// A map of service type to the services themselves
private readonly Dictionary<Type, object> services = new Dictionary<Type, object>(); /// <summary>
/// Adds a new service to the service provider.
/// </summary>
/// <param name="serviceType">The type of service to add.</param>
/// <param name="service">The service object itself.</param>
public void AddService(Type serviceType, object service)
{
// Validate the input
if (serviceType == null)
throw new ArgumentNullException("serviceType");
if (service == null)
throw new ArgumentNullException("service");
if (!serviceType.IsAssignableFrom(service.GetType()))
throw new ArgumentException("service does not match the specified serviceType"); // Add the service to the dictionary
services.Add(serviceType, service);
} /// <summary>
/// Gets a service from the service provider.
/// </summary>
/// <param name="serviceType">The type of service to retrieve.</param>
/// <returns>The service object registered for the specified type..</returns>
public object GetService(Type serviceType)
{
// Validate the input
if (serviceType == null)
throw new ArgumentNullException("serviceType"); // Retrieve the service from the dictionary
return services[serviceType];
} /// <summary>
/// Removes a service from the service provider.
/// </summary>
/// <param name="serviceType">The type of service to remove.</param>
public void RemoveService(Type serviceType)
{
// Validate the input
if (serviceType == null)
throw new ArgumentNullException("serviceType"); // Remove the service from the dictionary
services.Remove(serviceType);
}
}
}

GamePage.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics; namespace Combine
{
public partial class GamePage : PhoneApplicationPage
{
// 以 XNA 的方式加载资源
ContentManager contentManager;
// 计时器
GameTimer timer;
// 精灵绘制器
SpriteBatch spriteBatch;
// 2D 纹理对象
Texture2D sprite; // silverlight 元素绘制器
UIElementRenderer elementRenderer; // sprite 的位置信息
Vector2 position = Vector2.Zero; public GamePage()
{
InitializeComponent(); // 获取 ContentManager 对象
contentManager = (Application.Current as App).Content; // 指定计时器每 1/30 秒执行一次,即帧率为 30 fps
timer = new GameTimer();
timer.UpdateInterval = TimeSpan.FromTicks();
timer.Update += OnUpdate;
timer.Draw += OnDraw; // 当 silverlight 可视树发生改变时
LayoutUpdated += new EventHandler(GamePage_LayoutUpdated);
} protected override void OnNavigatedTo(NavigationEventArgs e)
{
// 指示显示设备需要同时支持 silverlight 和 XNA
SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(true); // 实例化精灵绘制器
spriteBatch = new SpriteBatch(SharedGraphicsDeviceManager.Current.GraphicsDevice); // 将图片 Image/Son 加载到 Texture2D 对象中
if (sprite == null)
{
sprite = contentManager.Load<Texture2D>("Image/Son");
} // 启动计时器
timer.Start(); base.OnNavigatedTo(e);
} void GamePage_LayoutUpdated(object sender, EventArgs e)
{
// 指定窗口的宽和高
if ((ActualWidth > ) && (ActualHeight > ))
{
SharedGraphicsDeviceManager.Current.PreferredBackBufferWidth = (int)ActualWidth;
SharedGraphicsDeviceManager.Current.PreferredBackBufferHeight = (int)ActualHeight;
} // 实例化 silverlight 元素绘制器
if (elementRenderer == null)
{
elementRenderer = new UIElementRenderer(this, (int)ActualWidth, (int)ActualHeight);
}
} protected override void OnNavigatedFrom(NavigationEventArgs e)
{
// 停止计时器
timer.Stop(); // 指示显示设备关闭对 XNA 的支持
SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(false); base.OnNavigatedFrom(e);
} /// <summary>
/// Draw 之前的逻辑计算
/// </summary>
private void OnUpdate(object sender, GameTimerEventArgs e)
{ } /// <summary>
/// 在窗口上进行绘制
/// </summary>
private void OnDraw(object sender, GameTimerEventArgs e)
{
// 清除窗口上的所有对象,然后以 CornflowerBlue 颜色作为背景
SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Color.Black); // 呈现 silverlight 元素到缓冲区
elementRenderer.Render(); spriteBatch.Begin();
// 绘制 silverlight 元素
spriteBatch.Draw(elementRenderer.Texture, Vector2.Zero, Color.White);
// 绘制 sprite 对象
spriteBatch.Draw(sprite, position, Color.White);
spriteBatch.End();
} private void btnUp_Click(object sender, RoutedEventArgs e)
{
position.Y--;
} private void btnDown_Click(object sender, RoutedEventArgs e)
{
position.Y++;
} private void btnLeft_Click(object sender, RoutedEventArgs e)
{
position.X--;
} private void btnRight_Click(object sender, RoutedEventArgs e)
{
position.X++;
}
}
}

OK
[源码下载]

与众不同 windows phone (1) - Hello Windows Phone的更多相关文章

  1. 玩转Windows服务系列——给Windows服务添加COM接口

    当我们运行一个Windows服务的时候,一般情况下,我们会选择以非窗口或者非控制台的方式运行,这样,它就只是一个后台程序,没有界面供我们进行交互. 那么当我们想与Windows服务进行实时交互的时候, ...

  2. 玩转Windows服务系列——创建Windows服务

    创建Windows服务的项目 新建项目->C++语言->ATL->ATL项目->服务(EXE) 这样就创建了一个Windows服务项目. 生成的解决方案包含两个项目:Servi ...

  3. Windows Service--Write a Better Windows Service

    原文地址: http://visualstudiomagazine.com/Articles/2005/10/01/Write-a-Better-Windows-Service.aspx?Page=1 ...

  4. WPF程序在Windows 7下应用Windows 8主题

    这篇博客介绍如何在Windows 7下应用Windows 8的主题. 首先我们先看一个很常见的场景,同样的WPF程序(样式未重写)在不同的操作系统上展示会有些不同.这是为什么呢?WPF程序启动时会加载 ...

  5. Windows 10系统更换Windows 7系统磁盘分区注意事项一

    新买的电脑预装系统是WIN10,考虑到兼容性问题,打算更换为WIN7,但在新机上不能直接装WIN7系统,需要在BIOS启动中做一点小改动. 原因分析:由于Windows 8采用的是UEFI引导和GPT ...

  6. Windows GUI代码与Windows消息问题调试利器

    Windows GUI代码与Windows消息问题调试利器 记得很久前有这么一种说法: 人类区别于动物的标准就是工具的使用.同样在软件开发这个行业里面,对于工具的使用也是高手和入门级选手的主要区别,高 ...

  7. 需要正确安装 Microsoft.Windows.ShellExperienceHost 和 "Microsoft.Windows.Cortana" 应用程序。

    windows 10 开始菜单修复工具 Win10开始菜单修复工具出现的原因,自从升级到Windows  10,一直BUG不断,而其中有一个BUG非常的让你印象深刻,就是开始菜单无响应,你用着用着电脑 ...

  8. 不可或缺 Windows Native (25) - C++: windows app native, android app native, ios app native

    [源码下载] 不可或缺 Windows Native (25) - C++: windows app native, android app native, ios app native 作者:web ...

  9. Windows 8.1升级至Windows 10后,启动VisualSVN Server Manager报错:提供程序无法执行所尝试的操作 (0x80041024)的解决

    1.1.Windows 8.1升级至Windows 10后,启动VisualSVN Server Manager报错:提供程序无法执行所尝试的操作 (0x80041024),VisualSVN Ser ...

随机推荐

  1. 一步一步重写 CodeIgniter 框架 (6) —— 实现在控制器Controller中加载View

    1. 控制器将模型类获得的数据,传递给视图进行显示,所以视图必须负责接收数据,另外重要的一点是当模型和视图分开后,多个模型的数据可以传递给一个视图进行展示,也可以说一个模型的数据在多个不同的视图中进行 ...

  2. linux cmd: linux下解压命令大全

    linux下解压命令大全 .tar 解包:tar xvf FileName.tar打包:tar cvf FileName.tar DirName(注:tar是打包,不是压缩!)———————————— ...

  3. Denny Zhang:一辈子做一个自由职业者

    程序猿訪谈录供稿 Denny是一个旅居美国的自由职业者,这是一份让人羡慕的职业,选择这个职业意味着他已经实现某种程度上的经济自由,能够最大限度的做自己喜欢的事情,对他来说,选择自由职业作为自己终生的事 ...

  4. WCF技术剖析之十二:数据契约(Data Contract)和数据契约序列化器(DataContractSerializer)

    原文:WCF技术剖析之十二:数据契约(Data Contract)和数据契约序列化器(DataContractSerializer) [爱心链接:拯救一个25岁身患急性白血病的女孩[内有苏州电视台经济 ...

  5. GAE+bottle+jinja2+beaker快速开发demo - Python,GAE - language - ITeye论坛

    GAE+bottle+jinja2+beaker快速开发demo - Python,GAE - language - ITeye论坛     :GAE+bottle+jinja2+beaker快速开发 ...

  6. Android应用开发学习笔记之BroadcastReceiver

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 一.BroadcastReceiver机制概述 Broadcast Receiver是Android的一种“广播发布 ...

  7. Swift - 使用位运算提取颜色,合并颜色

    通常我们可以使用16进制的格式表示RGB颜色,比如0x2f88c0.通过位操作运算,能很方便的将其中的R,G,B颜色各部分分别提取出来.反之,也可以将R,G,B颜色值组合成一个完整的颜色. 1,提取颜 ...

  8. Swift - 使用NSURLSession加载数据、下载、上传文件

    NSURLSession类支持三种类型的任务:加载数据.下载和上传.下面通过样例分别进行介绍. 1,使用Data Task加载数据 使用全局的sharedSession()和dataTaskWithR ...

  9. UVA 10574 - Counting Rectangles(枚举+计数)

    10574 - Counting Rectangles 题目链接 题意:给定一些点,求可以成几个边平行于坐标轴的矩形 思路:先把点按x排序,再按y排序.然后用O(n^2)的方法找出每条垂直x轴的边,保 ...

  10. sharepoint具体错误提示

    sharepoint页面发生错误时,默认不会显示具体错误信息,只显示“未知错误”提示.需要修改配置站点的webconfig文件,才能显示出具体错误提示.具体方法如下: 将safeMode中的CallS ...