首页 > 解决方案 > Admob 广告仅显示在 MainPage (Xamarin Android)

问题描述

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

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

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

我引用 Xamarin.Firebase.Ads

提前致谢!


主页.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>

标签: c#androidxamarinadmobads

解决方案


推荐阅读