Enable XNA Framework Events in Windows Phone Applications
Event-based systems place messages in a queue where they wait to be dispatched. XNA Framework event messages are placed in a queue that is processed by the XNA FrameworkDispatcher.
In an application that implements the Game class, the FrameworkDispatcher.Update method is called automatically whenever Game.Update is processed. This FrameworkDispatcher.Update method triggers event processing in the XNA Framework.
If you use the XNA Framework from an application that does not implement the Game class—for example, a Windows Phone application that uses the Silverlight application model—you must call the FrameworkDispatcher.Update method yourself to dispatch messages that are in the XNA Framework message queue. You can do this once per frame in a ticking timer loop, or you can implement the IApplicationService interface with a DispatcherTimer and a Tick event handler.
To do this, you can create an application service, and then register the service with the application.
To create an XNA Framework dispatcher service
Add a reference to the XNA Framework.
On the Project menu, click Add Reference, click the .NET tab, select Microsoft.Xna.Framework, and then click OK.
- Create using directives to use the types in the Microsoft.Xna.Framework and System.Windows.Threading namespaces without having to specify the namespace.
- Create a class that implements IApplicationService.
- Add a DispatcherTimer member.
In the constructor for the XNA Framework dispatcher service, do the following:
- Create a DispatcherTimer object and configure it to tick at 30fps, which is the standard rendering rate.
- Subscribe to the Tick event handler of the DispatcherTimer
- Call FrameworkDispatcher.Update once. This is important and must be done in the constructor to ensure proper behavior from the XNA Framework.
- Call FrameworkDispatcher.Update in the Tick event handler.
- Call StartService to start the dispatcher timer.
- Call StopService to stop the dispatcher timer.
To register the service with the application
Your implementation of the IApplicationService class must to be added to the ApplicationLifeTimeObjects collection. This can be done by editing App.xaml.
In the Application element of App.xaml, add an attribute called xmlsn:s and set the value to
clr-namespace:WindowsPhoneApplication;assembly=WindowsPhoneApplication. This will allow you to reference the class in the
<ApplicationLifetimeObjects>element. Note that WindowsPhoneApplication is the name of the namespace for the sample in this example, your application namespace should be used instead.
Add to or create an
<ApplicationLifetimeObjects/>element as a child to the
<ApplicationLifetimeObjects/>element, add an element which specifies the name of your implementation of the application service. In this example, the class that was created is named XNAFrameworkDispatcherService, so the element is added as
<Application x:Class="WindowsPhoneApplication.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:System="clr-namespace:System;assembly=mscorlib" xmlns:navigation="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Navigation" xmlns:s="clr-namespace:WindowsPhoneApplication;assembly=WindowsPhoneApplication"> <Application.ApplicationLifetimeObjects> <s:XNAFrameworkDispatcherService /> </Application.ApplicationLifetimeObjects> . . . </Application> </code> </li>