Monogame With Xamarin or Maui (IOS /Android)

Hi,
Has anyone ever worked with monogame on Xmarin.form or Maui?
for IOS and Android projects?
Does anyone have a starter example?
thanks in advance

I’m looking for a way to use ADmob and thought of hijacking the problem this way view render on Xmarin.form or Maui then use ADMobe
If you have any other suggestions, I’m all ears

thanks in advance

You can get the MAUI equivalent of Xamarin.Essentials by adding <UseMauiEssentials>true</UseMauiEssentials> to your csproj. We are using admob in both Android and iOS this way (using Xamarin.GooglePlayServices.Ads.Lite and Xamarin.Google.iOS.MobileAds, respectively).

2 Likes

Hi,
thank you very much for your answer

How did you do ?
you create a Maui or xamarin project
then how do you integrate the monogame viewer?
and admob?
how do you call monogame in Maui?
Do you have a basic sample?

thanks in advance

You mean if I add <UseMauiEssentials>true</UseMauiEssentials> to my csproj
(monogame)
I can use admob without problem?
if yes
I just have one question mark left how can I call banners in the monogame viewer

thanks in advance

Yes, create a MonoGame Android/iOS project and add <UseMauiEssentials>true</UseMauiEssentials> to the csproj to get the MAUI/NET6 equivalent of Xamarin Essentials (which is needed by the latest versions of Admob libs mentioned).

I didn’t have much luck with it and ended up making my own lib like this to fit our needs, but you can try to make use of something like this (go-between lib so you don’t call the admob apis directly, per platform): GitHub - marcojak/MauiMTAdmob (this is about the closest thing you’re going to find to a “basic example” - which really doesn’t exist for this type of thing since it is so specific to your stack, ad types, app/game flow, etc - and these APIs change so much so often). If you’re using MG 3.8.1 (and therefore .NET6) then your ads stack should start with MauiEssentials and the latest version of the Admob nuget needed for your target platform.

If you’re using an older version of MG then you’d instead just use Xamarin.Essentials and the earlier versions of the admob apis that have .NET Core/Standard/Whatever prereqs instead.

2 Likes

Thank you very much for your answer
I see better
there is just the inlay of the banner
if I understand correctly if I configure things correctly, they appear independently of the monogamous viewer?
it’s just this step that intrigues me

thanks again

The Admob API renders the ad using the same Activity instance that MonoGame uses. For example, to load in an interstitial ad you’d do something like this: InterstitialAd.Load((AndroidActivity)Game1.Activity, AD_ID, new AdRequest.Builder().Build(), new InterstitialCallbackInherit()); Note the passing of your game’s AndroidActivity instance (a MonoGame template class which inherits from AndroidGameActivity which is a MonoGame class that inherits from Xamarin’s Android.App.Activity), which is how Admob gets the “viewer”, as you call it, which it uses to render the ads. Therefore, both your game and the ads use the same Android.App.Activity instance and therefore the same “viewer”/renderer/windowing.

2 Likes

Hello,
Thank you very much it helps me a lot
I was going to get started and concerning IOS how can we attach viewer level ads?

I don’t know how I can do to thank you and support you?
can I make a participation?

1 Like

Thing is, i dont know how big your project is or how far along but consider that Xamarin is being Sunsetted or replaceable for many situations, in a sort of way, because of Silk, for new projects. Maui future is uncertain…The recent announcement is not clear on Maui. like many or most, Microsfoft UIs like UWP, and many other, its not been received well ( no linux support) wiill likely be killed. I will mabye try the Maui essentials as as suggested (GitHub - marcojak/MauiMTAdmob) . But if its as bloated as Xamarin, then im not sure if its ideal.

However MG 3.8.1, Xamarin, The samples, GameActivity works fine now… im running MG 3.81 /xamarin/Net 6, no major problems , but the tool chain is bloated, slow and almost none of all the bloat in Xamarin/Maui is being used as my UI is built on Monogame. Since im shipping soon I’ll stick with in but if you are looking at longer term plans, there should be a way to ditch Xamarin and Maui especially if you simply using it to host an MG view and some ads.

Here is the announcement:
Silk.NET, .NET 6, and the Sunsetting of Xamarin Support - Silk.NET

This simpler and slimmer and newer approach looks like its beign already used for quite some time.

its simplly hosting a window and in your case, the ad. So… .I hope someone takes a hard look at the new Silk Windowing which hooks directly to the Java sdk without the whole Xamarin slow to build and basically unused UI stuff… I have not tried it myself, but someone should be able to hook in a Monogame view somehow via GL bindings and Netcore6… then you can hopefully just use the Java and the Monogame without all the Xamarin stuff, I think… I hope… And teh ad hosting via Java/ google APIs directly.

I jsut dont know how to make the rendertarget hook to OpenGL… . I hope someone notices and makes a sample though, MG->Silk Windowing for Android/OSX, va openGL and locked shared Buffer to rendertarget like the old WPF integration… I don’t know of admob abstraction in Silk, for IOS / Android but in MauiMTAdmob, the source might have it and it might be extractable, or just host it via SWIFT and Java its just an adhost.

Rght now, to build and deploy Xamarin, or a phone or emulator, even a simple game on Androind the simplest things takes minutes so I can go around it someday.

1 Like

Hello,

Thank you for your answer, it sheds light on another angle for me, however, it raises questions
I work on a small project that runs on Android / Ios

I propose to create an open source project around it to help the community
I think it will help a lot of people something easily updated and long term

Does qualquin here have the ability to handle this?

I want to launch the project in open source

a project that looks like marcojack but specially designed for Monogame that can be downloaded directly from Nuget

I couldnt find any vault or project Qualquin on nuget or gitub…

for now I want to just stay on Xamarin …But since there is no netstandard 2.0 support on mg 3.8.1 but finally got through the transition,
I will try TheKelsams advice first. Im may to have to do this next for ads.

Going off topic of ad hosting, and meta as usual, its TLDR: but ill share it since i typed it and have alot invested…

looking deeper at the broad subject, of XAML /maui…based hosts… that we can embedd MG into…and show Ads… on Ios and Android with either one codebase , all c# , or via very simple platfrom-specific host code for the ads or the window hosts…in java or swift or whatever.

Im lookat at this just for a day or so… for the sake of future proofing. scoping features to ship or defer, and to see if i can get a faster toolchain, i may not need to. as I said Xamarin might be bloated and slow ,but its alive , mature, and it works fine wiht MG latest/ and Net 6 or Net 7. For Net 7 is just change 6s to 7s… no issues so far for me. But I havent hosted ads yet.

I dont know how much slimmer Maui is than Xamarin… Or if ill ever use the UI features. Or deploy it on desktops

One other I turned up specifically about ad hosting is is…
GitHub - dmanning23/AdMobBuddy: Library for viewing AdMob ads into a Xamarin.Android, Xamarin.iOS, and MonoGame apps havent look deeply tho.

Re. what i mentioned earlier about Silk, I have less high hopes for it on deeper look …I dont even know how old or relevant that announcement is that i put earlier. At some point im might try the Silks Android windowing sample, see its dependencies, give it a quick try , look if its possible to embedded a MG view via a locked render target or something , via open GL…
There isnt even an iOS sample…and I can’t even build the android one now. I’m merely hosting one window, but behind the senses there might be quite a bit of java / Mono interop.

Also , yet another option is GitHub - AvaloniaUI/Avalonia: A cross-platform UI framework for .NET. Now in Beta are iOS and Android samples. Avalonia and MG interop should be possible via a shared rendertarget, as was used in the WPF interop. I have not tried Avalonia UI nor Avalonia as a replacement for WPF.

I dont know if Maui will be killed… If it will really replace WPF on desktop, with docking windows and all stuff .
It doesnt have a Linux plan at all wrt desktop. For some thats a nonstarter. WPF is 15 years old and very naunced. Avalonia has Linux support is quite active and mature, but heavily nuaced, forked and tons of issues.

You may want that complex Windowing and XAML UI stuff for a custom Level Designer like I had to build with a team like most game studios, but in my usecase, in my actual game i dont use any XAML UI, I just need one MG window host, will need an ad host, (just full page ads ) and I made just simple buttons, touch joystick, pinch zoom, built on monogame from scratch for the mobile version.

. Lots of things to try… and consider , can be overwhelming… Definitely must be careful not to build on a doomed platform … thants why i lose focus , ramble on , and monitor too many vaults. sighhh

1 Like

if you can get admob to work with maui windowing
can you share the sample?

i plan to because i probably need it… In next couple months…
but i might just do android and Im only doing full page simple video ads, not polluting banners or tricky ones :slight_smile: … it will try admob before any native way tho.

i hope to get to this this next week just to show i have a monetizaton strategy that even works on people.w no money… iright now I can only use VS 2022 preview that updates very week… and Net 7. Last i tried to build Maui hello world it didnt work at all so, not promising anything. Don’t they have samples?

1 Like

thank you for your help

When you say you had no luck with that
did you manage to add mauiessential without errors?

Because I get something like this:

/usr/local/share/dotnet/packs/Microsoft.Android.Sdk.Darwin/32.0.476/tools/Xamarin.Android.Common.targets(3,3): 
Error XAGJS7028: System.IO.FileNotFoundException: Could not load assembly 'Microsoft.Maui, 
Version=1.0.0.0, Culture=neutral, PublicKeyToken='. Perhaps it doesn't exist in the Mono for Android profile?

File name: ‘Microsoft.Maui.dll’
thank you

I find myself between two problems

i feel like the integration maui essential integration the monogame project 3.8.1 doesn’t like it
and when i try to switch back to xamarin using marcojack’s lib
I have a hundred errors I don’t know what to do

thanks in advance

No, I don’t have an issue building MG 3.8.1 with Maui Essentials (targeting .NET6 and Android SDK 31). I would say try creating a brand new MG Android project, add the <UseMauiEssentials>true</UseMauiEssentials> directive and see if that builds. If so, you have something project-specific hanging you up. If not, your dev setup/stack/SDKs are messed up.

when downgrading stuff, ms dev caches things in user folders, and i had to go to my user data and clean recent files. i dont remever exactly everything but I found it by searching over my user data… c:\users\dami for teh most recent files from when everything broke. restroing and erasing the nugets wasnt enoug… dump temp files as well… try again… Imusing vs 2022 preview… its updates every week, really net7 is fine, i need them to finish fixes, anywasy, and my other vs 2022 official relese its in a state where it cant uninstall or repair, because a install didnt get all teh way through.

anywasy aside from this its stable enough… the caches are so that builds are faster subsequently but… well i guess downgrades ofr similar might leave new stuff around in local cache … as long as its in your user data it should be save to remove… especially if the file is very new and near the time when you downgraded something and it all broke… mabye i put that on reddit and it helped someone…dont remember …

def use a fresh clone periodically in case of missing files not put in git, and commit often as well when your code is stable… so that its not your own accidental changes…