how can i get status bar height in xamarin.forms?

我们两清 提交于 2020-12-03 08:12:26

问题


I am trying to get status / action bar height in my application. I got some of how we can get it in native android. can we get status / action bar height in xamarin.forms ?if yes then how? please help if anybody have idea about it.


回答1:


You can do this by creating your own dependency service. (https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/dependency-service/introduction/)

In your shared code, create an interface for example IStatusBar:

public interface IStatusBar
    {
        int GetHeight();
    }

Add implementation for Android platform:

[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 property is set from 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());
        }
    }

This is how you call the implementation from the shared code:

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

Implementation for IOS platform:

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



回答2:


Common code (in App.xaml.cs)

public static int StatusBarHeight {get; set;}

Android part (in MainActivity.cs, in the OnCreate method)

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

iOS part (in AppDelegate.cs, in the FinishedLaunching method)

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

So App.StatusBarHeight will be initialized when the App will be launched, then you will be able to use it anywhere in the common code.




回答3:


I used a dependency service like this:

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;
        }
    }
}

Then in the mainactivity I set the context:

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


来源:https://stackoverflow.com/questions/48276053/how-can-i-get-status-bar-height-in-xamarin-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!