WPF Interop - Updated with Input Question

Hello all.

I’ve got a basic 2D game up and running and I’m at a point where I need to start creating levels so I’m looking to build a simple level editor. The latest release notes state that there is now official support for WPF interop but outside of many outdated XNA articles, I haven’t been able to find samples or examples that show Monogame 3.2 WPF interop.

If there are any official 3.2 samples out there can someone please point me to them? If not, it would be much appreciated someone with interop experience could outline the steps I need to take to get started.

Thanks!

1 Like

Here is an example: https://github.com/CartBlanche/MonoGame-Samples/tree/master/WpfInteropSample

We created it a while ago but I think it should still work with MonoGame 3.2.

1 Like

Thanks for the reply, HelmutG. I’ve managed to use your sample to get the rendering going but now I’ve hit another bump: input, which I need to get a proper level editor going. Attempting to get both keyboard and mouse input results in nothing happenings. The mouse position, for example is always 0,0. I even made a few changes to your sample to get input going but I got the same result. Any thoughts on how to get input working?

Example Input Logic:

var mouseY = Microsoft.Xna.Framework.Input.Mouse.GetState().Position.Y;
var halfScreenHeight = _graphicsDevice.Viewport.Height / 2;

if(mouseY < halfScreenHeight)
{
    GraphicsDevice.Clear(Color.SteelBlue);
}

else
{
    GraphicsDevice.Clear(Color.Green);
}

In MonoGame the keyboard and mouse input are created by the Windows Forms game window. If you do not create a normal MonoGame windows game, then keyboard/mouse input is not available. You can try two things:

A) If you create a pure WPF application, you can use the WPF input instead of the MonoGame input devices.

B) You can create a normal MonoGame game to have the MonoGame game window, and open a WPF window from inside your game.

With editors this is generally what we’ve done… just use MonoGame for the rendering work.