Получить высоту страницы содержимого в формах Xamarin с помощью CustomRenderer

#xamarin #xamarin.forms #xamarin.android #custom-renderer

#xamarin #xamarin.forms #xamarin.android #пользовательский рендерер

Вопрос:

Я хочу высоту панели навигации, пробовал использовать CustomRenderer, но всегда получает одно и то же значение, будь то планшет, телефон Android.

Я использую следующий код

 namespace OfflineFieldService.Droid
{
    public class CustomAndroidPageRenderer : PageRenderer
    {
        public CustomAndroidPageRenderer(Context context) : base(context)
        {

        }

        protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
        {
            base.OnElementChanged(e);
            var height = 0;
            var resources = Context.Resources;
            int resourceId = resources.GetIdentifier("navigation_bar_height", "dimen", "android");
            if (resourceId > 0)
            {
                height = resources.GetDimensionPixelSize(resourceId);
            }
            var scale = Resources.DisplayMetrics.Density;
            var heightinUnits = (height - .5) / scale;
            App.screenHeight = heightinUnits;

        }
    }```

[Attached image in the link, since dont have permission to embed][1]


  [1]: https://i.stack.imgur.com/UosCb.png

In the image whatever marked in yellow, need to get the height of it. I am using Shell template.
  

Ответ №1:

Высота панели навигации iOS

 public class NavRendererForiOS : NavigationRenderer
{
    protected override void OnElementChanged(VisualElementChangedEventArgs e)
    {
        base.OnElementChanged(e);

        var height = NavigationBar.Bounds.Height;
    }
}
  

Высота панели навигации android

 public class NavRendererForAndroid : NavigationPageRenderer
{
    public CustomNaviForAndroid(Context context) : base(context)
    {

    }

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

        var height = 0;

        Resources resources = Context.Resources;
        int resourceId = resources.GetIdentifier("navigation_bar_height", "dimen", "android");
        if (resourceId > 0)
        {
            height = resources.GetDimensionPixelSize(resourceId);
        }
    }
}
  

Размер экрана Android

 App.ScreenHeight = (int) (Resources.DisplayMetrics.HeightPixels / Resources.DisplayMetrics.Density);
App.ScreenWidth = (int) (Resources.DisplayMetrics.WidthPixels / Resources.DisplayMetrics.Density);
  

Размер экрана iOS

 App.ScreenHeight = (int)UIScreen.MainScreen.Bounds.Height;
App.ScreenWidth = (int)UIScreen.MainScreen.Bounds.Width;