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

AdMob implementation using shared project

$
0
0

I am trying to add an AdMob banner to my cross-platform project. I have tried many googles and most of the examples are for Xamarin.Android only. I am opening a page in a Shared Project folder from my MainActivity.cs. This root page will also be opened from AppDelegate.cs. Below I have code from Android version.

From MainActivity.cs

 protected override void OnCreate(Bundle bundle)
 {
        base.OnCreate(bundle);
        var page = new NavigationPage(new RootPage());
        SetPage(page);
}

AdMob helper class

   using Android.Gms.Ads;
   using Android.Content;
   using Xamarin.Forms;

   [assembly: Dependency(typeof(GAdMobBanner))]     

    public class GAdMobBanner
    {
        const string UnitID = "My-Unit-ID";

        public AdView BuildStandardBanner()
        {
            var ad = new AdView(Forms.Context);
            ad.AdSize = Android.Gms.Ads.AdSize.SmartBanner;
            ad.AdUnitId = UnitID;
            return ad;
        }
   }

RootPage

    public class RootPage : ContentPage
    {
        public RootPage()
        {
            //code above creates content in variable called 'layout'

            var gadmob = DependencyService.Get<GAdMobBanner>();
            var ad = gadmob.BuildStandardBanner();

            var finallayout = new StackLayout();
            finallayout.Children.Add(layout);
            finallayout.Children.Add(ad);   //<<<ERROR : cannot convert `Android.Gms.Ads.AdView' expression to type `Xamarin.Forms.View'          
            Content = finallayout;
        }
    }

Does anybody have any ideas on how to do it? I can try something inside MainActivity.cs itself before opening the rootPage but I can't figure out how to fit the contents of the rootPage inside a LinearLayout there. Please can anybody help?

Thanks,
John


Viewing all articles
Browse latest Browse all 79144

Trending Articles



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