我正在尝试在 Xamarin.Android 的 C# 中实现激励广告。
Android留档中的代码片段是用Java编写的,通常我在C#中采用该代码没有太大问题,但现在它似乎确实是一个问题。
要在Java中加载一个RewardedAd,您必须执行以下操作(根据官方文档):
AdRequest adRequest = new AdRequest.Builder().build();
RewardedAd.load(this, "ca-app-pub-3940256099942544/5224354917",
adRequest, new RewardedAdLoadCallback(){
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
// Handle the error.
Log.d(TAG, loadAdError.getMessage());
mRewardedAd = null
}
@Override
public void onAdLoaded(@NonNull RewardedAd rewardedAd) {
mRewardedAd = rewardedAd;
Log.d(TAG, "onAdFailedToLoad");
}
});
现在这里的问题是,在这段代码中传递的对象<code>rewardedLoadCallback</code>也定义了方法实现(<code>onAdFailedToLoad</code>和<code>onAddLoaded</code>),而在C#中,这是无法实现的。
所以我的解决方案是创建我自己的类,MyRewardedAdLoadCallback
,并覆盖这些方法。
public class MyRewardedAdLoadCallback : RewardedAdLoadCallback
{
public override void OnAdLoaded(Java.Lang.Object p0)
{
base.OnAdLoaded(p0);
}
public override void OnAdFailedToLoad(LoadAdError p0)
{
base.OnAdFailedToLoad(p0);
}
}
然后,我简单地用这段代码加载奖励广告:
AdRequest adRequest = new AdRequest.Builder().Build();
RewardedAd.Load(this, "ca-app-pub-3940256099942544/5224354917", adRequest, new MyRewardedAdLoadCallback());
现在由于一些奇怪的原因,我在构建项目时收到此错误:
error: name clash: onAdLoaded(Object) in MyRewardedAdLoadCallback and onAdLoaded(AdT) in AdLoadCallback have the same erasure, yet neither overrides the other
public void onAdLoaded (java.lang.Object p0)
where AdT is a type-variable:
AdT extends Object declared in class AdLoadCallback
我试图在互联网上搜索这个错误,并得出结论,这是一个Java错误,与泛型类型有关(见这个SO问题)。然而,我在我的代码中没有看到任何通用类型,也没有在Android文档中看到关于奖励广告的内容。也没有关于我试图实现的奖励广告的Xamarin文档。
如果有人能帮我解决这个问题,我会非常高兴的。先谢谢你。
也许只是对回调进行子类化
class RewardedVideoAds : RewardedAdLoadCallback
{
private RewardedAd rewardedAd;
public RewardedVideoAds()
{
AdRequest adRequest = new AdRequest.Builder().Build();
RewardedAd.Load(MainActivity.Current, "ca-app-pub-3940256099942544/5224354917", adRequest, this);
}
public override void OnAdFailedToLoad(LoadAdError p0)
{
base.OnAdFailedToLoad(p0);
}
public override void OnAdLoaded(Java.Lang.Object p0)
{
base.OnAdLoaded(p0);
}
}
public abstract class InterstitialCallback : Android.Gms.Ads.Interstitial.InterstitialAdLoadCallback
{
[Register("onAdLoaded", "(Lcom/google/android/gms/ads/interstitial/InterstitialAd;)V", "GetOnAdLoadedHandler")]
public virtual void OnAdLoaded(Android.Gms.Ads.Interstitial.InterstitialAd interstitialAd)
{
}
private static Delegate cb_onAdLoaded;
private static Delegate GetOnAdLoadedHandler()
{
if (cb_onAdLoaded is null)
cb_onAdLoaded = JNINativeWrapper.CreateDelegate((Action<IntPtr, IntPtr, IntPtr>)n_onAdLoaded);
return cb_onAdLoaded;
}
private static void n_onAdLoaded(IntPtr jnienv, IntPtr native__this, IntPtr native_p0)
{
InterstitialCallback thisobject = GetObject<InterstitialCallback>(jnienv, native__this, JniHandleOwnership.DoNotTransfer);
Android.Gms.Ads.Interstitial.InterstitialAd resultobject = GetObject<Android.Gms.Ads.Interstitial.InterstitialAd>(native_p0, JniHandleOwnership.DoNotTransfer);
thisobject.OnAdLoaded(resultobject);
}
}
public abstract class RewardedAdLoadCallback : Android.Gms.Ads.Rewarded.RewardedAdLoadCallback
{
[Register("onAdLoaded", "(Lcom/google/android/gms/ads/rewarded/RewardedAd;)V", "GetOnAdLoadedHandler")]
public virtual void OnAdLoaded(Android.Gms.Ads.Rewarded.RewardedAd rewardedAd)
{
}
private static Delegate cb_onAdLoaded;
private static Delegate GetOnAdLoadedHandler()
{
if (cb_onAdLoaded is null)
{
cb_onAdLoaded = JNINativeWrapper.CreateDelegate((Action<IntPtr, IntPtr, IntPtr>)n_onAdLoaded);
}
return cb_onAdLoaded;
}
private static void n_onAdLoaded(IntPtr jnienv, IntPtr native__this, IntPtr native_p0)
{
RewardedAdLoadCallback thisobject = GetObject<RewardedAdLoadCallback>(jnienv, native__this, JniHandleOwnership.DoNotTransfer);
Android.Gms.Ads.Rewarded.RewardedAd resultobject = GetObject<Android.Gms.Ads.Rewarded.RewardedAd>(native_p0, JniHandleOwnership.DoNotTransfer);
thisobject.OnAdLoaded(resultobject);
}
}
这是一个技巧,可以让插页式广告和激励广告在 Xamarin.谷歌Play服务.Ads 版本 120.0.0 中工作。
将以下文件添加到您的项目中(需要使用不安全标志构建),并使用Android.Gms.Ads.Hack
命名空间加载您的Ads并将回调子类化。
例如,< code>InterstitialAd。Load变成了< code>global::Android。GMS . ads . hack . interstitialad . load 和< code > InterstitialAdLoadCallback 变成< code>global::Android。GMS . ads . hack . interstitialadloadcallback 。
回调类分别将OnInterstitialAdLoade()
和OnRewardedAdLoade()
虚拟方法添加到间隙和奖励广告加载回调中。
using System;
using System.ComponentModel;
using System.Diagnostics;
using Android.Content;
using Android.Runtime;
using Java.Interop;
namespace Android.Gms.Ads.Hack
{
public abstract class InterstitialAd : Java.Lang.Object
{
private static readonly JniPeerMembers _members = new XAPeerMembers("com/google/android/gms/ads/interstitial/InterstitialAd", typeof(InterstitialAd));
public unsafe static void Load(Context context, string adUnit, AdRequest request, AdLoadCallback callback)
{
IntPtr intPtr = JNIEnv.NewString(adUnit);
try
{
JniArgumentValue* ptr = stackalloc JniArgumentValue[4];
*ptr = new JniArgumentValue(context?.Handle ?? IntPtr.Zero);
ptr[1] = new JniArgumentValue(intPtr);
ptr[2] = new JniArgumentValue(request?.Handle ?? IntPtr.Zero);
ptr[3] = new JniArgumentValue(callback?.Handle ?? IntPtr.Zero);
_members.StaticMethods.InvokeVoidMethod("load.(Landroid/content/Context;Ljava/lang/String;Lcom/google/android/gms/ads/AdRequest;Lcom/google/android/gms/ads/interstitial/InterstitialAdLoadCallback;)V", ptr);
}
finally
{
JNIEnv.DeleteLocalRef(intPtr);
GC.KeepAlive(context);
GC.KeepAlive(request);
GC.KeepAlive(callback);
}
}
}
[Register("com/google/android/gms/ads/interstitial/InterstitialAdLoadCallback", DoNotGenerateAcw = true)]
public abstract class InterstitialAdLoadCallback : AdLoadCallback
{
private static readonly JniPeerMembers _members = new XAPeerMembers("com/google/android/gms/ads/interstitial/InterstitialAdLoadCallback", typeof(InterstitialAdLoadCallback));
static IntPtr class_ref => _members.JniPeerType.PeerReference.Handle;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
[EditorBrowsable(EditorBrowsableState.Never)]
public override JniPeerMembers JniPeerMembers => _members;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
[EditorBrowsable(EditorBrowsableState.Never)]
protected override IntPtr ThresholdClass => _members.JniPeerType.PeerReference.Handle;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
[EditorBrowsable(EditorBrowsableState.Never)]
protected override Type ThresholdType => _members.ManagedPeerType;
protected InterstitialAdLoadCallback(IntPtr javaReference, JniHandleOwnership transfer)
: base(javaReference, transfer)
{
}
[Register(".ctor", "()V", "")]
public unsafe InterstitialAdLoadCallback()
: base(IntPtr.Zero, JniHandleOwnership.DoNotTransfer)
{
if (!(base.Handle != IntPtr.Zero))
{
SetHandle(_members.InstanceMethods.StartCreateInstance("()V", GetType(), null).Handle, JniHandleOwnership.TransferLocalRef);
_members.InstanceMethods.FinishCreateInstance("()V", this, null);
}
}
private static Delegate cb_onAdLoaded;
private static Delegate GetOnAdLoadedHandler()
{
if (cb_onAdLoaded is null)
{
cb_onAdLoaded = JNINativeWrapper.CreateDelegate((Action<IntPtr, IntPtr, IntPtr>)n_onAdLoaded);
}
return cb_onAdLoaded;
}
private static void n_onAdLoaded(IntPtr jnienv, IntPtr native__this, IntPtr native_p0)
{
InterstitialAdLoadCallback? @object = Java.Lang.Object.GetObject<InterstitialAdLoadCallback>(jnienv, native__this, JniHandleOwnership.DoNotTransfer);
global::Android.Gms.Ads.Interstitial.InterstitialAd object2 = Java.Lang.Object.GetObject<global::Android.Gms.Ads.Interstitial.InterstitialAd>(native_p0, JniHandleOwnership.DoNotTransfer);
@object!.OnInterstitialAdLoaded(object2);
}
[Register("onAdLoaded", "(Lcom/google/android/gms/ads/interstitial/InterstitialAd;)V", "GetOnAdLoadedHandler")]
public virtual void OnInterstitialAdLoaded(global::Android.Gms.Ads.Interstitial.InterstitialAd interstitialAd)
{
}
}
public abstract class RewardedAd : Java.Lang.Object
{
private static readonly JniPeerMembers _members = new XAPeerMembers("com/google/android/gms/ads/rewarded/RewardedAd", typeof(RewardedAd));
public unsafe static void Load(Context context, string adUnit, AdRequest request, RewardedAdLoadCallback callback)
{
IntPtr intPtr = JNIEnv.NewString(adUnit);
try
{
JniArgumentValue* ptr = stackalloc JniArgumentValue[4];
*ptr = new JniArgumentValue(context?.Handle ?? IntPtr.Zero);
ptr[1] = new JniArgumentValue(intPtr);
ptr[2] = new JniArgumentValue(request?.Handle ?? IntPtr.Zero);
ptr[3] = new JniArgumentValue(callback?.Handle ?? IntPtr.Zero);
_members.StaticMethods.InvokeVoidMethod("load.(Landroid/content/Context;Ljava/lang/String;Lcom/google/android/gms/ads/AdRequest;Lcom/google/android/gms/ads/rewarded/RewardedAdLoadCallback;)V", ptr);
}
finally
{
JNIEnv.DeleteLocalRef(intPtr);
GC.KeepAlive(context);
GC.KeepAlive(request);
GC.KeepAlive(callback);
}
}
}
[Register("com/google/android/gms/ads/rewarded/RewardedAdLoadCallback", DoNotGenerateAcw = true)]
public abstract class RewardedAdLoadCallback : AdLoadCallback
{
private static readonly JniPeerMembers _members = new XAPeerMembers("com/google/android/gms/ads/rewarded/RewardedAdLoadCallback", typeof(RewardedAdLoadCallback));
static IntPtr class_ref => _members.JniPeerType.PeerReference.Handle;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
[EditorBrowsable(EditorBrowsableState.Never)]
public override JniPeerMembers JniPeerMembers => _members;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
[EditorBrowsable(EditorBrowsableState.Never)]
protected override IntPtr ThresholdClass => _members.JniPeerType.PeerReference.Handle;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
[EditorBrowsable(EditorBrowsableState.Never)]
protected override Type ThresholdType => _members.ManagedPeerType;
protected RewardedAdLoadCallback(IntPtr javaReference, JniHandleOwnership transfer)
: base(javaReference, transfer)
{
}
[Register(".ctor", "()V", "")]
public unsafe RewardedAdLoadCallback()
: base(IntPtr.Zero, JniHandleOwnership.DoNotTransfer)
{
if (!(base.Handle != IntPtr.Zero))
{
SetHandle(_members.InstanceMethods.StartCreateInstance("()V", GetType(), null).Handle, JniHandleOwnership.TransferLocalRef);
_members.InstanceMethods.FinishCreateInstance("()V", this, null);
}
}
private static Delegate cb_onAdLoaded;
private static Delegate GetOnAdLoadedHandler()
{
if (cb_onAdLoaded is null)
{
cb_onAdLoaded = JNINativeWrapper.CreateDelegate((Action<IntPtr, IntPtr, IntPtr>)n_onAdLoaded);
}
return cb_onAdLoaded;
}
private static void n_onAdLoaded(IntPtr jnienv, IntPtr native__this, IntPtr native_p0)
{
RewardedAdLoadCallback? @object = Java.Lang.Object.GetObject<RewardedAdLoadCallback>(jnienv, native__this, JniHandleOwnership.DoNotTransfer);
global::Android.Gms.Ads.Rewarded.RewardedAd object2 = Java.Lang.Object.GetObject<global::Android.Gms.Ads.Rewarded.RewardedAd>(native_p0, JniHandleOwnership.DoNotTransfer);
@object!.OnRewardedAdLoaded(object2);
}
[Register("onAdLoaded", "(Lcom/google/android/gms/ads/rewarded/RewardedAd;)V", "GetOnAdLoadedHandler")]
public virtual void OnRewardedAdLoaded(global::Android.Gms.Ads.Rewarded.RewardedAd rewardedAd)
{
}
}
}
Xamarin.Android是Xamarin的Android版本。
本文向大家介绍详解Xamarin.Android 利用Fragment实现底部菜单,包括了详解Xamarin.Android 利用Fragment实现底部菜单的使用技巧和注意事项,需要的朋友参考一下 本篇文章主要介绍了详解Xamarin.Android 利用Fragment实现底部菜单,分享给大家,具体如下: 效果图: 第一步:添加引用 引用 Crosslight.Xamarin.Android.
问题内容: 当用户访问列表中可见的项目时,如何在底部显示进度栏。 我编写了一个代码,其中使用Web服务获取数据,现在我想填充部分记录,因为我的JSON中大约有 630条记录 。 这是我用来从JSON获取数据并填充到RecyclerView中的整个代码。 这是我的JSON的外观,但是真实的JSON包含600多个记录: 有人可以指导我在我的代码中进行更改的地方吗? 每当用户使用进度条滚动到底部时,我想
问题内容: 我正在使用iOS的Google Maps API,并想使用标记聚类实用程序。我想出了如何显示聚簇标记,但是我想自定义标记。有人可以解释如何设置/更改每个标记或群集标记的图标和标题吗?示例代码将非常有帮助。 到目前为止,这就是我所拥有的。我不知道该如何处理renderClusters和更新函数。 问题答案: 在 Swift 4上 ,我找到了一种针对聚簇标记的干净解决方案,可以为聚簇使用自
问题内容: 我是SwiftUI的新手。我有三个视图,我希望它们在PageView中。我想像滑动浏览器一样滑动每个“视图”,并希望这些小点表示我所在的视图。 问题答案: 页面控制 您的页面浏览量 您的页面视图控制器 假设您有一个类似的观点 您可以像这样在主swiftUI视图中使用此组件。
问题内容: 我有指令,它是带有后退按钮的网站标题,我想单击可返回到上一页。我如何以有角度的方式做到这一点? 我努力了: 这是指令js: 但什么也没发生。我查看了有关$ location 的angular.js API,但未找到有关后退按钮或的任何信息。 问题答案: 您需要在指令中使用链接函数: 参见jsFiddle。