是否可以使用 Uno 平台将 AdMob 集成到 Android 应用程序中

max*_*han 2 admob xamarin.forms uno-platform

我有几个 UWP 应用程序想迁移到 Android。

我已经使用 Xamarin.Forms 迁移了一些我发现 Uno 平台似乎很棒。但是我在使用 Uno Platform 的 Android 项目中没有找到任何关于集成 AdMob 广告的信息。

有没有人做过?

Mar*_*und 5

是的,这是可能的,我已经能够在 Android 和 iOS 上的 Uno Platform 应用程序中使用它。我打算写一个博客帖子有关获取的AdMob和AdSense在Android,iOS和WASM运行,并发布的NuGet一个乌诺平台库,将尽一切繁重的你,敬请期待:-)。

现在,这是我当前使用的控件的未经编辑的原始版本。它要求您在Android 项目iOS 项目中安装 Google Play Services Ads NuGet 包。

安卓

#if __ANDROID__
using Android.Gms.Ads;
using Android.Widget;
using System;
using System.Collections.Generic;
using System.Text;
using Uno.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace SmsTicket.Core.Controls
{
    public partial class AdControl : ContentControl
    {
        public AdControl()
        {
            var adView = new AdView(ContextHelper.Current);
            adView.AdSize = AdSize.SmartBanner;
            adView.AdUnitId = "YOUR AD UNIT ID";
            HorizontalContentAlignment = HorizontalAlignment.Stretch;
            VerticalContentAlignment = VerticalAlignment.Stretch;
            var adParams = new LinearLayout.LayoutParams(
                LayoutParams.WrapContent, LayoutParams.WrapContent);
            adView.LayoutParameters = adParams;           
            adView.LoadAd(new AdRequest.Builder().AddTestDevice("YOUR TEST DEVICE ID").Build());
            Content = adView;
        }
    }
}
#endif
Run Code Online (Sandbox Code Playgroud)

IOS

#if __IOS__
using Google.MobileAds;
using System;
using System.Collections.Generic;
using System.Text;
using UIKit;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml;
using CoreGraphics;

namespace SmsTicket.Core.Controls
{
    public partial class AdControl : ContentControl
    {
        public AdControl()
        {
            HorizontalContentAlignment = HorizontalAlignment.Stretch;
            VerticalContentAlignment = VerticalAlignment.Stretch;
            Background = SolidColorBrushHelper.Red;
            Width = AdSizeCons.LargeBanner.Size.Width;
            Height = AdSizeCons.LargeBanner.Size.Height;
            Windows.UI.Xaml.Window.Current.Activated += Current_Activated;
        }

        private void LoadAd()
        {
            if (!(Content is BannerView))
            {
                var adView = new BannerView(AdSizeCons.LargeBanner)
                {
                    AdUnitID = "YOUR AD UNIT ID",
                    RootViewController = GetVisibleViewController()
                };
                adView.LoadRequest(GetRequest());
                Content = adView;
            }
        }

        Request GetRequest()
        {
            var request = Request.GetDefaultRequest();
            // Requests test ads on devices you specify. Your test device ID is printed to the console when
            // an ad request is made. GADBannerView automatically returns test ads when running on a
            // simulator. After you get your device ID, add it here
            request.TestDevices = new[] { Request.SimulatorId.ToString(), "YOUR TEST DEVICE ID" };
            return request;
        }

        UIViewController GetVisibleViewController()
        {
            UIViewController rootController;
            if (UIApplication.SharedApplication.KeyWindow == null)
            {
                return null;
            }
            else
            {
                rootController = UIApplication.SharedApplication.KeyWindow.RootViewController;
            }

            if (rootController.PresentedViewController == null)
                return rootController;

            if (rootController.PresentedViewController is UINavigationController)
            {
                return ((UINavigationController)rootController.PresentedViewController).VisibleViewController;
            }

            if (rootController.PresentedViewController is UITabBarController)
            {
                return ((UITabBarController)rootController.PresentedViewController).SelectedViewController;
            }

            return rootController.PresentedViewController;
        }

        private void Current_Activated(object sender, Windows.UI.Core.WindowActivatedEventArgs e)
        {
            LoadAd();
        }
    }
}
#endif
Run Code Online (Sandbox Code Playgroud)

还要确保仅有条件地包含 Ad 控件(因为我在此处仅提供了 Android 和 iOS 版本)。