【问题标题】:Ads not showing up in xamarin.Form on Droid projectDroid 项目的 xamarin.Form 中未显示广告
【发布时间】:2018-11-28 01:40:16
【问题描述】:

我在 Xamarin.Forms 上添加了 AdMob 广告,但在 Droid 项目上没有显示广告 而且我的项目没有任何错误 在我的 AdMob 帐户上请求 144,但广告没有显示请有人帮助我 在控制文件夹中的 Portable AdControlView 中

using Xamarin.Forms;

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

在 adView 渲染器中

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>
    {
        AdSize adSize = AdSize.SmartBanner;
        AdView adView;
        AdView CreateNativeAdControl()
        {
            if (adView != null)
                return adView;
            adUnitId = "My 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);
            }
        }
    }
}

在 MainActivity 中

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);
            global::Xamarin.Forms.Forms.Init(this, bundle);
            MobileAds.Initialize(ApplicationContext, "YOUR APP ID HERE FROM AdMob, has a ~ in it");
            LoadApplication(new App());
            /////////////////////////
        }
    }

在 AndroidManifest.xml 中

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application android:label="StoryForKids.Android" android:icon="@drawable/IdeaGroup">
    <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/>

        <activity android:name="com.google.android.gms.ads.AdActivity" 
              android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" 
              android:theme="@android:style/Theme.Translucent" />
    </application>

我想要显示我的广告但没有显示的页面

    xmlns:ads="clr-namespace:MeetupManager.Controls;assembly=MeetupManager"
                 x:Class="MeetupManager.page2">
    <ContentPage.ToolbarItems>
        <ToolbarItem x:Name="english" Order="Secondary" Text="English" Icon="america.png" Clicked="english_Clicked"/>
        <ToolbarItem x:Name="Kurdish" Order="Secondary" Text="Kurdish" Icon="kurdistan.png" Clicked="Kurdish_Clicked" />
    </ContentPage.ToolbarItems>
    <ContentPage.Content>
<StackLayout >
            <Image x:Name="img"  VerticalOptions="CenterAndExpand" HeightRequest="100" HorizontalOptions="Center"/>
            <Label x:Name="lbltitle" HorizontalOptions="Center" FontAttributes="Bold"/>
            <Label x:Name="lblstory" FontFamily="Arial" 
                VerticalOptions="CenterAndExpand" 
                HorizontalOptions="CenterAndExpand" />
            <ads:AdControlView BackgroundColor="White" ></ads:AdControlView>
</StackLayout>
    </ContentPage.Content>

【问题讨论】:

    标签: c# forms xamarin.forms


    【解决方案1】:

    你得到答案了吗?如果不是,您的问题可能是尺寸问题。我的是。在您的视图和 AdViewRenderer 中将您的广告设置为 320x50,从

    AdSize adSize = AdSize.SmartBanner;
    

    AdSize adSize = AdSize.Banner;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-05
      相关资源
      最近更新 更多