简体   繁体   English

Xamarin Forms 自适应横幅广告不会在横向加载

[英]Xamarin Forms Adaptive Banner Ad Won't Load in Landscape

I recently upgraded from a Galaxy S6 to a Galaxy S21 Ultra and now the AdMob adaptive banner advert will not load in landscape.我最近从 Galaxy S6 升级到 Galaxy S21 Ultra,现在 AdMob 自适应横幅广告无法横向加载。 On the S6 it is fine, but on the S21 I get the error:在 S6 上没问题,但在 S21 上我收到错误消息:

[Ads] Ad failed to load : 1 [广告] 广告加载失败:1

The code I use to create the advert is here:我用来创建广告的代码在这里:

private void NewAdaptiveBannerAd()
{
    try
    {
        adAdaptiveBannerView = new AdAdaptiveBannerView();
        adAdaptiveBannerView.Padding = new Thickness(0);
        adAdaptiveBannerView.BackgroundColor = (Color)Application.Current.Resources["baseBackground_Light"];

        AdGrid.Children.Add(adAdaptiveBannerView, 0, 1);

        adAdaptiveBannerView.HeightRequest = getCurrentOrientationAnchoredAdaptiveBannerAdSize(Android.App.Application.Context, (int)(DeviceDisplay.MainDisplayInfo.Width / DeviceDisplay.MainDisplayInfo.Density));

        static int getCurrentOrientationAnchoredAdaptiveBannerAdSize(Context context, int adWidth)
        {
            int var3 = 0;

            if (context == null)
            {
                return 0;
            }
            else
            {
                int var7 = 2;
                switch (DeviceDisplay.MainDisplayInfo.Orientation)
                {
                    case DisplayOrientation.Unknown: var7 = 0; break;
                    case DisplayOrientation.Portrait: var7 = 1; break;
                    case DisplayOrientation.Landscape: var7 = 2; break;
                    default: return 0;
                }
                if (var3 == 0)
                {
                    var3 = var7;
                }

                int var8 = var3 == var7 ? (int)Math.Round((float)DeviceDisplay.MainDisplayInfo.Height / DeviceDisplay.MainDisplayInfo.Density) : (int)Math.Round((float)DeviceDisplay.MainDisplayInfo.Width / DeviceDisplay.MainDisplayInfo.Density);
                int var9 = (int)Math.Min(90, Math.Round((float)var8 * 0.15F));
                int var10;
                if (adWidth > 655)
                {
                    var10 = (int)Math.Round((float)adWidth / 728.0F * 90.0F);
                }
                else if (adWidth > 632)
                {
                    var10 = 81;
                }
                else if (adWidth > 526)
                {
                    var10 = (int)Math.Round((float)adWidth / 468.0F * 60.0F);
                }
                else if (adWidth > 432)
                {
                    var10 = 68;
                }
                else
                {
                    var10 = (int)Math.Round((float)adWidth / 320.0F * 50.0F);
                }

                var10 = Math.Max(Math.Min(var10, var9), 50);
                return var10;
            }
        }
    }
    catch (Exception)
    {
        Console.WriteLine("Creation of banner ad in SavedCharts failed.");
    }
}

adAdaptiveBannerView is declared at the top of the pages class. adAdaptiveBannerView 在 pages 类的顶部声明。 The getCurrentOrientationAnchoredAdaptiveBannerAdSize function is from another post on here that I can't find now. getCurrentOrientationAnchoredAdaptiveBannerAdSize 函数来自这里的另一篇文章,我现在找不到了。

I can't find anything that would explain this, or why it would behave differently between these two 'phones (is it the difference in Android versions, the different aspect ratio or what?).我找不到任何可以解释这一点的东西,或者为什么这两款“手机”之间的行为会有所不同(是 Android 版本的差异、不同的纵横比还是什么?)。 Testing on the emulator is difficult because it doesn't report orientation correctly.在模拟器上进行测试很困难,因为它没有正确报告方向。

Does anyone have any idea what's going on?有谁知道发生了什么?

My app will, by it's nature, mostly be used in landscape mode, so this problem means there's a chance many people won't see the adverts.我的应用程序本质上主要用于横向模式,所以这个问题意味着很多人可能看不到广告。

PS I'm using Xamarin.GooglePlayServices.Ads version 119.8.0. PS 我使用的是 Xamarin.GooglePlayServices.Ads 版本 119.8.0。 I haven't been able to implement version 120 because there's no documentation or anything that would help me do it.我一直无法实现 120 版,因为没有文档或任何可以帮助我做到的东西。

Edit 1: I've tried conditionally adding an adaptive banner ad when in portrait, and a standard banner ad using MarcTron.Admob.编辑 1:我尝试在纵向时有条件地添加自适应横幅广告,以及使用 MarcTron.Admob 的标准横幅广告。 It doesn't work.它不起作用。

I wondered if it's because the S21 Ultra's display is 1440 x 3200 pixels with an aspect ration of 20:9, and Google Ad Mob can't cope with it.我在想是不是因为S21 Ultra的显示屏是1440 x 3200像素,纵横比为20:9,Google Ad Mob应付不来。 It's rather stupid if that's the case ('adaptive'?!).如果是这种情况,那就太愚蠢了(“自适应”?!)。 So I tried giving it a screen width of 2560 on the basis that it worked on the S6 (1440x3200), and 1600 (ie half), but neither worked.所以我试着给它一个 2560 的屏幕宽度,因为它可以在 S6(1440x3200)和 1600(即一半)上工作,但都没有工作。

Edit 2: I realised I'd made a mistake when I tried limiting the screen width to 2560px, which was that I only did that for the height request of the view, whereas I also needed to do it for the AdSize property of the AdMob class itself.编辑 2:当我尝试将屏幕宽度限制为 2560px 时,我意识到我犯了一个错误,即我只对视图的高度请求执行此操作,而我还需要为 AdMob 的 AdSize 属性执行此操作类本身。 Anyway, that is the problem - adaptive banner ads can't cope with a display 3200px wide.无论如何,这就是问题所在——自适应横幅广告无法处理 3200 像素宽的显示器。

Google AdMab adaptive banner adverts cannot cope with a screen width of 3200px. Google AdMab 自适应横幅广告无法处理 3200 像素的屏幕宽度。 I don't know what the actual limit is but since it works on my other device that has a screen width of 2560px I've limited it to that, like so:我不知道实际的限制是什么,但由于它适用于我的其他设备,屏幕宽度为 2560 像素,因此我将其限制为:

adView.AdSize = AdSize.GetCurrentOrientationAnchoredAdaptiveBannerAdSize(Android.App.Application.Context, (int)(Math.Min(2560,DeviceDisplay.MainDisplayInfo.Width) / DeviceDisplay.MainDisplayInfo.Density));

and applied the same Math.Min() limiter to the code in my question.并将相同的 Math.Min() 限制器应用于我问题中的代码。

The has the slightly unfortunate side-effect of a gap either side of the banner ad (3200-2560)/2=320 pixels wide.横幅广告(3200-2560)/2 = 320 像素宽的任何一侧的间隙都有一点不幸的副作用。 This doesn't violate Google's policies though ( https://developers.google.com/admob/android/banner/adaptive ).这并不违反 Google 的政策( https://developers.google.com/admob/android/banner/adaptive )。

I suggest this is a bug in Google's API (or at least the NuGet package), as an 'adaptive' ad that can't cope with all screen resolutions in real devices doesn't live up to its name.我认为这是 Google 的 API(或至少是 NuGet 包)中的一个错误,因为无法处理真实设备中所有屏幕分辨率的“自适应”广告并不符合其名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM