我过去7星期一直在研究这个问题,但是我不知道为什么广告没有显示。我见过很多人在Google上发布了同样的问题,但是似乎没人知道如何解决它……好像xamarim需要简单的东西并使它变得复杂。无论如何,尽管我应该在stackoverflow上寻求帮助
我使用的代码是默认的xamarin.forms代码,可以在以下网站上找到:click here
issue:当我运行此代码时,广告未显示。
在测试设备上,它工作正常。此代码在这里没有问题
//Test Device
var requestbuilder = new AdRequest.Builder().AddTestDevice(testDeviceID);
但是在生产设备上,广告没有显示
//Production Device
var requestbuilder = new AdRequest.Builder();
在日志中,您会收到以下消息:
I/Ads ( 4131): Ad failed to load : 3
D/Mono ( 4131): DllImport searching in: 'libmono-native.so' ('./libmono-native.so').
D/Mono ( 4131): Searching for 'SystemNative_CopyFile'.
D/Mono ( 4131): Probing 'SystemNative_CopyFile'.
D/Mono ( 4131): Found as 'SystemNative_CopyFile'.
完整代码:
AndroidManifiedt.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="2" android:versionName="2.0" package="com.wolfTech.discount_calculator" android:installLocation="auto">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="28" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<application android:label="Discount_Calculator.Android">
<meta-data android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-76543245678567~1212345354" />
<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>
MainPage.xaml
<ContentPage ...>
<StackLayout>
<Controls:AdControlview Size="Standardbanner" VerticalOptions="EndAndExpand" />
</StackLayout>
</ContentPage>
AdControlview.cs
namespace Discount_Calculator.Controls
{
public class AdControlview : Xamarin.Forms.View
{
public enum Sizes { Standardbanner, LargeBanner, MediumRectangle, FullBanner, Leaderboard, SmartBannerPortrait }
public Sizes Size { get; set; }
public AdControlview()
{
this.BackgroundColor = Color.Accent;
}
}
}
AdViewRenderer.cs
[assembly: ExportRenderer(typeof(AdControlview), typeof(AdViewRenderer))]
namespace Discount_Calculator.Droid.Helper
{
public class AdViewRenderer : ViewRenderer
{
Context context;
string testDeviceID = "0F92232223232323230BBC9";
string myAdUnitId = "ca-app-pub-442332323323320/92223323232321";
public AdViewRenderer(Context _context) : base(_context)
{
context = _context;
}
protected override void OnElementChanged(ElementChangedEventArgs<View> e)
{
base.OnElementChanged(e);
if (e.OldElement == null)
{
var adView = new AdView(Context);
var MyAdHeight = 50;
switch ((Element as AdControlview).Size)
{
case AdControlview.Sizes.Standardbanner:
adView.AdSize = AdSize.Banner;
MyAdHeight = 50;
break;
case AdControlview.Sizes.LargeBanner:
adView.AdSize = AdSize.LargeBanner;
MyAdHeight = 100;
break;
case AdControlview.Sizes.MediumRectangle:
adView.AdSize = AdSize.MediumRectangle;
MyAdHeight = 250;
break;
case AdControlview.Sizes.FullBanner:
adView.AdSize = AdSize.FullBanner;
MyAdHeight = 60;
break;
case AdControlview.Sizes.Leaderboard:
adView.AdSize = AdSize.Leaderboard;
MyAdHeight = 90;
break;
case AdControlview.Sizes.SmartBannerPortrait:
adView.AdSize = AdSize.SmartBanner;
break;
default:
adView.AdSize = AdSize.Banner;
break;
}
// TODO: change this id to your admob id
adView.AdUnitId = myAdUnitId;
//Test Device
//var requestbuilder = new AdRequest.Builder().AddTestDevice(testDeviceID);
//Production Device
var requestbuilder = new AdRequest.Builder();
e.NewElement.HeightRequest = MyAdHeight;
adView.LoadAd(requestbuilder.Build());
SetNativeControl(adView);
}//end of if
}//end of OnElementChanged
}//end of class
} //end of name
这是我的项目结构:
由于某种原因,现在它的工作了。我什么也没做