Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?xml version="1.0" encoding="utf-8" ?>
- <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
- xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
- xmlns:local="clr-namespace:DDP"
- x:Class="DDP.MainPage"
- BackgroundColor="#3f183d"
- Title="My App Title">
- <StackLayout BackgroundColor="Transparent" HeightRequest="70" HorizontalOptions="Start" VerticalOptions="Center" WidthRequest="1000">
- <local:AdMobView x:Name="adMobView" HorizontalOptions="FillAndExpand" VerticalOptions="EndAndExpand"/>
- </StackLayout>
- </ContentPage>
- public MainPage()
- {
- InitializeComponent();
- BindingContext = this;
- adMobView.AdUnitId = AdMobView.codigoAdmob;
- }
- using Xamarin.Forms;
- namespace DDP
- {
- public class AdMobView : View
- {
- public static readonly BindableProperty AdUnitIdProperty = BindableProperty.Create(
- nameof(AdUnitId),
- typeof(string),
- typeof(AdMobView),
- string.Empty);
- public string AdUnitId
- {
- get => (string)GetValue(AdUnitIdProperty);
- set => SetValue(AdUnitIdProperty, value);
- }
- //admob google test code
- public static string codigoAdmob = "ca-app-pub-3940256099942544/6300978111";
- }
- using System.ComponentModel;
- using DDP;
- using DDP.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 DDP.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;
- }
- }
- //somecode
- base.OnCreate(bundle);
- MobileAds.Initialize(ApplicationContext, "ca-app-pub-3940256099942544/6300978111");
- global::Xamarin.Forms.Forms.Init(this, bundle);
- LoadApplication(new App());
- //somecode
Add Comment
Please, Sign In to add comment