maui 无法直接支持 admob,需通过平台原生代码(android/ios)调用 google admob sdk,并借助接口抽象与依赖注入桥接。须配置权限与应用 id、定义跨平台 iadservice 接口、分平台实现广告逻辑、在 maui 页面中嵌入原生容器调用,不可依赖 nuget 一键集成。

MAUI 本身不直接支持 AdMob,因为 Google 官方未提供 .NET MAUI 原生 SDK。要在 MAUI 中显示 AdMob 广告(如横幅、插页、激励视频),必须通过平台特定代码(Android/iOS)调用原生 AdMob SDK,并用 MAUI 的 Platform-agnostic API(如 Handler 或 DependencyService)桥接逻辑。
1. 先配置 Android 和 iOS 原生环境
这是最关键的前置步骤,跳过会导致运行时崩溃或广告不加载。
-
Android:在
Platforms/Android/AndroidManifest.xml中添加网络权限和 AdMob 应用 ID:<uses-permission android:name="android.permission.INTERNET" /> <meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy" /> -
iOS:在
Platforms/iOS/Info.plist中添加:<key>GADApplicationIdentifier</key> <string>ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy</string>
并确保启用 App Tracking Transparency(iOS 14+ 必需)。
2. 创建跨平台广告服务接口
定义一个统一的 C# 接口,让 MAUI 页面调用,屏蔽平台差异:
public interface IAdService
{
void ShowBanner(string adUnitId, View container);
void ShowInterstitial();
void ShowRewardedAd(Action onUserEarned);
}然后在 MauiProgram.cs 中注册实现类(如 AdServiceImplementation)为依赖服务。
3. 在 Android 和 iOS 中分别实现广告逻辑
以 Banner 广告为例:
-
Android 实现:使用
AdView,添加到ViewGroup(如LinearLayout),注意在主线程操作 UI; -
iOS 实现:使用
GADBannerView,设置RootViewController和约束(Auto Layout),并处理生命周期(如ViewWillAppear); - 两种平台都要处理广告加载失败回调(
OnAdFailedToLoad),避免空指针或卡死。
4. 在 MAUI 页面中嵌入 Banner 容器并调用
推荐用 ContentView + Handler 自定义渲染器方式,更可控:
<ContentView x:Class="MyApp.Views.AdBannerView" />
在后台代码中获取平台视图容器(如 Android 的 LinearLayout),传给 IAdService.ShowBanner() 方法。不要直接在 XAML 放 WebView 或自定义控件模拟广告——无法触发真实填充和计费。
基本上就这些。核心是“平台原生实现 + MAUI 接口桥接”,不是装个 NuGet 就能跑。Google 目前仍主推 Flutter 和原生开发,MAUI 的广告生态靠社区维护(如 Microsoft AppCenter 已停更,Sharpnado 等项目有轻量封装)。别指望一键集成,但按步骤走,一周内可上线合规 Banner。










