上一节我们讲了如何创建一个基础的SliverLight For Windows Phone Application,这一节我们讲述如何制作一个简单的使用XNAWindows Phone 程序,并在界面上输出“Hello WP7”。

    1. 创建一个XNA for Windows Phone程序

       创建一个XNA for Windows Phone 程序的方法与创建一个SliverLight For Windows Phone Application 方法类似。打开VS2010,选择新建项目,在左侧已安装模板中,依次选择Visual C#XNA Game Studio 4.0,出现如下画面:

 

在右侧选择Windows Phone Game 4.0 输入Name HelloWP7,点击OK生成项目。

 

一个默认的基于XNAWindows Phone 程序包括如下图所示内容:

描述

Programe.cs

只能在Windwos Xbox 的代码,删掉也无所谓

GameThumbnail.png

程序显示的图标,图片,删掉后由默认图标替换

Background.png/Game.ico

一个没有使用的图片和图标

Game1.cs

程序的主要代码实现部分。

Properties\AppManifest.xml

一个生成应用程序包所必需的应用程序清单文件

Properties\AssemblyInfo.cs

包含名称和版本的元数据,这些元数据将被嵌入到生成的程序集

Properties\WMAppManifest.xml

一个包含与Windows Phone 应用程序相关的特定元数据的清单文件,且包含了用于Windows PhoneXNA所具有的特定功能

References folder

一些库文件(集)的列表,为应用程序的工作提供功能和服务。

HelloWP7Content(Content)

为程序放置资源的单独项目

Content References\HelloWP7(Content)

是否需要使用到单独的资源文件项目,如果有则添加引用。默认为引用

 

其具体功能可以到后面的章节Windows Phone 7 开发之深入浅出-进阶篇之基础结构介绍XNA一节中看到。此时按F5 运行,则可在模拟器上启动该程序,如图:

 

1. 增加字体文件

接下来我们需要让这个界面显示一些东西,那么显示什么呢?就显示一行字吧?“Hello World”,不,这里我们显示“Hello Windows Phone 7”。

要想显示字体,XNA要求必须添加字体文件,右键点击HelloWP7Content(Content)点击ADD,ADD New Item 出现如下图片:

 

选择Sprite Font ,起名spFont1,点击Add 增加该字体文件。在HelloWP7Content(Content)项目中会增加一个spFont1.spritefont 文件。该文件可以直接打开,是一个标准的XML文件。

代码如下:

 

代码
<?xml version="1.0" encoding="utf-8"?>
<!--
This file contains an xml description of a font, and will be read by the XNA
Framework Content Pipeline. Follow the comments to customize the appearance
of the font in your game, and to change the characters which are available to draw
with.
-->
<XnaContent xmlns:Graphics="Microsoft.Xna.Framework.Content.Pipeline.Graphics">
  
<Asset Type="Graphics:FontDescription">

    
<!--
    Modify this string to change the font that will be imported.
    
-->
    
<FontName>Segoe UI Mono</FontName>

    
<!--
    Size is a float value, measured in points. Modify this value to change
    the size of the font.
    
-->
    
<Size>14</Size>

    
<!--
    Spacing is a float value, measured in pixels. Modify this value to change
    the amount of spacing in between characters.
    
-->
    
<Spacing>0</Spacing>

    
<!--
    UseKerning controls the layout of the font. If this value is true, kerning information
    will be used when placing characters.
    
-->
    
<UseKerning>true</UseKerning>

    
<!--
    Style controls the style of the font. Valid entries are "Regular", "Bold", "Italic",
    and "Bold, Italic", and are case sensitive.
    
-->
    
<Style>Regular</Style>

    
<!--
    If you uncomment this line, the default character will be substituted if you draw
    or measure text that contains characters which were not included in the font.
    
-->
    
<!-- <DefaultCharacter>*</DefaultCharacter> -->

    
<!--
    CharacterRegions control what letters are available in the font. Every
    character from Start to End will be built and made available for drawing. The
    default range is from 32, (ASCII space), to 126, ('~'), covering the basic Latin
    character set. The characters are ordered according to the Unicode standard.
    See the documentation for more information.
    
-->
    
<CharacterRegions>
      
<CharacterRegion>
        
<Start>&#32;</Start>
        
<End>&#126;</End>
      
</CharacterRegion>
    
</CharacterRegions>
  
</Asset>
</XnaContent>

 

其每个节点具体含义,我们将在本地化的一节进行讲解,不过注释的那么清楚,我想大家猜也可以猜到哟。

3. 显示字体文件

有了字体,我们就要显示文字了。按照理论的步骤是增加字体-〉显示文字,下面我们来载入字体:

    首先我们在Game1 类中定义一个私有变量

    private SpriteFont spriteFont;

    并在   protected override void LoadContent() 方法中载入字体

    ContentManager cm = this.Content;

    spriteFont = cm.Load<SpriteFont>("spFont1"); 

   载入后,输出字体的方法要到

protected override void Draw(GameTime gameTime) 方法中加入

 spriteBatch.Begin(); 

  spriteBatch.DrawString(spriteFont, "Hello Windows Phone 7", new Vector2(200, 200), Color.Red);

  spriteBatch.End(); 

其中   spriteBatch.DrawString(spriteFont, "Hello Windows Phone 7", new Vector2(200, 200), Color.Red);为输出字体

spriteBatch.Begin();  spriteBatch.End();

在绘制开始分别被调用

运行结果如下图所示:

 

 

posted on 2010-10-29 20:08  Happy Miao  阅读(282)  评论(0)    收藏  举报