Created
April 19, 2018 14:34
-
-
Save juucustodio/f34db15c19d37081143968c237e262ea to your computer and use it in GitHub Desktop.
Example how to monetize yours Xamarin.Forms applications with AdMob by Google - http://julianocustodio.com/admob-xamarin-forms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.ComponentModel; | |
using DemoAdMob; | |
using DemoAdMob.Droid; | |
using Android.Content; | |
using Android.Gms.Ads; | |
using Android.Widget; | |
using Xamarin.Forms; | |
using Xamarin.Forms.Platform.Android; | |
[assembly: ExportRenderer(typeof(AdMobView), typeof(AdMobViewRenderer))] | |
namespace DemoAdMob.Droid | |
{ | |
public class AdMobViewRenderer : ViewRenderer<AdMobView, AdView> | |
{ | |
public AdMobViewRenderer(Context context) : base(context) { } | |
protected override void OnElementChanged(ElementChangedEventArgs<AdMobView> e) | |
{ | |
base.OnElementChanged(e); | |
if (e.NewElement != null && Control == null) | |
{ | |
SetNativeControl(CreateAdView()); | |
} | |
} | |
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) | |
{ | |
base.OnElementPropertyChanged(sender, e); | |
if (e.PropertyName == nameof(AdView.AdUnitId)) | |
Control.AdUnitId = Element.AdUnitId; | |
} | |
private AdView CreateAdView() | |
{ | |
var adView = new AdView(Context) | |
{ | |
AdSize = AdSize.SmartBanner, | |
AdUnitId = Element.AdUnitId | |
}; | |
adView.LayoutParameters = new LinearLayout.LayoutParams(LayoutParams.MatchParent, LayoutParams.MatchParent); | |
adView.LoadAd(new AdRequest.Builder().Build()); | |
return adView; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment