วันพุธที่ 9 พฤษภาคม พ.ศ. 2555

Using the Project Template that Combines Silverlight and XNA

Using the Project Template that Combines Silverlight and XNA

Starting in Windows Phone OS 7.1, you can combine Silverlight and XNA content in the same application or game. This enables you to add rich text support to your XNA games using Silverlight and enables you to add pages with XNA content to your Silverlight applications. This topic describes the Silverlight and XNA template and how to use it.
When you install the Windows Phone SDK 7.1, the Windows Phone Silverlight and XNA Application template is installed to help you combine Silverlight and XNA content in a single Windows Phone application. The template is available in the New Project dialog box for the Silverlight for Windows Phone or the XNA Game Studio 4.0 categories in both Visual Basic and C#. This template is the same whether you select it from the Silverlight or XNA categories.
This template creates a Silverlight page and a Silverlight page that renders XNA content. This template also provides a way to navigate from the Silverlight page to the page that renders the XNA content.
When you create a new project with this template, the following three projects are created for you in Solution Explorer.
  • Main application project
  • XNA library project
  • XNA library content project
The main application project contains application level files, a Silverlight page, a Silverlight page that renders XNA content, and a button to navigate from the Silverlight page to the XNA content.
FilesPurpose
App.xaml
App.xaml.cs/vb
Contains application-level data, initialization code, and lifecycle code. App.xaml provides a GameTimer and ContentManager at the application level, and contains a SharedGraphicsDeviceManager object. These enable Silverlight and XNA to run in the same application.
MainPage.xaml
MainPage.xaml.cs/vb
Contains Silverlight content and provides the application start-up experience.
GamePage.xaml.cs/vbContains XNA content.
GamePage.xaml can only be used with the UIElementRenderer class, which renders Silverlight content in a XNA game. For more information, see How to: Create Your First XNA Framework Application for Windows Phone.
The XNA library project acts as a bridge between the Silverlight and XNA build systems.
The XNA library content project provides a location for XNA resources, such as sound and image files.
Caution noteCaution:
To debug an application that renders XNA content in the emulator, the computer you are debugging the application on must be GPU-enabled. For more information, see Optimizing Performance for Windows Phone Emulator.
You create the application start-up experience by modifying the MainPage.xaml and MainPage.xaml.cs/vb files. MainPage.xaml contains the standard title text boxes found in other Silverlight for Windows Phone applications and also a button. The code-behind file contains the click event handler for the button that navigates to GamePage.xaml, which is the Silverlight page that renders the XNA content. The following example shows the navigation code.
NavigationService.Navigate(new Uri("/GamePage.xaml", UriKind.Relative));
If you delete the button, you will have to add a way to navigate to the page that renders the XNA content elsewhere in the Silverlight page.
You can add UI elements and controls to MainPage.xaml and code, such as event handlers for UI elements, to MainPage.xaml.cs/vb. For information about the different kinds of controls you can use in Silverlight for Windows Phone applications, see Controls for Windows Phone and Types of Controls QuickStart.
To add additional Silverlight pages to the application, choose the Project menu and then Add New Item.
GamePage.xaml.cs/vb contains the main game loop for your application. Although the navigation URL is to GamePage.xaml, GamePage.xaml contains no XAML content because XNA cannot render XAML. All of the content for GamePage is added in the code-behind file.
You add XNA content to the application by adding resources, such as images and sound files, to the XNA library content project and referencing them in GamePage.xaml.cs/vb. There are several important methods in GamePage.xaml.cs/vb that are added by the template. The following table lists the methods and their purpose. For more information, see How to: Create Your First XNA Framework Application for Windows Phone.
MethodPurpose
GamePage constructorInitializes the game timer that is used to update and draw the page. In addition, this method initializes the ContentManager for the page by setting it to the ContentManager for the application. The constructor is called when the page is first created.
OnNavigatedToSets the SharingMode to true, which enables XNA content to be drawn in a Silverlight application. Initializes a SpriteBatch for the page and starts the game timer. Use this method to load additional content using the ContentManager for the page. This method is called every time the page is navigated to.
OnNavigatedFromSets SharingMode to false. Stops the timer. This method is called every time the page is navigated from.
OnUpdateHandles the Timer.Update event. This method should contain updates to the page, such as gathering input and playing audio files.
OnDrawHandles the Timer.Draw event. This method should contain code for drawing the contents of the page.
In addition to navigating between the Silverlight and XNA pages, you can render Silverlight controls with XNA content using the UIElementRenderer class. For more information about how to do this, see How to: Combine Silverlight and the XNA Framework in a Windows Phone Application.
NoteNote:
You cannot remove the Silverlight UIElement specified in the UIElementRenderer constructor from the visual tree.

ไม่มีความคิดเห็น:

แสดงความคิดเห็น