【问题标题】:xamarin Interstitia ad needs to click button twice to show upxamarin Interstitia 广告需要点击两次按钮才能显示
【发布时间】:2020-03-28 16:56:55
【问题描述】:

我想在按钮点击时展示插页式广告。 问题是它需要按两次按钮才能显示出来。

界面

public interface IAdInterstitial
{
    void ShowAd();
}

Android 类

[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>

【问题讨论】:

    标签: c# android xamarin interstitial


    【解决方案1】:

    原始代码在第一次按钮点击时加载广告,然后 IsLoaded 属性返回 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();
        }     
    }
    

    【讨论】:

    • 如果我想在广告后导航到主屏幕,我只需在按钮点击中使用 Navigation.PushAsync(myPage)?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-01
    • 2018-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多