gpt4 book ai didi

xamarin.forms - 是否可以使用 Uno Platform 将 AdMob 集成到 Android 应用程序中

转载 作者:行者123 更新时间:2023-12-04 03:52:22 24 4
gpt4 key购买 nike

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

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

有人做过吗?

最佳答案

是的,这是可能的,我已经能够在我的 Android 和 iOS 上的 Uno Platform 应用程序中使用它。我打算写一个 blogpost关于让 AdMob 和 AdSense 在 Android、iOS 和 WASM 上运行,并在 NuGet 上发布一个 Uno Platform 库,它将为您完成所有繁重的工作,敬请期待:-)。

目前,这是我当前使用的控件的未经编辑的原始版本。它要求您在 Android project 中安装 Google Play 服务广告 NuGet 包在iOS project .

安卓

#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

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

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

关于xamarin.forms - 是否可以使用 Uno Platform 将 AdMob 集成到 Android 应用程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59859836/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com