Xamarin Ad作为单一实例

时间:2019-06-27 12:35:13

标签: ios xamarin admob

我已经看过这篇文章; Stackoverflow how to have a single instance

但是,这个问题尚未得到解答,我的问题将深入探讨更多细节。


我在IOS中具有以下ViewRenderer:

public class AdBanner_IOS : ViewRenderer<AdBanner, UIView> {
    protected override void OnElementChanged(ElementChangedEventArgs<AdBanner> e) {
        base.OnElementChanged(e);

        if (e.OldElement == null) {
            switch ((Element as AdBanner).Size) {
                case AdBanner.Sizes.Standardbanner:
                    _bannerView = new BannerView(AdSizeCons.Banner, new CGPoint(0, 0));
                    break;
                case AdBanner.Sizes.LargeBanner:
                    _bannerView = new BannerView(AdSizeCons.LargeBanner, new CGPoint(0, 0));
                    break;
                case AdBanner.Sizes.MediumRectangle:
                    _bannerView = new BannerView(AdSizeCons.MediumRectangle, new CGPoint(0, 0));
                    break;
                case AdBanner.Sizes.FullBanner:
                    _bannerView = new BannerView(AdSizeCons.FullBanner, new CGPoint(0, 0));
                    break;
                case AdBanner.Sizes.Leaderboard:
                    _bannerView = new BannerView(AdSizeCons.Leaderboard, new CGPoint(0, 0));
                    break;
                case AdBanner.Sizes.SmartBannerPortrait:
                    _bannerView = new BannerView(AdSizeCons.SmartBannerPortrait, new CGPoint(0, 0));
                    break;
                default:
                    _bannerView = new BannerView(AdSizeCons.Banner, new CGPoint(0, 0));
                    break;
            }
            _bannerView.AdUnitID = Constants.Banner_AdUnitId;

            var request = Request.GetDefaultRequest();
            _bannerView.LoadRequest(request);

            foreach (UIWindow uiWindow in UIApplication.SharedApplication.Windows) {
                if (uiWindow.RootViewController != null) {
                    _bannerView.RootViewController = uiWindow.RootViewController;
                }
            }

            SetNativeControl(_bannerView);
        }
    }

这工作得很好,但这确实意味着它将一遍又一遍地加载每个(新)页面。我想保持不同页面上的广告之间的一致性,并且我认为也不应每隔一个页面加载并发出请求。这就是为什么我尝试使视图静态化并在加载时将其放置在新页面上的原因。这样(通过更改rootviewcontroller);

public class AdBanner_IOS : ViewRenderer<AdBanner, UIView> {
    private static BannerView _bannerView;
    private static bool _loaded;

    protected override void OnElementChanged(ElementChangedEventArgs<AdBanner> e) {
        base.OnElementChanged(e);

        if (_loaded) {
            SetAdView();
            return;
        }

        if (e.OldElement == null) {
            switch ((Element as AdBanner).Size) {
                case AdBanner.Sizes.Standardbanner:
                    _bannerView = new BannerView(AdSizeCons.Banner, new CGPoint(0, 0));
                    break;
                case AdBanner.Sizes.LargeBanner:
                    _bannerView = new BannerView(AdSizeCons.LargeBanner, new CGPoint(0, 0));
                    break;
                case AdBanner.Sizes.MediumRectangle:
                    _bannerView = new BannerView(AdSizeCons.MediumRectangle, new CGPoint(0, 0));
                    break;
                case AdBanner.Sizes.FullBanner:
                    _bannerView = new BannerView(AdSizeCons.FullBanner, new CGPoint(0, 0));
                    break;
                case AdBanner.Sizes.Leaderboard:
                    _bannerView = new BannerView(AdSizeCons.Leaderboard, new CGPoint(0, 0));
                    break;
                case AdBanner.Sizes.SmartBannerPortrait:
                    _bannerView = new BannerView(AdSizeCons.SmartBannerPortrait, new CGPoint(0, 0));
                    break;
                default:
                    _bannerView = new BannerView(AdSizeCons.Banner, new CGPoint(0, 0));
                    break;
            }
            _bannerView.AdUnitID = Constants.Banner_AdUnitId;

            var request = Request.GetDefaultRequest();
            _bannerView.LoadRequest(request);

            _loaded = true;
            SetAdView();
        }
    }
    private void SetAdView() {
        foreach (UIWindow uiWindow in UIApplication.SharedApplication.Windows) {
            if (uiWindow.RootViewController != null) {
                _bannerView.RootViewController = uiWindow.RootViewController;
            }
        }

        SetNativeControl(_bannerView);
    }

这也可以正常工作,直到我导航回到导航堆栈中的现有页面。

然后,ViewRenderer属性不同,并且SetNativeControl函数引发异常,因为Element为空。 (我要做的唯一一件事就是将横幅视图放回到页面上)

有人知道我可以做些什么还是已经实现了一个实例?

0 个答案:

没有答案