-
-
Notifications
You must be signed in to change notification settings - Fork 0
MAUI Usage
AMDevIT.Admob.Wrapper.MAUICross provides a XAML banner control, dependency-
injection services for full-screen formats, platform logging adapters, and one
consent abstraction for all MAUI targets.
using AMDevIT.Admob.Wrapper.MAUICross;
public static MauiApp CreateMauiApp()
{
MauiAppBuilder builder = MauiApp.CreateBuilder();
builder.UseMauiApp<App>();
builder.Logging.AddDebug();
builder.UseAMDevITAdMobWrapper();
return builder.Build();
}Do not register the concrete ad services again unless you intentionally want to replace the library implementation.
Inject IAdMobConsentService into the application's startup coordinator or
first-page view model. Complete Privacy and Consent UMP before making a
mobile banner visible or loading a full-screen ad.
One simple view-model pattern is:
public bool CanLoadAds { get; private set; }
public async Task InitializeAdsAsync(CancellationToken cancellationToken = default)
{
if (!consentService.IsSupported)
{
// Desktop: enable only the fallback UI, not full-screen services.
CanLoadAds = true;
return;
}
ConsentGatheringResult result = await consentService.GatherConsentAsync(
cancellationToken: cancellationToken);
if (!result.CanRequestAds)
return;
string applicationId = OperatingSystem.IsAndroid()
? "ca-app-pub-XXXXXXXXXXXXXXXX~YYYYYYYYYY"
: string.Empty;
await consentService.InitializeAsync(applicationId, cancellationToken);
CanLoadAds = true;
}<ContentPage
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:admob="clr-namespace:AMDevIT.Admob.Wrapper.MAUICross;assembly=AMDevIT.Admob.Wrapper.MAUICross">
<admob:BannerAd
AdUnitId="ca-app-pub-3940256099942544/6300978111"
AdSize="Adaptive"
IsVisible="{Binding CanLoadAds}"
AdLoaded="OnBannerLoaded"
AdFailed="OnBannerFailed">
<admob:BannerAd.FallbackTemplate>
<DataTemplate>
<Border Padding="12" BackgroundColor="#EEEEEE">
<Label
Text="Advertising is unavailable on this platform."
HorizontalTextAlignment="Center" />
</Border>
</DataTemplate>
</admob:BannerAd.FallbackTemplate>
</admob:BannerAd>
</ContentPage>Available banner events and commands include loaded, failed, clicked,
impression, and dismissed. AdFailedEventArgs contains the native error code
and message.
BannerAdSize |
Logical size |
|---|---|
Adaptive |
Uses the arranged container width |
Banner |
320x50 |
LargeBanner |
320x100 |
MediumRectangle |
300x250 |
FullBanner |
468x60 |
Leaderboard |
728x90 |
Adaptive banners reload when the arranged width changes. Fixed banners are centered and report their actual requested size to MAUI.
Inject one or more services:
public sealed class AdCoordinator(
IInterstitialAdService interstitial,
IAppOpenAdService appOpen,
IShowableRewardedAdService rewarded)
{
public Task ShowInterstitialAsync(CancellationToken cancellationToken = default)
{
return interstitial.LoadAndShowAsync(
"ca-app-pub-3940256099942544/1033173712",
cancellationToken);
}
public Task ShowAppOpenAsync(CancellationToken cancellationToken = default)
{
return appOpen.LoadAndShowAsync(
"ca-app-pub-3940256099942544/9257395921",
cancellationToken);
}
public async Task ShowRewardedAsync(CancellationToken cancellationToken = default)
{
rewarded.AdRewardEarned += OnRewardEarned;
await rewarded.LoadAndShowAsync(
"ca-app-pub-3940256099942544/5224354917",
cancellationToken);
}
private static void OnRewardEarned(object? sender, AdReward reward)
{
Console.WriteLine($"Reward: {reward.Amount} {reward.Type}");
}
}The services publish loaded, failed-to-load, shown, dismissed, clicked, impression, and failed-to-show events.
- One native load can be active per service instance.
- Starting another load before the native callback arrives throws
InvalidOperationException. - Cancellation stops the caller's wait but cannot cancel the already-started native SDK operation.
- Wait for the native callback before reusing that service instance.
- Full-screen ads are one-shot and must be loaded again after dismissal.
Windows and Mac Catalyst render FallbackTemplate. Consent is a safe no-op,
but full-screen services are not available and throw
PlatformNotSupportedException. Check platform support or hide mobile-only
commands. See Desktop Fallbacks.