博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
与众不同 windows phone (1) - Hello Windows Phone
阅读量:6681 次
发布时间:2019-06-25

本文共 10357 字,大约阅读时间需要 34 分钟。

原文:

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

作者:
介绍
与众不同 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

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 / 30);            // 当禁止在锁屏状态下运行应用程序空闲检测(默认是开启的)时,将此属性设置为 1 秒钟,可减少锁屏启动应用程序时的耗电量。此属性默认值为 0.02 秒            InactiveSleepTime = TimeSpan.FromSeconds(1);        }        ///         /// 游戏运行前的一些初始化工作        ///         protected override void Initialize()        {            base.Initialize();        }        ///         /// 加载游戏所需用到的资源,如图像和音效等        ///         protected override void LoadContent()        {            spriteBatch = new SpriteBatch(GraphicsDevice);            // 将图片 Image/Son 加载到 Texture2D 对象中            sprite = Content.Load
("Image/Son"); } ///
/// 手工释放对象,游戏退出时会自动调用此方法 /// 注:XNA 会自动进行垃圾回收 /// protected override void UnloadContent() { } ///
/// Draw 之前的逻辑计算 /// ///
游戏的当前时间对象 protected override void Update(GameTime gameTime) { // 用户按返回键则退出应用程序 if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); base.Update(gameTime); } ///
/// 在游戏窗口上进行绘制 /// ///
游戏的当前时间对象 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) / 2, (this.Window.ClientBounds.Height - sprite.Height) / 2), Color.White); spriteBatch.End(); base.Draw(gameTime); } }}

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

AppServiceProvider.cs

using System;using System.Collections.Generic;namespace Combine{    ///     /// 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.    ///     public class AppServiceProvider : IServiceProvider    {        // A map of service type to the services themselves        private readonly Dictionary
services = new Dictionary
(); ///
/// Adds a new service to the service provider. /// ///
The type of service to add. ///
The service object itself. 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); } ///
/// Gets a service from the service provider. /// ///
The type of service to retrieve. ///
The service object registered for the specified type..
public object GetService(Type serviceType) { // Validate the input if (serviceType == null) throw new ArgumentNullException("serviceType"); // Retrieve the service from the dictionary return services[serviceType]; } ///
/// Removes a service from the service provider. /// ///
The type of service to remove. 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(333333);            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
("Image/Son"); } // 启动计时器 timer.Start(); base.OnNavigatedTo(e); } void GamePage_LayoutUpdated(object sender, EventArgs e) { // 指定窗口的宽和高 if ((ActualWidth > 0) && (ActualHeight > 0)) { 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); } ///
/// Draw 之前的逻辑计算 /// private void OnUpdate(object sender, GameTimerEventArgs e) { } ///
/// 在窗口上进行绘制 /// 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

转载地址:http://ycxao.baihongyu.com/

你可能感兴趣的文章
PXE网络安装Linux
查看>>
图解VMware内存机制
查看>>
【翻译】Win with APIs by keeping it simple
查看>>
1月15日.xyz域名总量10强:新网排名降至第八
查看>>
1月末中国域名商解析量13强:西数破百万指日可待
查看>>
分布式项目规范总结
查看>>
阿里创新自动化测试工具平台--Doom
查看>>
Centos 5.5-yum安装配置LNMP
查看>>
跟 陌生人吃饭-这样的网站你认为如何?
查看>>
IT项目管理之接受风险
查看>>
Android Service启动Dialog
查看>>
Win2000 Server***监测
查看>>
如何查询SQL Server备份还原历史记录
查看>>
微信公众号H5支付遇到的那些坑
查看>>
好程序员web前端分享js实现实战案例
查看>>
Virtualbox安装Ubuntu,please remove the installation
查看>>
活动的启动模式汇总
查看>>
pptpd基于mysql用户验证的完整操作步骤
查看>>
git shallow clone之后切换远程分支的方案
查看>>
web服务之Apache实现的https访问
查看>>