Forum Xamarin.Forms

Get dimensions for view at run-time and get Native view given view from PCL?

LeoJHarrisLeoJHarris NZMember ✭✭✭
edited June 2017 in Xamarin.Forms

Hi, I am trying to get the height/dimensions of a component at run-time for android and iOS.

My attempt is to get the native component through dependency injection and use the ViewTreeObserver method to get access to the view hierarchy of the PCL component in order to call this method i need to get the native component

If some some has got the dimensions of view views at run time I would really like to know for iOS and Android please...?

My current code is:

     public int GetNativeViewHeight(View view)
            {
                Android.Views.View nativeView = view.GetNativeContent();

                MyLayoutListener layoutListener = new MyLayoutListener();

                ViewTreeObserver vto = nativeView.ViewTreeObserver;
                vto.AddOnGlobalLayoutListener(layoutListener);

                int layoutheight = 1;

                vto.GlobalLayout += (sender, args) => {  layoutheight = nativeView.Height; };

                return layoutheight;
            }

public class MyLayoutListener : Java.Lang.Object, ViewTreeObserver.IOnGlobalLayoutListener
    {
        public void OnGlobalLayout()
        {

        }
    }

Thanks

Sign In or Register to comment.