Blograby

Using Interop from Silverlight to XNA

The following are some of the features of Windows Phone 7 that you may want to use in a Silverlight application where interop with XNA is required:

For the last two features in the preceding list, you must create an XNA asynchronous dispatcher service and initialize it in the constructor or startup event in your App.xaml.cs file. This is required to raise the events in Silverlight when they are raised by the XNA objects.

Creating and Using an XNA Dispatcher Service

The XNA run-time event mechanism is different from the Silverlight event mechanism. Events raised by XNA objects are available within the XNA game loop, but they are not automatically available to the Silverlight event mechanism. To expose XNA events to Silverlight application code, you can take advantage of the Silverlight ability to use application extension services. These are services that add functionality to an application, and that can be reused by many applications. They circumvent the requirement to use inheritance. For more information, see “Application Extension Services” on MSDN (http://msdn.microsoft.com/en-us/library/dd833084(VS.95).aspx).

The following code example shows an XNA asynchronous event dispatcher service that you can use in your Silverlight applications.

C#
using System;
using System.Windows;
using Microsoft.Xna.Framework;
using System.Windows.Threading;
namespace YourAppNamespace
{
public class XNAAsyncDispatcher : IApplicationService
{
private DispatcherTimer frameworkDispatcherTimer;
public XNAAsyncDispatcher(TimeSpan dispatchInterval)
{
this.frameworkDispatcherTimer = new DispatcherTimer();
this.frameworkDispatcherTimer.Tick
+= new EventHandler(frameworkDispatcherTimer_Tick);
this.frameworkDispatcherTimer.Interval = dispatch
Interval;
}
void IApplicationService.StartService(ApplicationService
Context context)
{ this.frameworkDispatcherTimer.Start(); }
void IApplicationService.StopService()
{ this.frameworkDispatcherTimer.Stop(); }
void frameworkDispatcherTimer_Tick(object sender, EventArgs e)
{ FrameworkDispatcher.Update(); }
}
}

The DispatcherTimer class is a .NET Framework class that is included in Silverlight. It executes on the application thread and can raise events at preset intervals. In the preceding XNAAsyncDispatcher example, the Tick event executes the Update method of the FrameworkDispatcher class, which is part of the XNA Framework. The FrameworkDispatcher class is an alternative mechanism to drive the update loop if the base class is not an XNA game.

For more information, see “DispatcherTimer Class” (http://msdn. microsoft.com/en-us/library/system.windows.threading.dispatchertimer. aspx) and “FrameworkDispatcher.Update” (http://msdn.microsoft.
com/en-us/library/microsoft.xna.framework.frameworkdispatcher. update.aspx) on MSDN.

After you create an XNA dispatcher service class, you can instantiate it in the constructor of your application class in the App.xaml.cs file. The typical tick rate for capturing and exposing XNA events is 50 milliseconds.

C#
public App()
{
// other constructor code here …
this.ApplicationLifetimeObjects.Add(new
XNAAsyncDispatcher
(TimeSpan.FromMilliseconds(50)));
}

 

Exit mobile version