Monogame and WPF. Link the game engine

Hello there

I read this article and a lot of other articles that explain how to render Monogame or XNA in WPF. Sadly, all the articles I found seem to be outdated.

I’m at a point where I’d like to use the GraphicDevice created by the Game1 class in order to create a RenderTarget2D out of it. The problem is that as soon as I call Run(), the main thread gets busy with Monogame’s engine.
Am I on the right path here or should I use another solution ?

I don’t know how helpful it is, but there have been some projects that solved that, for example you can look into

The author of this software - @tgjones in this community forum and @tim_jones on twitter - is very competent and you maybe able to get some answers from him

Thank you @kosmonautgames for the link. I’ve seen this project during my research but it is introduced as

a WPF framework designed specifically for building IDE-like applications

Which is not what I’m looking for.

I will look into the repository and try to find out how Monogame works with WPF in it!

I’ve been round and round on most of the articles / samples relating to this.

I have had a workable application using the approach in that article but the performance was cr*p.
In addition, I’d been looking for a cross-platform approach using OpenGL but the underlying solution uses a WriteableBitmap which is AFAIK Windows-only anyway

With that mind, I’ve reverted to using a Windows UAP project with a SwapChainPanel which hosts MonoGame using the standard MonoGame VS template. My requirements are only to have menus, toolbars and dialogs working in WPF so this is fine for me. The cross-platform side of things I’ll look at a bit later - I’ve moved all my Game Engine source into a shareable project ready for when it’s needed.

The problem with Windows UAP is that it can’t be compiled for windows 7…

Ah I understand where you’re coming from… I toyed with the idea of a “desktop” project (and I’m still planning to look into it) but IMHO it needs some additional MonoGame classes along the lines of the ones in WindowsUniversal i.e. versions of UAPGamePlatform.cs / UAPGameWindow.cs and XamlGame.cs for the Windows Desktop. I felt I needed a better understanding of how those classes interact with the other Game classes before attempting it.

I’m happy to collaborate on anything you decide to work on in that area but I think the SwapChainPanel is the way forward…

There’s an open issue on embedding MonoGame. The sample referred to there (http://xbox.create.msdn.com/en-US/education/catalog/sample/winforms_series_1) shows how you can embed an XNA game in WinForms and it works with minor modifications for MonoGame. Not sure if the same thing works for WPF though.
As for cross-platform, the method used in the sample can currently only work for DirectX, since no Game is instantiated and GraphicsDevice with OpenGL relies on a Game instance being present, but that should be fixed.
Embedding should be easier in the future, but AFAIK noone is working on this right now.

The “minor modifications” I had to make a Winforms sample (based on an article based on that article etc etc) included having to create a Game Instance since I had a run-time exception without it.The same issue was present with WPF.

The article based on the article was here:

http://jaquadro.com/2013/03/embedding-your-monogame-game-in-a-winforms-control/

Are you using OpenGL then? Using the latest develop version of MonoGame with Windows DX I only had to remove some parameters for functions that aren’t implemented in MonoGame, then it worked fine.

Not currently - I’m using WPF + Monogame + SwapChainPanel (i.e. DirectX)

Edit: It all felt a bit like I was hacking around to get it to work…and because it relied on a WriteableBitmap it was going to be cross-platform anyway.

Yeah, it is a bit hacky. Do you remember where the exception came from? I’m looking into this a bit to see if I can contribute.

I’ve still got the solution + project files - I’ll see if I can re-create the error…