原文:与众不同 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

  1. <phone:PhoneApplicationPage
  2. x:Class="Silverlight.MainPage"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
  6. xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
  7. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  8. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  9. mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
  10. FontFamily="{StaticResource PhoneFontFamilyNormal}"
  11. FontSize="{StaticResource PhoneFontSizeNormal}"
  12. Foreground="{StaticResource PhoneForegroundBrush}"
  13. SupportedOrientations="Portrait" Orientation="Portrait"
  14. shell:SystemTray.IsVisible="True">
  15.  
  16. <StackPanel>
  17. <!--按钮-->
  18. <Button Name="btn" Content="hello webabcd" />
  19. </StackPanel>
  20.  
  21. </phone:PhoneApplicationPage>

MainPage.xaml.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Documents;
  8. using System.Windows.Input;
  9. using System.Windows.Media;
  10. using System.Windows.Media.Animation;
  11. using System.Windows.Shapes;
  12. using Microsoft.Phone.Controls;
  13.  
  14. namespace Silverlight
  15. {
  16. public partial class MainPage : PhoneApplicationPage
  17. {
  18. public MainPage()
  19. {
  20. InitializeComponent();
  21.  
  22. this.Loaded += new RoutedEventHandler(MainPage_Loaded);
  23. }
  24.  
  25. void MainPage_Loaded(object sender, RoutedEventArgs e)
  26. {
  27. // 弹出 MessageBox 信息
  28. btn.Click += delegate { MessageBox.Show("hello webabcd"); };
  29. }
  30. }
  31. }

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Audio;
  6. using Microsoft.Xna.Framework.Content;
  7. using Microsoft.Xna.Framework.GamerServices;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Input.Touch;
  11. using Microsoft.Xna.Framework.Media;
  12.  
  13. namespace XNA
  14. {
  15. // 启动时先 Initialize,再 LoadContent,退出时 UnloadContent
  16. public class Game1 : Microsoft.Xna.Framework.Game
  17. {
  18. // 图形设备(显卡)管理器,XNA 在游戏窗口上做的所有事情都要通过此对象
  19. GraphicsDeviceManager graphics;
  20.  
  21. // 精灵绘制器
  22. SpriteBatch spriteBatch;
  23.  
  24. // 2D 纹理对象
  25. Texture2D sprite;
  26.  
  27. public Game1()
  28. {
  29. graphics = new GraphicsDeviceManager(this);
  30.  
  31. // 指定游戏窗口的宽和高,不设置的话会花屏
  32. graphics.PreferredBackBufferWidth = this.Window.ClientBounds.Width;
  33. graphics.PreferredBackBufferHeight = this.Window.ClientBounds.Height;
  34.  
  35. Content.RootDirectory = "Content";
  36.  
  37. // 两次绘制的间隔时间,本例为每 1/30 秒绘制一次,即帧率为 30 fps。此属性默认值为 60 fps
  38. TargetElapsedTime = TimeSpan.FromSeconds(1f / );
  39.  
  40. // 当禁止在锁屏状态下运行应用程序空闲检测(默认是开启的)时,将此属性设置为 1 秒钟,可减少锁屏启动应用程序时的耗电量。此属性默认值为 0.02 秒
  41. InactiveSleepTime = TimeSpan.FromSeconds();
  42. }
  43.  
  44. /// <summary>
  45. /// 游戏运行前的一些初始化工作
  46. /// </summary>
  47. protected override void Initialize()
  48. {
  49. base.Initialize();
  50. }
  51.  
  52. /// <summary>
  53. /// 加载游戏所需用到的资源,如图像和音效等
  54. /// </summary>
  55. protected override void LoadContent()
  56. {
  57. spriteBatch = new SpriteBatch(GraphicsDevice);
  58.  
  59. // 将图片 Image/Son 加载到 Texture2D 对象中
  60. sprite = Content.Load<Texture2D>("Image/Son");
  61. }
  62.  
  63. /// <summary>
  64. /// 手工释放对象,游戏退出时会自动调用此方法
  65. /// 注:XNA 会自动进行垃圾回收
  66. /// </summary>
  67. protected override void UnloadContent()
  68. {
  69.  
  70. }
  71.  
  72. /// <summary>
  73. /// Draw 之前的逻辑计算
  74. /// </summary>
  75. /// <param name="gameTime">游戏的当前时间对象</param>
  76. protected override void Update(GameTime gameTime)
  77. {
  78. // 用户按返回键则退出应用程序
  79. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  80. this.Exit();
  81.  
  82. base.Update(gameTime);
  83. }
  84.  
  85. /// <summary>
  86. /// 在游戏窗口上进行绘制
  87. /// </summary>
  88. /// <param name="gameTime">游戏的当前时间对象</param>
  89. protected override void Draw(GameTime gameTime)
  90. {
  91. // 清除游戏窗口上的所有对象,然后以 CornflowerBlue 颜色作为背景
  92. GraphicsDevice.Clear(Color.CornflowerBlue);
  93.  
  94. // SpriteBatch.Draw() - 用于绘制图像,其应在 SpriteBatch.Begin() 和 SpriteBatch.End() 之间调用
  95. spriteBatch.Begin();
  96. spriteBatch.Draw(sprite, new Vector2((this.Window.ClientBounds.Width - sprite.Width) / , (this.Window.ClientBounds.Height - sprite.Height) / ), Color.White);
  97. spriteBatch.End();
  98.  
  99. base.Draw(gameTime);
  100. }
  101. }
  102. }

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

  1. <phone:PhoneApplicationPage
  2. x:Class="Combine.GamePage"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
  6. xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
  7. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  8. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  9. FontFamily="{StaticResource PhoneFontFamilyNormal}"
  10. FontSize="{StaticResource PhoneFontSizeNormal}"
  11. Foreground="{StaticResource PhoneForegroundBrush}"
  12. SupportedOrientations="Portrait" Orientation="Portrait"
  13. mc:Ignorable="d" d:DesignHeight="800" d:DesignWidth="480"
  14. shell:SystemTray.IsVisible="False">
  15.  
  16. <StackPanel Orientation="Vertical">
  17.  
  18. <!--
  19. 4 个按钮,用于控制 sprite 的 上/下/左/右 移动
  20. -->
  21.  
  22. <Button Name="btnUp" Content="上" Click="btnUp_Click" />
  23. <Button Name="btnDown" Content="下" Click="btnDown_Click" />
  24. <Button Name="btnLeft" Content="左" Click="btnLeft_Click" />
  25. <Button Name="btnRight" Content="右" Click="btnRight_Click" />
  26. </StackPanel>
  27.  
  28. </phone:PhoneApplicationPage>

AppServiceProvider.cs

  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Combine
  5. {
  6. /// <summary>
  7. /// Implements IServiceProvider for the application. This type is exposed through the App.Services
  8. /// property and can be used for ContentManagers or other types that need access to an IServiceProvider.
  9. /// </summary>
  10. public class AppServiceProvider : IServiceProvider
  11. {
  12. // A map of service type to the services themselves
  13. private readonly Dictionary<Type, object> services = new Dictionary<Type, object>();
  14.  
  15. /// <summary>
  16. /// Adds a new service to the service provider.
  17. /// </summary>
  18. /// <param name="serviceType">The type of service to add.</param>
  19. /// <param name="service">The service object itself.</param>
  20. public void AddService(Type serviceType, object service)
  21. {
  22. // Validate the input
  23. if (serviceType == null)
  24. throw new ArgumentNullException("serviceType");
  25. if (service == null)
  26. throw new ArgumentNullException("service");
  27. if (!serviceType.IsAssignableFrom(service.GetType()))
  28. throw new ArgumentException("service does not match the specified serviceType");
  29.  
  30. // Add the service to the dictionary
  31. services.Add(serviceType, service);
  32. }
  33.  
  34. /// <summary>
  35. /// Gets a service from the service provider.
  36. /// </summary>
  37. /// <param name="serviceType">The type of service to retrieve.</param>
  38. /// <returns>The service object registered for the specified type..</returns>
  39. public object GetService(Type serviceType)
  40. {
  41. // Validate the input
  42. if (serviceType == null)
  43. throw new ArgumentNullException("serviceType");
  44.  
  45. // Retrieve the service from the dictionary
  46. return services[serviceType];
  47. }
  48.  
  49. /// <summary>
  50. /// Removes a service from the service provider.
  51. /// </summary>
  52. /// <param name="serviceType">The type of service to remove.</param>
  53. public void RemoveService(Type serviceType)
  54. {
  55. // Validate the input
  56. if (serviceType == null)
  57. throw new ArgumentNullException("serviceType");
  58.  
  59. // Remove the service from the dictionary
  60. services.Remove(serviceType);
  61. }
  62. }
  63. }

GamePage.xaml.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Documents;
  8. using System.Windows.Input;
  9. using System.Windows.Navigation;
  10. using System.Windows.Shapes;
  11. using Microsoft.Phone.Controls;
  12. using Microsoft.Xna.Framework;
  13. using Microsoft.Xna.Framework.Content;
  14. using Microsoft.Xna.Framework.Graphics;
  15.  
  16. namespace Combine
  17. {
  18. public partial class GamePage : PhoneApplicationPage
  19. {
  20. // 以 XNA 的方式加载资源
  21. ContentManager contentManager;
  22. // 计时器
  23. GameTimer timer;
  24. // 精灵绘制器
  25. SpriteBatch spriteBatch;
  26. // 2D 纹理对象
  27. Texture2D sprite;
  28.  
  29. // silverlight 元素绘制器
  30. UIElementRenderer elementRenderer;
  31.  
  32. // sprite 的位置信息
  33. Vector2 position = Vector2.Zero;
  34.  
  35. public GamePage()
  36. {
  37. InitializeComponent();
  38.  
  39. // 获取 ContentManager 对象
  40. contentManager = (Application.Current as App).Content;
  41.  
  42. // 指定计时器每 1/30 秒执行一次,即帧率为 30 fps
  43. timer = new GameTimer();
  44. timer.UpdateInterval = TimeSpan.FromTicks();
  45. timer.Update += OnUpdate;
  46. timer.Draw += OnDraw;
  47.  
  48. // 当 silverlight 可视树发生改变时
  49. LayoutUpdated += new EventHandler(GamePage_LayoutUpdated);
  50. }
  51.  
  52. protected override void OnNavigatedTo(NavigationEventArgs e)
  53. {
  54. // 指示显示设备需要同时支持 silverlight 和 XNA
  55. SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(true);
  56.  
  57. // 实例化精灵绘制器
  58. spriteBatch = new SpriteBatch(SharedGraphicsDeviceManager.Current.GraphicsDevice);
  59.  
  60. // 将图片 Image/Son 加载到 Texture2D 对象中
  61. if (sprite == null)
  62. {
  63. sprite = contentManager.Load<Texture2D>("Image/Son");
  64. }
  65.  
  66. // 启动计时器
  67. timer.Start();
  68.  
  69. base.OnNavigatedTo(e);
  70. }
  71.  
  72. void GamePage_LayoutUpdated(object sender, EventArgs e)
  73. {
  74. // 指定窗口的宽和高
  75. if ((ActualWidth > ) && (ActualHeight > ))
  76. {
  77. SharedGraphicsDeviceManager.Current.PreferredBackBufferWidth = (int)ActualWidth;
  78. SharedGraphicsDeviceManager.Current.PreferredBackBufferHeight = (int)ActualHeight;
  79. }
  80.  
  81. // 实例化 silverlight 元素绘制器
  82. if (elementRenderer == null)
  83. {
  84. elementRenderer = new UIElementRenderer(this, (int)ActualWidth, (int)ActualHeight);
  85. }
  86. }
  87.  
  88. protected override void OnNavigatedFrom(NavigationEventArgs e)
  89. {
  90. // 停止计时器
  91. timer.Stop();
  92.  
  93. // 指示显示设备关闭对 XNA 的支持
  94. SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(false);
  95.  
  96. base.OnNavigatedFrom(e);
  97. }
  98.  
  99. /// <summary>
  100. /// Draw 之前的逻辑计算
  101. /// </summary>
  102. private void OnUpdate(object sender, GameTimerEventArgs e)
  103. {
  104.  
  105. }
  106.  
  107. /// <summary>
  108. /// 在窗口上进行绘制
  109. /// </summary>
  110. private void OnDraw(object sender, GameTimerEventArgs e)
  111. {
  112. // 清除窗口上的所有对象,然后以 CornflowerBlue 颜色作为背景
  113. SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Color.Black);
  114.  
  115. // 呈现 silverlight 元素到缓冲区
  116. elementRenderer.Render();
  117.  
  118. spriteBatch.Begin();
  119. // 绘制 silverlight 元素
  120. spriteBatch.Draw(elementRenderer.Texture, Vector2.Zero, Color.White);
  121. // 绘制 sprite 对象
  122. spriteBatch.Draw(sprite, position, Color.White);
  123. spriteBatch.End();
  124. }
  125.  
  126. private void btnUp_Click(object sender, RoutedEventArgs e)
  127. {
  128. position.Y--;
  129. }
  130.  
  131. private void btnDown_Click(object sender, RoutedEventArgs e)
  132. {
  133. position.Y++;
  134. }
  135.  
  136. private void btnLeft_Click(object sender, RoutedEventArgs e)
  137. {
  138. position.X--;
  139. }
  140.  
  141. private void btnRight_Click(object sender, RoutedEventArgs e)
  142. {
  143. position.X++;
  144. }
  145. }
  146. }

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. 【转】关于C语言生成不重复的随机数

    一 说起随机函数,恐怕又有人说这是老生长谈了……一般很多人都形成了自己的固定格式,因为随机数用处比较大,用的时候比较多,拿过来就用了.但是新手不这么 干,他们总是抱有疑惑,我就是一个新手,而且较菜…… ...

  2. UVA 10340 (13.08.25)

    Problem E All in All Input: standard input Output: standard output Time Limit: 2 seconds Memory Limi ...

  3. eclipse、MyEclipse实现批量改动文件编码

    在使用eclipse或MyEclipse编程时,常常遇到部分文件打开后出现乱码的情况(特别是在导入项目后) 1:右击项目选择properties->Resource>Other选择UTF- ...

  4. Android模拟器的文件目录介绍

    文件存放在 .avd文件夹下 .ini为对应的配置文件     打开.avd文件夹 *.lock文件夹保存的是模拟器的一下数据,当模拟器正常关闭时这些文件夹都会被自动删除. 当模拟器无法开启的时候可以 ...

  5. 闲扯 Javascript 00

    引言 Javascript  的作用在此就不阐述了,相信你已经知道它的用途!那我说点什么呢? 不如就和大家先扯一把,后面的工作 随后后展开吧! 首先声明:我个人对Javascript 认识,我只知道它 ...

  6. IOS SWIFT 网络请求JSON解析 基础一

    前言:移动互联网时代,网络通信已经是手机端必不可少的功能.应用中也必不可少地使用了网络通信,增强客户端与服务器交互.使用NSURLConnection实现HTTP的通信.NSURLConnection ...

  7. MYSQL中delete删除多表数据与删除关联数据

    在mysql中删除数据方法有很多种,最常用的是使用delete来删除记录,下面我来介绍delete删除单条记 录与删除多表关联数据的一些简单实例. 1.delete from t1 where 条件 ...

  8. Android组件:Fragment切换后保存状态

    之前写的第一篇Fragment实例,和大多数人一开始学的一样,都是通过FragmentTransaction的replace方法来实现,replace方法相当于先移除remove()原来所有已存在的f ...

  9. 终于懂了:Delphi消息的Result完全是生造出来的,不是Windows消息自带的(Delphi对Windows编程体系的改造越大,学习收获就越大)——消息是否继续传递就看这个Result

    Windows中,消息使用统一的结构体(MSG)来存放信息,其中message表明消息的具体的类型, 而wParam,lParam是其最灵活的两个变量,为不同的消息类型时,存放数据的含义也不一样. t ...

  10. WCF技术剖析之五:利用ASP.NET兼容模式创建支持会话(Session)的WCF服务

    原文:WCF技术剖析之五:利用ASP.NET兼容模式创建支持会话(Session)的WCF服务 在<基于IIS的WCF服务寄宿(Hosting)实现揭秘>中,我们谈到在采用基于IIS(或者 ...