Xamarin AdMob for Android - Catch Exceptions

时间:2016-06-26 20:51:16

标签: android xamarin xamarin.android admob

我在Xamarin Android上实施了AdMob。 我希望在广告加载时添加一些代码,并且我想抓住AdMob的例外情况。

我该怎么做?

1 个答案:

答案 0 :(得分:-1)

[assembly: ExportRenderer(typeof(AdMobView),typeof(AdMobRenderer))]
namespace <ProjectName>.Droid
{
    public class AdMobRenderer : ViewRenderer<AdMobView, Android.Gms.Ads.AdView>
    {
        public AdMobRenderer()
        {
        }

        protected override void OnElementChanged(ElementChangedEventArgs<AdMobView> e)
        {
            base.OnElementChanged(e);
            if (Control == null)
            {
                var ad = new AdView(Forms.Context);
                ad.AdSize = AdSize.Banner;
                ad.AdUnitId = Consts.BANNER_ID_ANDROID_BOTTOM;
                ad.AdListener = new MyAdListener(adViewRef);

                ad.LoadAd((new AdRequest.Builder()).Build());

                SetNativeControl(ad);
            }
        }
    }



    class MyAdListener : AdListener
    {

        public CMyAdListener(View refToView)
        {
            adViewRef = refToView;
        }

        public override void OnAdFailedToLoad(int errorCode)
        {
            Debug.WriteLine("error: " + errorCode.ToString());
            base.OnAdFailedToLoad(errorCode);
        }

        public override void OnAdClosed()
        {
            base.OnAdClosed();
        }

        public override void OnAdLeftApplication()
        {
            base.OnAdLeftApplication();
        }

        public override void OnAdLoaded()
        {
            base.OnAdLoaded();
        }

        public override void OnAdOpened()
        {
            base.OnAdOpened();
        }
    }
}