立即在 Xamarin Forms 中显示 Google Admob 插页式广告

我正在从事 Xamarin 表单项目,并且在所有三个平台上都实施了插页式广告(Google admob 不支持 UWP)。广告工作正常,但显示延迟 4 到 5 秒。我想要的只是完成那个延迟,这样他们就可以立即显示。


PCL 类。


public interface IAdmobInterstitial

{

    void Show(string adUnit);

}

机器人代码。


public class InterstitialAdListener : AdListener

{

    readonly InterstitialAd _ad;


    public InterstitialAdListener(InterstitialAd ad)

    {

        _ad = ad;

    }


    public override void OnAdLoaded()

    {

        base.OnAdLoaded();


        if (_ad.IsLoaded)

            _ad.Show();

    }

}


public class AdmobInterstitial : Controls.IAdmobInterstitial

{

    InterstitialAd _ad;


    public void Show(string adUnit)

    {

        var context = Android.App.Application.Context;

        _ad = new InterstitialAd(context);

        _ad.AdUnitId = adUnit;


        var intlistener = new InterstitialAdListener(_ad);

        intlistener.OnAdLoaded();

        _ad.AdListener = intlistener;


        var requestbuilder = new AdRequest.Builder().AddTestDevice("302E90D530B2193F59FDD7F22A11B45A");

        _ad.LoadAd(requestbuilder.Build());

    }

}

iOS 代码。


public class AdmobInterstitial : IAdmobInterstitial

{

    Interstitial _adInterstitial;


    public void Show(string adUnit)

    {

        _adInterstitial = new Interstitial(adUnit);

        var request = Request.GetDefaultRequest();

        _adInterstitial.AdReceived += (sender, args) =>

        {

            if (_adInterstitial.IsReady)

            {

                var window = UIApplication.SharedApplication.KeyWindow;

                var vc = window.RootViewController;

                while (vc.PresentedViewController != null)

                {

                    vc = vc.PresentedViewController;

                }

                _adInterstitial.PresentFromRootViewController(vc);

            }

        };

        _adInterstitial.LoadRequest(request);


    }

}

在 PCL 页面上调用插页式广告。


DependencyService.Get<IAdmobInterstitial>().Show("(id will come here)");

Navigation.PushAsync(new Page());

广告显示完美,但有延迟。我想先显示广告,然后再显示页面。


繁华开满天机
浏览 188回答 1
1回答

泛舟湖上清波郎朗

所以我刚刚解决了这个问题。这是代码。PCL 类。向您的界面添加另一个功能。public interface IAdmobInterstitial{&nbsp; &nbsp; void Show(string adUnit);&nbsp; &nbsp; void Give();}机器人代码。将 _ad.Show() 放入新函数中。public class InterstitialAdListener : AdListener{&nbsp; &nbsp; readonly InterstitialAd _ad;&nbsp; &nbsp; public InterstitialAdListener(InterstitialAd ad)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; _ad = ad;&nbsp; &nbsp; }&nbsp; &nbsp; public override void OnAdLoaded()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; base.OnAdLoaded();&nbsp; &nbsp; &nbsp; &nbsp; //if (_ad.IsLoaded)&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; _ad.Show();&nbsp; &nbsp; }}public class AdmobInterstitial : Controls.IAdmobInterstitial{&nbsp; &nbsp; InterstitialAd _ad;&nbsp; &nbsp; public void Show(string adUnit)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var context = Android.App.Application.Context;&nbsp; &nbsp; &nbsp; &nbsp; _ad = new InterstitialAd(context);&nbsp; &nbsp; &nbsp; &nbsp; _ad.AdUnitId = adUnit;&nbsp; &nbsp; &nbsp; &nbsp; var intlistener = new InterstitialAdListener(_ad);&nbsp; &nbsp; &nbsp; &nbsp; intlistener.OnAdLoaded();&nbsp; &nbsp; &nbsp; &nbsp; _ad.AdListener = intlistener;&nbsp; &nbsp; &nbsp; &nbsp; var requestbuilder = new AdRequest.Builder().AddTestDevice("302E90D530B2193F59FDD7F22A11B45A");&nbsp; &nbsp; &nbsp; &nbsp; _ad.LoadAd(requestbuilder.Build());&nbsp; &nbsp; }&nbsp; &nbsp; public void Give()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (_ad.IsLoaded)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _ad.Show();&nbsp; &nbsp; }}现在,在 PCL 的 MainPage 的构造函数中调用 Show() 函数,并在按下下一页按钮时调用 Give() 函数。public MainPage()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; InitializeComponent();&nbsp; &nbsp; &nbsp; &nbsp; DependencyService.Get<IAdmobInterstitial>().Show("ca-app-pub-3940256099942544/1033173712");&nbsp; &nbsp; }private void button_Clicked(object sender, EventArgs e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; DependencyService.Get<IAdmobInterstitial>().Give();&nbsp; &nbsp; &nbsp; &nbsp; Navigation.PushAsync(new Percentage());&nbsp; &nbsp; }这仅适用于 Android。iOS 也一样。我希望它可以帮助其他程序员。:)
打开App,查看更多内容
随时随地看视频慕课网APP