2017-09-23 1 views
0

J'ai ajouté des annonces admob à mon application. Maintenant, je souhaite envoyer l'identifiant du bloc d'annonces à partir de la page xaml.Transférer le bloc d'annonces dans xaml pour rendre l'annonce adm.

Je l'ai implémenté comme ça.

Création d'un contrôle personnalisé

using Xamarin.Forms; 

namespace MeetupManager.Controls 
{ 
    public class AdControlView : View 
    { 
    } 
} 

Ajout Voir Renderer

using Android.Widget; 
using Android.Gms.Ads; 
using Xamarin.Forms; 
using Xamarin.Forms.Platform.Android; 

[assembly: ExportRenderer(typeof(MeetupManager.Controls.AdControlView), typeof(MeetupManager.Droid.PlatformSpecific.AdViewRenderer))] 
namespace MeetupManager.Droid.PlatformSpecific 
{ 
    public class AdViewRenderer : ViewRenderer<Controls.AdControlView, AdView> 
    { 
     string adUnitId = string.Empty; 
     //Note you may want to adjust this, see further down. 
     AdSize adSize = AdSize.SmartBanner; 
     AdView adView; 
     AdView CreateNativeAdControl() 
     { 
      if (adView != null) 
       return adView; 

      // This is a string in the Resources/values/strings.xml that I added or you can modify it here. This comes from admob and contains a/in it 
      adUnitId = Forms.Context.Resources.GetString(Resource.String.banner_ad_unit_id); 
      adView = new AdView(Forms.Context); 
      adView.AdSize = adSize; 
      adView.AdUnitId = adUnitId; 

      var adParams = new LinearLayout.LayoutParams(LayoutParams.WrapContent, LayoutParams.WrapContent); 

      adView.LayoutParameters = adParams; 

      adView.LoadAd(new AdRequest 
          .Builder() 
          .Build()); 
      return adView; 
     } 

     protected override void OnElementChanged(ElementChangedEventArgs<Controls.AdControlView> e) 
     { 
      base.OnElementChanged(e); 
      if(Control == null) 
      { 
       CreateNativeAdControl(); 
       SetNativeControl(adView); 
      } 
     } 
    } 
} 

Mise à jour MainActivity

MobileAds.Initialize(ApplicationContext, "YOUR APP ID HERE FROM AdMob, has a ~ in it"); 

Mise à jour XAML

d'abord ajouter de l'espace de noms xmlns personnalisé:

xmlns:controls="clr-namespace:MeetupManager.Controls;assembly=MeetupManager" 

en ajoutant ensuite le contrôle personnalisé

<Grid RowSpacing="0"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="*"/> 
      <RowDefinition Height="Auto"/> 
     </Grid.RowDefinitions> 
     <ListView Grid.Row="0"/> 
     <controls:AdView Grid.Row="1"/> 
    </Grid> 

Ceci est du tutoriel, je suivais de Xamarin.Forms: Google AdMob Ads in Android

Il fonctionne génial. Maintenant, ce que je veux, c'est envoyer un identifiant d'unité d'annonce depuis xaml. quelque chose comme ça.

<controls:AdView Grid.Row="1" AdUnitId="testid"/> 

Comment devrais-je faire cela?

Répondre

0

Vous devez ajouter une propriété dans votre contrôle personnalisé, comme ceci:

using Xamarin.Forms; 

namespace MeetupManager.Controls 
{ 
    public class AdControlView : View 
    { 
     public static readonly BindableProperty AdUnitIdProperty = 
     BindableProperty.Create(propertyName: nameof(AdUnitId), 
       returnType: typeof(string), 
       declaringType: typeof(AdControlView), 
       defaultValue: ""); 


     public string AdUnitId 
     { 
      get { return GetValue(AdUnitIdProperty).ToString(); } 
      set { SetValue(AdUnitIdProperty, value); } 
     } 
    } 
} 

Et à votre avis renderer

using Android.Widget; 
using Android.Gms.Ads; 
using Xamarin.Forms; 
using Xamarin.Forms.Platform.Android; 

[assembly: ExportRenderer(typeof(MeetupManager.Controls.AdControlView), typeof(MeetupManager.Droid.PlatformSpecific.AdViewRenderer))] 
namespace MeetupManager.Droid.PlatformSpecific 
{ 
    public class AdViewRenderer : ViewRenderer<Controls.AdControlView, AdView> 
    { 
     //Note you may want to adjust this, see further down. 
     AdSize adSize = AdSize.SmartBanner; 
     AdView adView; 
     //*********HERE'S THE CHANGE********** 
     AdView CreateNativeAdControl(string adUnitId) 
     { 
      if (adView != null) 
      return adView;    
      adView = new AdView(Forms.Context); 
      adView.AdSize = adSize; 
      adView.AdUnitId = adUnitId; 

      var adParams = new LinearLayout.LayoutParams(LayoutParams.WrapContent, LayoutParams.WrapContent); 

      adView.LayoutParameters = adParams; 

      adView.LoadAd(new AdRequest 
         .Builder() 
         .Build()); 
      return adView; 
     } 

     protected override void OnElementChanged(ElementChangedEventArgs<Controls.AdControlView> e) 
     { 
      base.OnElementChanged(e); 
      if(Control == null) 
      { 
       if (!String.IsNullOrEmpty(((AdControlView)Element).AdUnitId)) 
       { 
        string adUnitId = ((AdControlView)Element).AdUnitId; 
        CreateNativeAdControl(adUnitId); 
        SetNativeControl(adView); 
       }      
      } 
     } 
    } 
} 
+0

Si cela est le moyen le plus sûr de quelle approche dois-je prendre? L'identifiant du bloc d'annonces changera pour chaque page. –

+0

Vous avez raison, je vais éditer ma réponse est la bonne façon. Si cela vous a aidé, veuillez le marquer comme une réponse –

+0

alors je l'appelle comme ceci ? –