Monogame With Xamarin or Maui (IOS /Android)

thank you for the answer

no I don’t necessarily want to use the marcojack library
i just want it to work
I tried to integrate Xamarin.GooglePlayServices.Ads.Lite
without the library I received these error messages
I’m at the stage I just want to integrate this dependency without errors but without success I don’t know why I have the messages I sent above

Here is a barebones Android example that builds without issue. Create a fresh Android MG 3.8.1 project named “Project1”, then replace these 2 files.

Project1.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net6.0-android</TargetFramework>
    <SupportedOSPlatformVersion>23</SupportedOSPlatformVersion>
    <OutputType>Exe</OutputType>
    <ApplicationId>com.companyname.Project1</ApplicationId>
    <ApplicationVersion>1</ApplicationVersion>
    <ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
    <UseMauiEssentials>true</UseMauiEssentials>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="MonoGame.Framework.Android" Version="3.8.1.303" />
    <PackageReference Include="Xamarin.GooglePlayServices.Ads.Lite" Version="121.2.0" />
  </ItemGroup>
</Project>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="Project1.Project1" android:versionCode="1" android:versionName="1.0">
  <uses-sdk android:minSdkVersion="23" android:targetSdkVersion="31" />
  <uses-feature android:glEsVersion="0x00020000" android:required="true" />
  <uses-permission android:name="com.google.android.gms.permission.AD_ID" />
  <application android:label="Project1">
    <meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="ca-app-pub-3940256099942544~3347511713" />
  </application>
</manifest>

Note: the APPLICATION_ID provided here is just the test ID. If you cannot build this: run Build > Clean in VS, close VS, delete your obj and bin folders, restart VS, and try again. If you still cannot build, there is something wrong with your dev stack.

2 Likes

great thank you :+1:

I will test this and get back to you

thanks again

I don’t know what this means

sorry
it is a mistakeI wanted to sayI have to add <UseMauiEssentials>true</UseMauiEssentials> ?

I added Maui Essential I didn’t get any errors I’m satisfied
now i tried something like this in Activity1.cs

InterstitialAd.Load(Game1.Activity, "ca-app-pub-3940256099942544~3347511713",
               new AdRequest.Builder().Build(), new InterstitialCallbackInherit());

I managed to add a simple banner finally
Thanks a lot :+1: :grinning:
I based myself on an old class that I found on the forum it makes simple banners work, however the full page does not work for the moment

I used this class if it helps

using System;

using Android.Gms.Ads;
using Android.Gms.Ads.Interstitial;
using Android.Views;
using Android.Widget;


namespace Project1.Publicity
{
    public static class AdController
    {
        public static InterstitialAd interstitialHandle = null;
        public static AdView AdView = null;

        public static event EventHandler AdClosed;

        // AD ID's This is a test use yours from Admob
        public static string adID1 = "Your ID"; // standard full page ad
        public static string adID3 = "Your ID"; // banner ad

        public static bool adRegularLoaded = false;
        public static bool adBannerLoaded = false;

        public static void InitBannerAd(FrameLayout fl)
        {
            LinearLayout ll = new LinearLayout((Activity1)Game1.Activity)
            {
                Orientation = Orientation.Horizontal
            };

            ll.SetGravity(GravityFlags.CenterHorizontal | GravityFlags.Bottom);

            MobileAds.Initialize(Game1.Activity);

            AdView = new AdView((Activity1)Game1.Activity)
            {
               AdUnitId = adID3,
               // AdUnitId = adID1,
                // AdSize = AdSize.Banner //full
                //AdSize = AdSize.FullBanner //full
                AdSize = AdSize.FullBanner //full
            };

            ListeningBanner listening = new ListeningBanner();

            AdView.AdListener = listening;
            AdView.LoadAd(new AdRequest.Builder().Build());
            AdView.Visibility = ViewStates.Visible;

            ll.AddView(AdView);
            fl.AddView(ll);
        }


        private static void Listening_AdClosed(object sender, EventArgs e)
        {
            AdClosed?.Invoke(null, null);
        }


    }
    
    internal class ListeningRegular : AdListener
    {
        public event EventHandler AdClosed;

        public override void OnAdLoaded()
        {
            AdController.adRegularLoaded = true;
            base.OnAdLoaded();
        }

        public override void OnAdClosed()
        {
            //AdController.interstitialHandle.LoadAd(new AdRequest.Builder().Build());
           // AdClosed?.Invoke(this, null);
            base.OnAdClosed();
        }
        public override void OnAdOpened()
        {
            base.OnAdOpened();
        }
    }

    internal class ListeningBanner : AdListener
    {
        public override void OnAdFailedToLoad(LoadAdError p0)
        {
            base.OnAdFailedToLoad(p0);
        }

        public override void OnAdLoaded()
        {
            AdController.adBannerLoaded = true;
            base.OnAdLoaded();
        }

        public override void OnAdClosed()
        {
            AdController.AdView.LoadAd(new AdRequest.Builder().Build());
            base.OnAdClosed();
        }
    }
}

In Activity1.cs

    public class Activity1 : AndroidGameActivity
    {
        private Game1 _game;
        private View _view;

        private FrameLayout fl;


        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            _game = new Game1();
          // _view = _game.Services.GetService(typeof(View)) as View;
         
            fl = new FrameLayout(this);
            fl.AddView((View)_game.Services.GetService(typeof(View)));

            AdController.InitBannerAd(fl);

            SetContentView(fl);
            //SetContentView(_view);
            _game.Run();
        }

    }


}
1 Like

I don’t know if this method is still valid or are we limited only with banners?

Thanks The Kelsam
I want to reward you for your help

1 Like

I think it says limits on thr GitHub… might be one of them…that’s why I was waiting for him to fix it…I only wanted full page interstitial

Which MonoGame templates should I use in Visual Studio for Mac if I want to use .NET MAUI in an iOS and Android project?
Is it still necessary to use the MonoGame iOS, Android and the Shared/Game Library project template to create an iOS project and an Android project that use the same code from the Shared/Game Library project?

I managed to display the banner on iOS in a separate window, but I can’t display both the banner window and the game window at the same time. Do you have any ideas?

Hi, :grinning:
When I comment out RunGame(), the banner appears. How did you manage to display both the banner and the game window? In my case, for Android, I used FrameLayout. Thank you in advance.

  public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
        {
            window = new UIWindow(UIScreen.MainScreen.Bounds);

            adMobViewController = new AdMobViewController();
            window.RootViewController = adMobViewController;

            window.MakeKeyAndVisible();

            RunGame();

            return true;
        }

We don’t use banner ads.

Thank you for your response :grinning:. Is an interstitial the same thing? I managed to display the banner just now by calling the game window before the banner. However, I am now facing another problem :face_with_raised_eyebrow:where the touch functionality of the game no longer works because the banner covers the game’s touch input. Do you have any workaround ideas? Thank you in advance.

How to add the AdMob ad unit ID to .net maui
Please

I am also wondering if this is possible. I see Xamarin is dead now as of May 2024 so probably it is not wise to keep using it:

Microsoft says we must switch to Maui. How might a Maui & Monogame interaction work?

Would you start a Maui project in Visual Studio 2022, and then how would you get Monogame into it?

Could you set a Monogame camera to render to something like a RenderTexture (as it is done in Unity) and have that texture be displayed into a Maui VisualElement background?

ie. So the user is only ever seeing Maui VisualElement’s but sees the rendering of the game camera through that?

Thanks for any thoughts. :slight_smile:

You need to add this code to your project in order to be able to use the latest Admob versions.

dans .csproj:
<UseMauiEssentials>true</UseMauiEssentials>
Ou <UseMaui>true</UseMaui>

Hi,

I’ve created a fork of monogame with support for MAUI on iOS and Android.
I worked the last couple of days on it and will publish it soon.
You can mix MAUI UI with a MonoGame Graphics Engine in a view behind.

Regards
Ronny