Xamarin.forms 是否可以使用Uno平台将AdMob集成到Android应用程序中
我有一些UWP应用程序想迁移到Android 我已经用Xamarin.Forms迁移了一些 我发现Uno平台似乎很棒。但我没有发现任何关于在使用Uno平台的Android项目中集成AdMob广告的信息Xamarin.forms 是否可以使用Uno平台将AdMob集成到Android应用程序中,xamarin.forms,admob,uno-platform,Xamarin.forms,Admob,Uno Platform,我有一些UWP应用程序想迁移到Android 我已经用Xamarin.Forms迁移了一些 我发现Uno平台似乎很棒。但我没有发现任何关于在使用Uno平台的Android项目中集成AdMob广告的信息 有人已经这么做了吗?Uno平台不会阻止您使用任何第三方(尤其是在手机上)。如果有xamarin绑定,您可以像在代码中一样使用它,就像在xamarin.Forms应用程序中使用它一样。如果它是一个影响视图的库,您很可能会创建一个自定义控件,并通过C#与第三方类交互 如果库没有xamarin绑定,您可
有人已经这么做了吗?Uno平台不会阻止您使用任何第三方(尤其是在手机上)。如果有xamarin绑定,您可以像在代码中一样使用它,就像在xamarin.Forms应用程序中使用它一样。如果它是一个影响视图的库,您很可能会创建一个自定义控件,并通过C#与第三方类交互 如果库没有xamarin绑定,您可以按照microsoft文档创建一个
好消息!admob有一个microsoft支持的绑定,过去曾在uno应用程序中使用过。是的,这是可能的,我已经能够在Android和iOS上的uno平台应用程序中使用它。我计划写一篇关于让AdMob和AdSense在Android、iOS和WASM上运行的文章,并在NuGet上发布一个Uno平台库,它将为您完成所有繁重的工作,所以请继续关注:-) 现在,这里是我当前使用的控件的未编辑的原始版本。它要求您在和中安装Google Play Services Ads NuGet软件包 Android
#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版本)。很高兴它有帮助:-)@马丁齐克蒙德谢谢!你曾经在广告上写过那篇博文吗??@Maximus啊,还没有。我会把它放在我的积压工作之上!伟大的谢谢