【问题标题】:Get the height of the ContentPage in Xamarin Forms using CustomRenderer使用 CustomRenderer 获取 Xamarin 表单中 ContentPage 的高度
【发布时间】:2021-01-16 23:27:03
【问题描述】:

我想要 NavigationBar 的高度,尝试使用 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.

【问题讨论】:

    标签: xamarin xamarin.forms xamarin.android custom-renderer


    【解决方案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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多