Hosting XNA within Silverlight on Windows Phone 7 and why you shouldn’t do it

When anyone starts working with XNA, it doesn’t take too long before they get fed up of dividing vectors by the viewport width and ask whether it’s possible to use the goodness of the WPF/Silverlight layout system to do their 2D stuff and the power of XNA to do the 3D stuff.  The answer is invariably “well, kinda… not really”.  Many have tried, some have had limited success and Microsoft have generally said “it’s something we want to do”. With the imminent release of Windows Phone 7 the question has never been more relevant – a platform on which you can only develop in Silverlight or XNA.

By far the best implementation of rendering XNA within a WPF app we’ve seen, is that detailed in Nick Gravelyn’s blog post: Rendering with XNA Framework 4.0 inside of a WPF application.  We really liked the pure approach he’d taken and decided to try the same thing on WP7 with Silverlight.  With a few alterations (largely around getting hold of a handle with which to create a Graphics Device) we got it working.  Magic.

So why not do it?  Put simply, it will fail Market Place Ingestion.  If your Silverlight application uses anything from the Microsoft.Xna.Framework.Game or Microsoft.Xna.Framework.Graphics assemblies your app will automatically fail Ingestion.  (As a side note, if your XNA app uses anything from the System.Windows.Controls namespace, you’re also in for ingestion failure.)

So what now?  Back to dividing vectors?  Well here at Red Badger we’ve been busily working away on a new layout system for XNA called XPF.  Expect more announcements soon, but needless to say if you’re longing for some familiar controls like a Grid or a ScrollViewer in XNA – watch this space…