Guest User

Untitled

a guest
Nov 16th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  4. xmlns:local="clr-namespace:DDP"
  5. x:Class="DDP.MainPage"
  6. BackgroundColor="#3f183d"
  7. Title="My App Title">
  8.  
  9. <StackLayout BackgroundColor="Transparent" HeightRequest="70" HorizontalOptions="Start" VerticalOptions="Center" WidthRequest="1000">
  10. <local:AdMobView x:Name="adMobView" HorizontalOptions="FillAndExpand" VerticalOptions="EndAndExpand"/>
  11. </StackLayout>
  12. </ContentPage>
  13.  
  14. public MainPage()
  15. {
  16. InitializeComponent();
  17.  
  18. BindingContext = this;
  19. adMobView.AdUnitId = AdMobView.codigoAdmob;
  20. }
  21.  
  22. using Xamarin.Forms;
  23.  
  24. namespace DDP
  25. {
  26. public class AdMobView : View
  27. {
  28.  
  29. public static readonly BindableProperty AdUnitIdProperty = BindableProperty.Create(
  30. nameof(AdUnitId),
  31. typeof(string),
  32. typeof(AdMobView),
  33. string.Empty);
  34.  
  35. public string AdUnitId
  36. {
  37. get => (string)GetValue(AdUnitIdProperty);
  38. set => SetValue(AdUnitIdProperty, value);
  39. }
  40.  
  41. //admob google test code
  42. public static string codigoAdmob = "ca-app-pub-3940256099942544/6300978111";
  43.  
  44. }
  45.  
  46. using System.ComponentModel;
  47. using DDP;
  48. using DDP.Droid;
  49. using Android.Content;
  50. using Android.Gms.Ads;
  51. using Android.Widget;
  52. using Xamarin.Forms;
  53. using Xamarin.Forms.Platform.Android;
  54.  
  55. [assembly: ExportRenderer(typeof(AdMobView), typeof(AdMobViewRenderer))]
  56. namespace DDP.Droid
  57. {
  58. public class AdMobViewRenderer : ViewRenderer<AdMobView, AdView>
  59. {
  60. public AdMobViewRenderer(Context context) : base(context) { }
  61.  
  62. protected override void OnElementChanged(ElementChangedEventArgs<AdMobView> e)
  63. {
  64. base.OnElementChanged(e);
  65.  
  66. if (e.NewElement != null && Control == null)
  67. {
  68. SetNativeControl(CreateAdView());
  69. }
  70. }
  71.  
  72. protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
  73. {
  74. base.OnElementPropertyChanged(sender, e);
  75.  
  76. if (e.PropertyName == nameof(AdView.AdUnitId))
  77. Control.AdUnitId = Element.AdUnitId;
  78. }
  79.  
  80. private AdView CreateAdView()
  81. {
  82. var adView = new AdView(Context)
  83. {
  84. AdSize = AdSize.SmartBanner,
  85. AdUnitId = Element.AdUnitId
  86. };
  87.  
  88. adView.LayoutParameters = new LinearLayout.LayoutParams(LayoutParams.MatchParent, LayoutParams.MatchParent);
  89.  
  90. adView.LoadAd(new AdRequest.Builder().Build());
  91.  
  92. return adView;
  93. }
  94.  
  95. }
  96.  
  97. //somecode
  98.  
  99. base.OnCreate(bundle);
  100. MobileAds.Initialize(ApplicationContext, "ca-app-pub-3940256099942544/6300978111");
  101. global::Xamarin.Forms.Forms.Init(this, bundle);
  102. LoadApplication(new App());
  103.  
  104. //somecode
Add Comment
Please, Sign In to add comment