xamarin Interstitia 广告需要点击按钮两次才能显示
xamarin Interstitia ad needs to click button twice to show up
我想在点击按钮时显示插页式广告。
问题是它需要按两次按钮才能显示。
界面
public interface IAdInterstitial
{
void ShowAd();
}
Android class
[assembly: Dependency(typeof(AdMobInterstitial))]
namespace CoronavirusTest.Droid
{
class AdMobInterstitial:IAdInterstitial
{
InterstitialAd interstitialAd;
public AdMobInterstitial()
{
interstitialAd = new InterstitialAd(Android.App.Application.Context);
interstitialAd.AdUnitId = "ca-app-pub-2981452032483899/1747111924";
LoadAd();
}
void LoadAd()
{
var requestbuilder = new AdRequest.Builder();
interstitialAd.LoadAd(requestbuilder.Build());
}
public void ShowAd()
{
if (interstitialAd.IsLoaded)
interstitialAd.Show();
LoadAd();
}
}
}
按钮事件
private void Button_Clicked(object sender, EventArgs e)
{
IAdInterstitial adInterstitial = DependencyService.Get<IAdInterstitial>();
adInterstitial.ShowAd();
}
还在清单中添加了一些我在网上找到的代码,我在应用部分需要阅读这些代码
<activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" >
</activity>
原始代码在第一次点击按钮时加载广告,然后 IsLoaded 属性 return true 在第二次点击按钮时显示广告。
您可以试试下面的代码。
public interface IAdInterstitial
{
void LoadAd();
void ShowAd();
}
Android 实施:
[assembly: Dependency(typeof(AdMobInterstitial))]
namespace XamarinDemo.Droid.DependencyService
{
public class AdMobInterstitial : IAdInterstitial
{
InterstitialAd interstitialAd;
public AdMobInterstitial()
{
interstitialAd = new InterstitialAd(Android.App.Application.Context);
interstitialAd.AdUnitId = "ca-app-pub-3940256099942544/1033173712"; //PUT YOUR ID HERE
LoadAd();
}
public void LoadAd()
{
var requestbuilder = new AdRequest.Builder();
interstitialAd.LoadAd(requestbuilder.Build());
}
public void ShowAd()
{
if (interstitialAd.IsLoaded)
interstitialAd.Show();
}
}
}
用法:
public partial class Page1 : ContentPage
{
public Page1()
{
InitializeComponent();
}
protected override void OnAppearing()
{
base.OnAppearing();
DependencyService.Get<IAdInterstitial>().LoadAd();
}
private void btnLoad_Clicked(object sender, EventArgs e)
{
DependencyService.Get<IAdInterstitial>().ShowAd();
}
}
我想在点击按钮时显示插页式广告。 问题是它需要按两次按钮才能显示。
界面
public interface IAdInterstitial
{
void ShowAd();
}
Android class
[assembly: Dependency(typeof(AdMobInterstitial))]
namespace CoronavirusTest.Droid
{
class AdMobInterstitial:IAdInterstitial
{
InterstitialAd interstitialAd;
public AdMobInterstitial()
{
interstitialAd = new InterstitialAd(Android.App.Application.Context);
interstitialAd.AdUnitId = "ca-app-pub-2981452032483899/1747111924";
LoadAd();
}
void LoadAd()
{
var requestbuilder = new AdRequest.Builder();
interstitialAd.LoadAd(requestbuilder.Build());
}
public void ShowAd()
{
if (interstitialAd.IsLoaded)
interstitialAd.Show();
LoadAd();
}
}
}
按钮事件
private void Button_Clicked(object sender, EventArgs e)
{
IAdInterstitial adInterstitial = DependencyService.Get<IAdInterstitial>();
adInterstitial.ShowAd();
}
还在清单中添加了一些我在网上找到的代码,我在应用部分需要阅读这些代码
<activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" >
</activity>
原始代码在第一次点击按钮时加载广告,然后 IsLoaded 属性 return true 在第二次点击按钮时显示广告。
您可以试试下面的代码。
public interface IAdInterstitial
{
void LoadAd();
void ShowAd();
}
Android 实施:
[assembly: Dependency(typeof(AdMobInterstitial))]
namespace XamarinDemo.Droid.DependencyService
{
public class AdMobInterstitial : IAdInterstitial
{
InterstitialAd interstitialAd;
public AdMobInterstitial()
{
interstitialAd = new InterstitialAd(Android.App.Application.Context);
interstitialAd.AdUnitId = "ca-app-pub-3940256099942544/1033173712"; //PUT YOUR ID HERE
LoadAd();
}
public void LoadAd()
{
var requestbuilder = new AdRequest.Builder();
interstitialAd.LoadAd(requestbuilder.Build());
}
public void ShowAd()
{
if (interstitialAd.IsLoaded)
interstitialAd.Show();
}
}
}
用法:
public partial class Page1 : ContentPage
{
public Page1()
{
InitializeComponent();
}
protected override void OnAppearing()
{
base.OnAppearing();
DependencyService.Get<IAdInterstitial>().LoadAd();
}
private void btnLoad_Clicked(object sender, EventArgs e)
{
DependencyService.Get<IAdInterstitial>().ShowAd();
}
}