Embed MonoGame inside an AvaloniaUI control.

This question might be better suited for the AvaloniaUI team but I figured I would try here first.

I’m in the process of porting my editor from WPF to Avalonia so that it can be cross platform (Windows, Linux and MacOS) and I just ran into my first roadblock. Previously, I’ve been using MonoGame.WpfCore to render my game in a WPF app and I’d like to do the same in Avalonia.

Has anyone tried that before? Is it even possible?

Unfortunately I know next to nothing about OpenGL, Skia and such. Looking at the source code, it looks like one wold be able to use Avalonia’s OpenGlControlBase as seen in this example to accomplish that.

I’d be willing to port MonoGame.WpfCore to Avalonia myself but as of now, I have no clue how I could pull this off. Any help would be greatly appreciated.

Thanks! :slight_smile:

1 Like

i use WPF plus one of the monogame backing store samples that speaks to the directx… and comiled it with Netcore 6 using microsoft poritng tool, some command line things that converted the whole level editor… . 99% was without a hitch… even the scripting works… the monogame stuff says invalid handle at runtime but nearly everythign works. now… so I m thinking why avalonia… it was amazing that it worked… i havent tried wpf on other plafroms but its supposed to work…

i havent tried wpf on other plafroms but its supposed to work…

Where did you see this? As far as I know, WPF doesn’t work on other platformers. Also, Avalonia is in active development where as development on WPF basically stopped years ago, aside from making it work in new versions of .NET.

I would also love this. Does anyone have some info on it?

i havent tried wpf on other plafroms but its supposed to work…

Where did you see this? As far as I know, WPF doesn’t work on other platformers.

Maybe referring to the fact that WPF now runs on .Net Core which is mostly cross-platform? But you’re right, WPF itself, and WinForms for that matter, are not cross-platform. They rely heavily on Windows-specific technologies. That’s exactly why I switched to Avalonia, so that I can reuse most of my code and WPF knowledge.

I still don’t have any info so I might ask again in Avalonia’s repo at some point. It seems like it would be possible by using a RenderTarget2D, it’s shared handle and their OpenGL renderer API. Actually a good implementation should probably support other graphics backend as well since they have a plug-able rendering architecture (Skia maybe?).

But all of this goes quite a bit over my head. :slight_smile:

I’ll try to keep this thread updated if I get more info. I’m glad to see a bit of interest in this!

UPDATE: im stiil working on this… some progress ( i can’t advise a path im just gong to report my progress and plan, and its not quite all working i think very close).

avalonia is very alluring or mabye parts of it:
theres avalon dockng, some script debugging, code completion, inspection and even stepping , tons of tooling out there… but too many forks forks, and general flux form me rght now… and i have years of legacy toolng code i doubt i can jsut replace it in a week or so.

for level editors using wpf with MG views underlays or windows or tabs:
mg 3.81 and wpf interop w net6 now works in basic samples. Its true wpf netcore is windows only ( for now , and i dont see a motive , except maybe nativie ARM windows support for Surface , for MIcrosfoft to put payrollled staff on it) it adds a differnt sdk Microsoft.NetCore.App and Microsfto.WindowsDesktop.App im not sure how dead it is but its in such widespread use i dont think it will be left behind, and there are overlapping new UI XAML frameworks and we dont know which of those they will kill off internally, or if avalonia or one of its forks, is the best way forward.

i pushed a PR to both https://github.com/craftworkgames/MonoGame.WpfCore   and the https://github.com/MarcStan/monogame-framework-wpfinterop/pulls   samples, to update to net6 and mg 3.81.   But i might have not done it the simplest canonical way which might be using ANYCPU and the smallest possible project files.  also these are breaking changes once u move to mg3.81 regarding 48 and netstandard2.0 ( if u have legacy wpf net 4.8 pieces, netstandard 2.0 with mg links) so they might not get merged but you can see the tiny changes i made.    Netstandard 2.0 is pretty dead, so even if its supposed to work there are issues.

NOTES: updating non graphics, libs like physics engines or whatever, to from netstandard 2.0 to net 6 does obviously speed them up 20% or so. its worth it even if you dont think you have to. but you lose direct net 48 compatibility on parts linked to mg 3.81 so you have to on those… the windows compatibilty thing might get around some legacy net 48 ui you mght have… i think upgrade-assistant might introduce you to it , but as i said, it makes a mess.

So the samples that use Dxinterop work fine on windows 11 for me. but my remanng issue when I pull the Monogame.wpfcore content control into my level editor (wpf .48 not net6.windows) DX, i dont even step into the view model part that creates the render target thing…and i get a issue with window handle method not found excepting, something like htat. suspect is some issue with virtualization or the view model pattern or sharpdx/direct x bndng inssues.,… I remove teh SharpDX package nuget as its not needed, ( i think) same issue tho. So next i wll try the mark stan way…since its not using datacontext or view model and is more complete (antialiasing) … or some combination of the two.

As for Avalon id love to start using it so i can deploy a modding tool outside of windows on wndows GL or whatever , but I have not seen anyone produce a sample in the Avalon community and there are TONS of FORKS and tons of nauced ISSUES… its been tried a long time, they write to a bitmap, or try for the backing store… its a long , old , thread… But im not going back there also becasue the real WPF is very nuanced itself, and the netcore build (windows only) version is not showing me regression now, in legacy property sheets and docking and UI.

aslo Silk .NEt s is actively doing windows as i type so they might make the DX bindings on which both wpf - mG samples are built, but have abandoned for 2 years, obsolete. hte is SDL to watch, etc.

. so i decided not to go to avalonia but updating all my legacy docking and wpf controls to net6, and all my netstandard 2.0 to net 6… I don’t recommend the upgrade-assistant but it might give you clues if you have legacy pieces, lke the wndows compatibility thing, whch mght not even be needed… i made wpf net 6 lib projects form scratch istead from the templates n vs2022 preview.

I have devcomponets docking ( dead, but works fine) …CSScript for plugins now updated o rosyln, if you remove namespaces on any scripts, and , it is actively maintained…though the newest is not on nuget but hes built it. GitHub - oleg-shilo/cs-script.npp: CS-Script (C# Intellisense) plugin for Notepad++ (x86/x64)

now i use a sln that targets android, console desktop GL ( untested) and shared net 6 lib that has my game class, content w shaders, and windows DX app net 6… so my wpf class uses some of the draw code. problem is I added too many project configurations and all the permutations are too complex… so im trying ANYCPU and new projects … seeing if i can deploy on android and everything else at least while all the code churn from microsoft slows down. My test rigs are not tested on anything but windows, and a samsung phone and emulators… but they build shaders and run. there is no repeat code, just a link to the content.mgcb its basically the old netcore monogame sample with some rendertarget but its not clean or nice, i just dont have the time to push it yet.

ill update my public rig if i get a chance to tidy it up a bit.

So, my plan is stay on Windows wpf net6core, get my legacy code to work… fix the MG view since it works in the sample its should be possible , ( mark stan uses a Game class but I might merge some of his workarounds to the other one, which doesn’t not use a game class just a device service but has issues wiht tabbed dialogs and docking) to allow some user modding using Mrya or something later mabye if i actually want to deploy that , some games do better when there is a modding community. anyways sorry TLDR, i dont have the answers about Avalonia, im just sharing what ive decide to do because things are changing so fast in platform repos, i just want my tools to work as before with the new sdk and projects and all net6 for now while MSFT keeps blazing int this direction. sorry for the typos but im swamped with this for weeks.

found this 2 days ago its aboug 7 days old… seems to works ok… had to buildit … nugets and versions are painful…

sponsor if you can’t contribute…

1 Like

Oh, great find @Damian_Eaglestein! I will definitely try this out as soon as possible.

I looked at the code really quickly and it seems that this solution actually use instances of the Game class instead of implementing a new IGraphicsDeviceService. Interresting. Ultimately, it still copies backbuffer data into a writeable bitmap that it then draws into a custom Avalonia rendering control. Seems more or less straightforward!

In my engine, I try to decouple everything from the Game class as much as possible and I move most of the core update/draw logic into my own Engine class so this might require a few changes for my use-case.

I must admit I sort of forgot about Avalonia for a while as I’ve been exploring ImGui which works great for my case too but comes with a few minor restrictions which Avalonia could solve. I guess I’ll have to keep on experimenting with both! :smiley:

1 Like

i do as well…, not use the game class. i was following bread crumbs and asked a guy, he answers Yes, wiht two links, the vault is 5 days old, good timing i guess because its been all NO , or mabye possible since like 2017. I think he started trying a couple years back .I don’t use the game class and it uses an overcomplicated MVVM architecture. But its works ok, for us the sample had to be built. but a nuget should just work. There are also some React dependencies, doing some expemiments with it… its not super high performance, but 60 fps is all you can get from wpf and i never do anything on update, either, take key input differently, but its not as limiting as the mark stan way… and using my own game loop and timers anywas as the wpf timers are useless, and dispatchers and all that.

it really you should be able to take an avalonia hello world from template, then stick an avalonia.Inside.Monogame nuget and it should fly but theres i think everythignis in preview and its maybe got to be Net7…so still testing feasibily on this… .avaywasy it works well enoug now tyring tabs and the hard stuff… docking, etc… if you find a good dockiong thing plesas share becuase there are several and im stuck with an old DevComponents tahts out of business ( the risk of closed source) , they liquidate the company and do not open the source and its a miracle I could even update my WPF+ the craftwork one net 7 windows. If you want native UI on mobile ( mabye maui is worth a look for that) but for a real IDE like thing on desktop, i would use this , seems Avalonia is the best gaem in town and well sponspored now. But without JetBrains rider there no code completion on this. (mabye an extension tho) … im not sure how portable XAML is but id guess about 90%. . Iike IMGUI, avalonia will render UI and have its own ui but its not immediate mode, so should use less power, and the React stuff can be very great if you undertsand…or use Notiication properites, and that stuff…

When I was still on WPF and Visual Studio, I was using AvalonDock for docking which was quite popular and worked really well. It had a few quirks with my MonoGame to WPF solution but it was good enough. Same thing as you said, locked at 60FPS with some noticeable lag from time to time but as I said, it was good enough. I assume the performance hit always seems to come from copying backbuffer data into a writeable bitmap?

I moved my dev environment onto a Linux machine, hence the reason why I was looking into Avalonia as a cross-platform replacement for WPF. I also ditched Visual Studio for JetBrains Rider with the Avalonia plugin which has been working great for me. For docking on Avalonia, I’ve been using GitHub - wieslawsoltes/Dock: A docking layout system. It’s very MVVM oriented and a bit confusing to use at first but once you get it up and running, it’s pretty nice! I’ve seen another docking solution for avalonia somewhere but I don’t have the link to it right now. I’ll post it if I can find it again.

Note that I’m not looking at runing anything Avalonia on mobile, I just want to use it as a desktop application for an editor so I have no idea how well any of this would perform on mobile.
And of course, there are many other UI toolkits like GTK, QT, MAUI, etc. but I’ll stick for what I know for now. :stuck_out_tongue:

1 Like

Yes and his code works in wasm…mayve we can make a shadertoy for fx files. Ill host it but not much time foe that, or soemone can and get git sponsores. Maybe not Wasm but at least real time .plenty of scritping tools out there in avalonia land. Just put a avaloniaedit wan hlsl completing, thing, run the content buikdder , relaod shader,or something and or maybe script the game assemoly…cs-script or othrr…if needed to see the result. Also he has a graph node thinhy if someone wanted to go crazy unity killer with it…

And 60hz is limit for wpf ui thread timers and such.dispatcher and such… outside of that u can do gpu.game loops at whateter speed.in bk threads, ai or whstever at timed or maxed out at 4000 + fps then just draw at 60 hz… so his system, even if writeable bitmap, not a direct swap or shared backing render target, its good enough for prototyping fx files, and otherassets, not deploying. So that where maui moght be better or myra other…but for desktop, ill try his dock next…thxs…i saw one othrt as well , all have issues…tabs, tearoff floating ,layout saving , dock groupa ,all that is hard .

yea, im usng Dock from the same vault and see the other one but i thik its too complex…the old free docking thing is wndows-wpf only, now almost abandoned, and mine is better even if not been updated for years, it works… and on on net7. the nuget of the avalonia.inside for monogame is giving me a weird missng brush runtime error so next probably try mergng ihs source… i dont love MVVM. I bind rght to the model whenever i can wth an undo layer on the model which is weird but ts fine. but reactiive s seems a better way than Notify and they seem to have both.

However, Recently there is a ton of flux , overhauls, and regresson and its prerelease. 20 + emalis a day if you watch the vault i had to shut it off… the scrping editor thing i use for plugins to game objects, based on AvaloniaEdit ,that ran on wpf, for c# parsing for scripting , isnt fully there yet in the avalon N platfrom verson, AFAIK , seems… avalonia UI , They refactored a ton of on virtualizng panels code which i dont use, and its not as smooth as wpf… it got no central command, and but its definite charged up and active… mabye too active…

… but i pulled out any old net48 from his dock… There are netstandard2 dependencies but I can link to netcore6 dlls. no problem and have it all on Net7. im going to shelf t for a few months or years for my purposes thouhg its too late in my cycle … ill share the publc vault if we gate an MG control in a tab to resize… i see that save load / layouts works, i would not port and old big WPF complex IDE thng to it, but definitely looks promising for a new projects its 7 years old…

and UI has some touch and stylus on windows, or you can use monogame input for that support. for a shader graph viewer /IDE , he has a node editor thing as well we will try out …wieslawsoltes (Wiesław Šoltés) · GitHub… there mght be draw tools and such… if we get any of of it workng ill share my vault on it…since this is all general purpose stuff… i wont maintain it but someone can fork it and take contributions if it gets as good as a real time shader experimenting tool like shadertoy… and it does run in WASM i think, not sure w MG though.

also for mobile i would just use Maui and native UI…and the monogame template the way it is. this i Avalonia is best for desktop, but they are doing a test on mobiles/tablets, so the lack of linux is not an issue. . i dont want to float windows or expact big layouts on those, and seeing all the issue on right to left text like arabic, localization issues, and virtual keyboards, maui seems mabye a better way, or just stay on xamarin .

sticking to what you konw is best considering, if you can target just windows and android for an indie game, thast billions of devices right there…but knowing wpf and not having jetbrains, does not mean you Know Avalonia, its a bit differnt and WPF is quirky enough to drive u up a wall.

whats cool vs big engines, for 2d anayways, and not considering the consoles aversion to IL, is in Net7, by not doing AOT can give you wicked SIMD intrincics benefits finally delivered after like 20 years of empty promise. like Vector128, and 2-pass jittting optimizers , intrinsics under the hood , that would not be possible in AOT. old net SIMD code was mindboggling , but i think i can do some arrays and GPU stuff with this… net 6 is LTS but many simd-heavy engines just skipped to net 7 to avoid avx - specific code. see this 28 x on regex with big scripts…
Highlighted Performance Wins with .NET 7