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

简介: 原文:与众不同 windows phone (1) - Hello Windows Phone [索引页][源码下载] 与众不同 windows phone (1) - Hello Windows Phone 作者:webabcd介绍与众不同 windows phone 7.
原文: 与众不同 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 / 30);

            // 当禁止在锁屏状态下运行应用程序空闲检测(默认是开启的)时,将此属性设置为 1 秒钟,可减少锁屏启动应用程序时的耗电量。此属性默认值为 0.02 秒
            InactiveSleepTime = TimeSpan.FromSeconds(1);
        }

        /// <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) / 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

<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(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<Texture2D>("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);
        }

        /// <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
[源码下载]

目录
相关文章
|
存储 Kubernetes Cloud Native
云原生|kubernetes |一文带你搞懂pod调度策略,驱逐策略,污点、容忍调度(一)
云原生|kubernetes |一文带你搞懂pod调度策略,驱逐策略,污点、容忍调度
1053 0
|
25天前
|
人工智能 弹性计算 自然语言处理
如何快速拥有OpenClaw?使用阿里云轻量应用服务器快速部署流程与常见问题参考
OpenClaw是一款开源的本地优先AI代理与自动化平台,可将AI转变为高效的“数字员工”。通过阿里云轻量应用服务器快速部署,集成阿里云百炼大模型,用户可享受强大的文本生成与任务处理能力,同时利用云服务器的稳定与自主可控优势,打造随时可通过钉钉操控的专属AI服务。文章详细介绍了计费方式、购买与配置流程、个性化配置能力,并解答一些常见问题。
|
1月前
|
存储 弹性计算 数据库
2026年阿里云新老用户、企业用户云服务器新购·续费·升级活动整理
本文梳理2026年阿里云全生命周期优惠:新用户享38元/年轻量服务器、99元ECS及学生300元券;老用户续费同价+阶梯折扣;企业可得u1/u2a实例特惠、跨境补贴及组合套餐省30%;升级配额低至6.8折。
417 1
|
1月前
|
CDN
别花冤枉钱!阿里云 CDN 怎么收费?计费模式、价格表、增值费用全汇总
阿里云CDN收费分基础费(必选)和增值费(按需)。基础费支持按流量、带宽峰值或月结95峰值三种计费模式,默认按流量阶梯计价(中国内地低至0.15元/GB);增值费含HTTPS、QUIC、WAF、实时日志等,仅使用才计费。资源包可享大幅优惠。
365 1
|
1月前
|
存储 弹性计算 小程序
2026阿里云轻量应用服务器详解:免费试用、费用价格、200M带宽优势及问题解答FAQ
2026阿里云轻量应用服务器全面升级:新用户享1个月免费试用(2核1G/4G+200M带宽),国内套餐38元起/年,全系标配200M峰值带宽、不限流量、一键镜像。适合个人建站、小程序后端与开发测试,免备案香港版可选。
1019 3
|
5月前
|
消息中间件 人工智能 运维
AI 原生应用开发实战营·京沪双城回顾 & PPT 下载
近日,阿里云AI原生应用开发实战营 · 北京站&上海站圆满落幕。继深圳、杭州、成都等城市之后,这两场活动吸引了250+名技术从业者深度参与。活动聚焦 AI Agent 领域的前沿技术与落地实践,深度分享AI Agent 架构趋势和演进、AI 开放平台、AI应用运行时、AI应用托管、大模型可观测&AIOps、异步化的Agent事件驱动等热门技术议题,并设置了动手实操环节。
|
4月前
|
Web App开发 JSON JavaScript
从 Selenium 迁移到 Playwright:升级你的测试框架实战手册
Playwright正重塑Web自动化测试:相比Selenium,它通过直接控制浏览器实现更快、更稳的执行。迁移后测试速度提升40%,维护成本降低30%,稳定性显著增强。本文详解从环境搭建、API对照到高级特性的平滑迁移路径,助你完成测试体系的全面升级。
|
6月前
|
Web App开发 安全 Java
并发编程之《彻底搞懂Java线程》
本文系统讲解Java并发编程核心知识,涵盖线程概念、创建方式、线程安全、JUC工具集(线程池、并发集合、同步辅助类)及原子类原理,帮助开发者构建完整的并发知识体系。
|
9月前
|
关系型数据库 MySQL Linux
安装MySQL 5.7到红帽系RHEL8+系列上
本文介绍了在RHEL 8及以上系统中安装MySQL 5.7的两种方法:解压安装与RPM包安装。涵盖环境准备、目录配置、数据盘挂载、初始化及服务启动等关键步骤,适用于红帽系(8+)部署MySQL 5.7。
|
前端开发 JavaScript Java
一个软件开发工程师需要学几种编程语言?为什么?
一个软件开发工程师需要学几种编程语言?为什么?
945 64