我怎样才能在xamarin.forms中获得状态栏高度?

时间:2018-01-16 07:31:01

标签: xamarin.forms size statusbar

我正在尝试在我的应用程序中获取状态/操作栏高度。 我得到了一些我们如何在原生android中获得它。 我们可以在xamarin.forms中获得状态/操作栏高度吗?如果是,那么如何? 如果有人有任何想法,请帮助。 enter image description here

4 个答案:

答案 0 :(得分:5)

您可以通过创建自己的依赖服务来完成此操作。 (https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/dependency-service/introduction/

在共享代码中,创建一个接口,例如IStatusBar:

public interface IStatusBar
    {
        int GetHeight();
    }

为Android平台添加实施:

[assembly: Dependency(typeof(StatusBar))]
namespace StatusBarApp.Droid
{
    class StatusBar : IStatusBar
    {
        public static Activity Activity { get; set; }

        public int GetHeight()
        {
            int statusBarHeight = -1;
            int resourceId = Activity.Resources.GetIdentifier("status_bar_height", "dimen", "android");
            if (resourceId > 0)
            {
                statusBarHeight = Activity.Resources.GetDimensionPixelSize(resourceId);
            }
            return statusBarHeight;
        }
    }
}

Activity属性是从MainActivity.cs设置的:

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(bundle);

            global::Xamarin.Forms.Forms.Init(this, bundle);

            StatusBar.Activity = this;

            LoadApplication(new App());
        }
    }

这是您从共享代码调用实现的方式:

int statusBarHeight = DependencyService.Get<IStatusBar>().GetHeight();

IOS平台的实施:

[assembly: Dependency(typeof(StatusBar))]
namespace StatusBarApp.iOS
{
    class StatusBar : IStatusBar
    {
        public int GetHeight()
        {
            return (int)UIApplication.SharedApplication.StatusBarFrame.Height;
        }
    }
}

答案 1 :(得分:1)

此页面上的其他答案对我不起作用。他们返回的值比实际状态栏小约 8 DP,导致我的所有计算都关闭。

这是一个有效的纯 Xamarin Forms 解决方案:

every

不幸的是,这依赖于假设您的所有页面都扩展到其完整的父容器,这并不总是正确的。

它还返回 DP 中的高度。如果您想要以像素为单位的高度,请乘以 // Tested only in Android private double GetNavbarHeight() { NavigationPage navPage = Application.Current.MainPage as NavigationPage; return navPage?.CurrentPage != null ? navPage.Height - navPage.CurrentPage.Height : 0; }

答案 2 :(得分:0)

常用代码(在App.xaml.cs中)

public static int StatusBarHeight {get; set;}

Android部分(在MainActivity.cs中,在OnCreate方法中)

int resourceId = Resources.GetIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0)
{
    App.StatusBarHeight = (int)(Resources.GetDimensionPixelSize(resourceId) / Resources.DisplayMetrics.Density);
}

iOS部分(在AppDelegate.cs中,在FinishedLaunching方法中)

App.StatusBarHeight = (int)UIApplication.SharedApplication.StatusBarFrame.Height;

因此App.StatusBarHeight将在App启动时初始化,然后您就可以在公共代码中的任何位置使用它。

答案 3 :(得分:0)

我使用了这样的依赖服务:

using System;
using System.IO;
using System.Threading.Tasks;
using Android.Content;
using Android.Content.Res;
using ProTrain.Droid.DependencyServices;
using ProTrain.Interfaces;

[assembly: Xamarin.Forms.Dependency(typeof(PlatformStuff))]
namespace MyApp.Droid.DependencyServices
{
    public class PlatformStuff : IPlatformStuff
    {
        internal static Func<Context> GetContext { get; set; }                        

        public int GetToolbarSize()
        {
            var Context = GetContext();
            TypedArray styledAttributes = Context.Theme.ObtainStyledAttributes(
                new int[] { Android.Resource.Attribute.ActionBarSize });
            var actionbar = (int)styledAttributes.GetDimension(0, 0);
            styledAttributes.Recycle();
            return actionbar;
        }
    }
}

然后在mainactivity中设置上下文:

protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);
    PlatformStuff.GetContext = () => this;
...