# XNA in WPF window

## Recommended Posts

AntiGuy    267
I'm working on a game in XNA but the problem Is I've made an Application using WPF and MDX. I can't load things to my game because they contain classes from MDX. It's really a pain, so I'm wondering is there any info on running XNA in a WPF window. I'm trying and I'm getting a lot of errors. I was thinking of making my classes independent of the two but seeing as I'm rather attached to many of the the classes (EVEN THOUGH THEY'RE IDENTICAL [Razz]!!) I'm fairly sure I can't, smoothly at least. So, any advice?

##### Share on other sites
AntiGuy    267
Ah screw it I guess I'll have to try and make things (sigh) independent. If anyone else is trying the same thing the closest I came was......... Here

Didn't work for some reason kept getting an error when I set the device.

##### Share on other sites
gharen2    520
Are you running vista or xp? Xna isn't compatible with vista yet, maybe that's the problem.

I'm using Xna inside a winform on window xp, the same way as that sample you posted, and it works great.

##### Share on other sites
AntiGuy    267
Nope running XP and doesn't work. The demo doesn't even work. Odd.

##### Share on other sites
gharen2    520
Then the first thing I'd do is make sure you've converted the initialisation enums from mdx to xna correctly. The backbuffer format and such.

I threw this together based on my code (which is too complex to post here). Runs fine for me:

using System;using System.Collections.Generic;using System.Text;using System.Windows.Forms;using System.Drawing;using Microsoft.Xna.Framework;using Microsoft.Xna.Framework.Graphics;namespace ConsoleApplication1{    class Program    {        static void Main(string[] args)        {            Form form = new Form();            form.ClientSize = new Size(800, 600);            form.Show();            PresentationParameters presentationParameters = new PresentationParameters();            presentationParameters.BackBufferFormat = SurfaceFormat.Bgr32;            presentationParameters.BackBufferHeight = form.ClientSize.Height;            presentationParameters.BackBufferWidth = form.ClientSize.Width;            presentationParameters.SwapEffect = SwapEffect.Discard;            presentationParameters.IsFullScreen = false;            presentationParameters.EnableAutoDepthStencil = true;            presentationParameters.AutoDepthStencilFormat = DepthFormat.Depth24Stencil8;            presentationParameters.PresentOptions = PresentOptions.DiscardDepthStencil;            presentationParameters.PresentationInterval = PresentInterval.Immediate;            GraphicsDevice device = new GraphicsDevice(GraphicsAdapter.DefaultAdapter, DeviceType.Hardware,                form.Handle, CreateOptions.HardwareVertexProcessing, presentationParameters);            while (form.Created)            {                if (form == Form.ActiveForm)                {                    device.Clear(Microsoft.Xna.Framework.Graphics.Color.CornflowerBlue);                    device.Present();                }                Application.DoEvents();            }        }    }}

##### Share on other sites
AntiGuy    267
Hey it works now! Thanks a load! It seems things didn't work when I use SurfaceFormat Rgb32. I'll look into that weird little error.

You're a life saver [wink]!

[Update] More think I'd like to note that I looked at the XNA migration guide it's stupid to have it used backwards like that. I'm speaking without knowing the exact reason why but as far as I can know it's stupid : )!