Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 75885

Google Admob and Xamarin Forms Stacklayout

$
0
0

I'm trying to implement the google admob component in my app. I keep getting CustomAdView namespace missing. Can someone please shed some light on how to get google admob working?

In my lifehacks.ios file I have:

    using Xamarin.Forms;
    using CoreGraphics;
    using Xamarin.Forms.Platform.iOS;
    using UIKit;
    using LifeHacks.iOS;
    using GoogleAdMobAds;

[assembly: ExportRenderer(typeof(CustomAdView), typeof(CustomAdViewRenderer))]

namespace LifeHacks.iOS
{

    public class CustomAdViewRenderer : ViewRenderer
    {

        const string AdmobID = "xxxxxx";

        GADBannerView adView;
        bool viewOnScreen = false;

        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.View> e)
        {
            base.OnElementChanged(e);

            if (e.NewElement == null)
                return;

            if (e.OldElement == null)
            {
                adView = new GADBannerView(size: GADAdSizeCons.SmartBannerPortrait)//, origin: new PointF(0, 0))
                {
                    AdUnitID = AdmobID,
                    RootViewController = UIApplication.SharedApplication.Windows[0].RootViewController
                };

                adView.DidReceiveAd += (sender, args) =>
                {
                    if (!viewOnScreen) this.AddSubview(adView);
                    viewOnScreen = true;
                };

                GADRequest request = GADRequest.Request;

                #if DEBUG
                request.TestDevices = new string [] { GADRequest.GAD_SIMULATOR_ID };
                #endif

                adView.LoadRequest(request);
                base.SetNativeControl(adView);
            }
        }
    }
}

In my shared lifehacks.cs shared:

                public class CustomAdView : View
                {
                }

                static readonly StackLayout mainStackLayout = new StackLayout {
                    Spacing = 0,
                    VerticalOptions = LayoutOptions.FillAndExpand,
                };

                var ad = new CustomAdView();
                var layoutWithAd = new RelativeLayout();

                layoutWithAd.Children.Add(
                    ad, // put the add at the bottom of the Page
                    Constraint.Constant(0), 
                    Constraint.RelativeToParent((parent) => { return parent.Height - 50; }),
                    Constraint.RelativeToParent((parent) => { return parent.Width; }),
                    Constraint.Constant(50)
                );

                mainStackLayout.Children.Add (layoutWithAd);

                MainPage = new ContentPage {
                    Content = mainStackLayout,
                };

Viewing all articles
Browse latest Browse all 75885

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>