如何测量导航栏的高度

时间:2017-10-14 07:30:09

标签: xamarin xamarin.forms

我正在开发一个xamarin.forms应用程序,我需要测量导航栏的高度。我该如何测量它?我尝试关注this,但我不知道在哪里插入此代码:

Resources resources = context.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
    return resources.getDimensionPixelSize(resourceId);
}
return 0;

1 个答案:

答案 0 :(得分:4)

你走在正确的轨道上。问题是您的应用程序是跨平台的,导航栏是仅限Android的功能。因为您可能需要从共享代码中访问导航栏高度,所以您需要使用依赖注入。这是thoroughly described here

简而言之,您必须编写如下命名空间:

public interface INavigationBarInfo
{
    int Height { get; };
}

在Android项目中为Android实现它:

public class NavigationBarInfo : INavigationBarInfo
{
    public int Height => RetrieveHeight();

    private void RetrieveHeight()
    {
       //android specific code to retrieve the navigation bar height
       //you can use the answer you linked to in your question          
    }
}