Array

Using Interop from Silverlight to XNA

Must Read

The Actors

There are three actors in the scenario supported by the architecture,the ISV, the subscribers, and the surveyors. Tailspin - the...

How Astrive Student Loans Can Help You Pay For College and Grad School Tuition and Expenses

Many students choose Astrive student loans as a solution to pay for their education. They are available in several...

Student Loan Consolidation Program – Some Important Debt Advice to Heed

Student loan consolidation program for university and college students is an important option, especially if they find themselves suffering...

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:

  • Accessing input gestures on the device, such as drag, pinch, and hold gestures
  • Playing multi-channel audio
  • Advanced sound effects (such as using the XNA Dynamic- SoundEffectsInstance class, which lets you submit packets on the fly, vary the pitch, and apply 3-D effects to the sound)
  • Recording audio

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)));
}

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest News

Underlight As Accent, For Power and The Main Light for Photography

Underlighting, in which fill or accent light comes from under the topic, is not widely used technique in the traditional Portraits,...

How To Fix Overexposure As A Creative Tool, The Complete Guide

As an creative tool, overexposure is sort of underrated. What I’m close to propose could be a deliberate and well thought out technique for...

7D Mark II is Canon’s best DSLR cameras without full-frame sensor.

The expected long-awaited Canon EOS 7D Mark II are shipping in November for $ 1,799 without a lens. With a higher price tag, you...

Low key photography and How to isolate your subject.

Low key photography doesn’t depend on underexposure to make its point; the key to low key is that the majority of tones, even correctly...

High Key Lighting Techniques for Professional Photographers

I’ve written many times about high key lighting techniques and how to achieve them. The term “high key” is a bit misleading. As I’ve...

More Articles Like This

blograby facebook like page