Admob广告仅在MainPage(Xamarin Android)上显示

时间:2018-08-18 05:15:30

标签: c# android xamarin admob ads

这是我在这里的第一篇文章..这是我第一次使用Android开发和Xamarin(也是C#的新手)。

所以..我有一个正在制作的应用程序。它由1个MainPage和其他一些页面(导航页面)组成,我可以通过单击标签来调用这些页面。我的广告显示在MainPage的底部,但当我呼叫另一个时,它不会显示,我想在所有页面的底部显示该横幅。

这是我的一些代码(我排除了一些无关紧要的东西-我只在这里留下了第一个标签/按钮)...

我引用的是Xamarin.Firebase.Ads

谢谢!


  

MainPage.xaml

<?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" Title="Diário das Porquinhas">
    <StackLayout BackgroundColor="#3f183d" HeightRequest="100" WidthRequest="100">
        <StackLayout BackgroundColor="Transparent" HeightRequest="1000" VerticalOptions="Start" WidthRequest="100">
            <ScrollView Orientation="Vertical">
                <StackLayout x:Name="stkAlojamento" Orientation="Horizontal" Spacing="0" Padding="0" WidthRequest="600" BackgroundColor="#502d4e">
                    <Label Text=" " WidthRequest="20"/>
                    <Image BackgroundColor="#502d4e" Source="icon_calcalojamento.png" HeightRequest="40" HorizontalOptions="Start" WidthRequest="45" VerticalOptions="Center">
                        <Image.GestureRecognizers>
                            <TapGestureRecognizer Tapped="CalcularAlojamento"/>
                        </Image.GestureRecognizers>
                    </Image>
                    <Label Text=" " WidthRequest="20"/>
                    <Label BackgroundColor="#502d4e" FontAttributes="Bold" FontSize="Small" HeightRequest="80" HorizontalTextAlignment="Start" Margin="0" Text=" Cálculo de Alojamento" TextColor="White" VerticalTextAlignment="Center" VerticalOptions="CenterAndExpand">
                        <Label.GestureRecognizers>
                            <TapGestureRecognizer Tapped="CalcularAlojamento"/>
                        </Label.GestureRecognizers>
                    </Label>
                </StackLayout>
            </ScrollView>
        </StackLayout>
        <StackLayout BackgroundColor="Transparent" HeightRequest="70" VerticalOptions="FillAndExpand" WidthRequest="1000">
            <local:AdMobView x:Name="adMobView" HorizontalOptions="FillAndExpand" VerticalOptions="EndAndExpand"/>
        </StackLayout>
    </StackLayout>
</ContentPage>
  

MainPage.xaml.cs

using Xamarin.Forms;

namespace DDP
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            BindingContext = this;
            if (Device.RuntimePlatform == Device.Android)
            //admob test IDs
            adMobView.AdUnitId = "ca-app-pub-3940256099942544/6300978111";
        }

        public void CalcularAlojamento()
        {
            CalculoAlojamento tela = new CalculoAlojamento();
            Navigation.PushAsync(tela);
        }
    }
}
  

AdMobView.cs

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);
        }
    }
}
  

AdMobViewRenderer.cs

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;
        }
    }
}
  

MainActivity.cs

using Android.App;
using Android.Content.PM;
using Android.Gms.Ads;
using Android.OS;

namespace DDP.Droid
{
    [Activity(Label = "Diário das Porquinhas", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;
            base.OnCreate(bundle);
            MobileAds.Initialize(ApplicationContext, "ca-app-pub-3940256099942544/6300978111");
            global::Xamarin.Forms.Forms.Init(this, bundle);
            LoadApplication(new App());
        }
    }
}
  

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="br.com.ddp" android:installLocation="auto">
    <uses-sdk android:minSdkVersion="19" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application android:label="Diário das Porquinhas" android:icon="@drawable/icon"></application>
    <meta-data android:name="android.max_aspect" android:value="2.1" />
    <activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" android:theme="@android:style/Theme.Translucent" />
</manifest>

0 个答案:

没有答案