Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 89864

How to resize AdControl in UWP dynamically ?

$
0
0

I have implemented banner ads in UWP. It works fine but i cant figure out how to refresh or resize the ad if user changes the size of UWP app in desktop.
Code looks like as below. OnElementChanged is called only once when Adview on forms side is loaded. If I resize the window, it never gets called, only MeasureOverride method and OnPropertyChanged are called. but from here, i am not sure how to call GetAd method again or how to request a new size for my ad. Because I want to display a vertical ad instead of horizontal banner ad for if Width > height. For that i need to recalculate AdControl.

public class AdRenderer : ViewRenderer<Helpers.Controls.AdView, AdControl>
    {
        private const bool _hasSideMenu = true;
        private const int _sideBarWidth = 320;

        public AdRenderer()
        {

        }

        protected override Size MeasureOverride(Size availableSize)
        {
            return base.MeasureOverride(availableSize);
        }


        protected override void OnElementChanged(ElementChangedEventArgs<Helpers.Controls.AdView> e)
        {
            base.OnElementChanged(e);

            GetAd(e);

        }

        private void GetAd(ElementChangedEventArgs<Helpers.Controls.AdView> e)
        {

                try
                {
                    var ad = new Microsoft.Advertising.WinRT.UI.AdControl
                    {                       
                        IsAutoRefreshEnabled = true
                    };

                    if (e.OldElement != null)
                    {
                        return;
                    }

                    if (Control == null)
                    {


                        var availableWidth = Window.Current.Bounds.Width;
                        var availableHight = Window.Current.Bounds.Height;



                        if (_hasSideMenu)
                        {
                            var isDesktop = Windows.System.Profile.AnalyticsInfo.VersionInfo.DeviceFamily == "Windows.Desktop";
                            if (isDesktop)
                            {
                                availableWidth = Window.Current.Bounds.Width - _sideBarWidth;
                            }
                        }

                        if (availableWidth >= 728)
                        {
                            ad.Width = 728;
                            ad.Height = 90;
                        }
                        else if (availableWidth >= 640)
                        {
                            ad.Width = 640;
                            ad.Height = 100;
                        }
                        else if (availableWidth >= 320)
                        {
                            ad.Width = 320;
                            ad.Height = 50;
                        }
                        else if (availableWidth >= 300)
                        {
                            ad.Width = 300;
                            ad.Height = 50;
                        }

                        //if (availableHight < availableWidth)
                        //{
                        //    ad.Width = 160;
                        //    ad.Height = 600;
                        //}


                        e.NewElement.HeightRequest = ad.Height;

                        SetNativeControl(ad);
                    }
                }
                catch (System.Exception ex)
                {

                }

        }

        private void OnErrorOccurred(object sender, AdErrorEventArgs e)
        {

        }
    }

Viewing all articles
Browse latest Browse all 89864

Trending Articles