简体   繁体   English

如何在Xamarin.Forms页面中获取标签栏高度

[英]How to get the tab bar height in a Xamarin.Forms page

I'm displaying an image within my content pages such that its height is a fixed percentage of the page height. 我在我的内容页面中显示一个图像,使其高度是页面高度的固定百分比。 I do this using a grid with star unit row height. 我这样做是使用星形单位行高的网格。 However, when the content pages are placed within a TabbedPage, the total grid height seems to be that of [the screen minus the tab bar height], so as a consequence, the image appears a bit shorter. 但是,当内容页面放置在TabbedPage中时,总网格高度似乎是[屏幕减去标签栏高度],因此图像看起来有点短。

If I could access the tab bar height within my content pages, it would be a simple adjustment to the percentage. 如果我可以访问内容页面中的标签栏高度,则可以对百分比进行简单调整。

Question: 题:

1) How do I get the tab bar height within a content page? 1)如何在内容页面中获得标签栏高度?

2) Is there a better way to achieve the desired result? 2)有没有更好的方法来达到预期的效果?

1) How do I get the tab bar height within a content page? 1)如何在内容页面中获得标签栏高度?

We can't get tabbar height from PCL directly.Just need a custom renderers for TabbedRenderer 我们无法直接从PCL获取tabbar高度。只需要TabbedRenderer的自定义渲染器

[assembly: ExportRenderer(typeof(TabbedPage), 
typeof(CustomTabbedPageRenderer))]
namespace TabbedPageWithNavigationPage.iOS
{
    class CustomTabbedPageRenderer : TabbedRenderer
    {
        public override void ViewWillAppear(bool animated)
        {
            base.ViewWillAppear(animated);
            App.TabHeight = (int)TabBar.Frame.Height;
        }
    }
}

Define TabHeight in App in PCL 在PCL中定义App中的TabHeight

public static int TabHeight { get; set; }

Get the height in TabbedPage TabbedPage获取高度

protected override void OnAppearing()
{
    base.OnAppearing();
    int height = App.TabHeight;
}

在此输入图像描述

2) Is there a better way to achieve the desired result? 2)有没有更好的方法来达到预期的效果?

We'r better not set the fixed frame on Image, and should not operate the frame manually . 我们最好不要在Image上设置固定帧,也不应该手动操作帧。

Aspect of Image can help us to show the image as we want. Image Aspect可以帮助我们按照自己的意愿显示图像。


AspectFit AspectFit

在此输入图像描述


AspectFill AspectFill

在此输入图像描述


Fill

在此输入图像描述

Edited: 编辑:

I would suggest you to get the screen size and calculate the percentage based on screen size and use the variable value instead of proportional value, in this way, you could use other the layout other than AbsoluteLayout, but I am using AbsoluteLayout for my example: 我建议你获取屏幕大小并根据屏幕大小计算百分比并使用变量值而不是比例值,这样,你可以使用除AbsoluteLayout之外的其他布局,但我使用AbsoluteLayout作为我的例子:

protected override void OnAppearing()
        {
            var imageWidth = this.Width;
            var imageHeight = this.Height * 0.25;
            AbsoluteLayout.SetLayoutBounds(image, new Rectangle(0, 0, imageWidth, imageHeight));
            AbsoluteLayout.SetLayoutFlags(image, AbsoluteLayoutFlags.None);
        }

*** In my test, when I used AbsoluteLayout proportional method ***在我的测试中,当我使用AbsoluteLayout比例法时

AbsoluteLayout.LayoutBounds = "0,0,1,0.25" AbsoluteLayout.LayoutFlags="All"

the outcome of both image size will have slightly different If the page use with or without Tabbedpage 两个图像大小的结果将略有不同如果页面使用有或没有Tabbedpage

So I suggest to use code behind to get the image size based on the [screen height * percentage]. 所以我建议使用后面的代码来获取基于[屏幕高度*百分比]的图像大小。

TabbedPage TabbedPage

XAML: XAML:

<?xml version="1.0" encoding="utf-8"?>
<TabbedPage 
    xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
    xmlns:local="clr-namespace:AbsoluteLayoutExample" x:Class="AbsoluteLayoutExample.AbsoluteLayoutExamplePage">

    <ContentPage x:Name="MyPage" Title = "Page 1">
        <AbsoluteLayout> 
           <Image x:Name="image" Source="icon.png" Aspect="AspectFill" />
        </AbsoluteLayout> 
    </ContentPage>

    <ContentPage Title = "Page 2">
        <Label Text="Hello" />
    </ContentPage>

</TabbedPage>

CodeBehind: 代码隐藏:

protected override void OnAppearing()
{
    var imageWidth = this.Width;
    var imageHeight = this.Height * 0.25;
    AbsoluteLayout.SetLayoutBounds(image, new Rectangle(0,0,imageWidth,imageHeight));
    AbsoluteLayout.SetLayoutFlags(image,AbsoluteLayoutFlags.None);
}

Content Page only: 仅限内容页面:

XAML: XAML:

<?xml version="1.0" encoding="utf-8"?>
<ContentPage Padding="0,20,0,0"
    xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
    xmlns:local="clr-namespace:ContentPageWidthAndHieght" 
    x:Class="ContentPageWidthAndHieght.ContentPageWidthAndHieghtPage">
    <AbsoluteLayout>
         <Image x:Name="image" Source="icon.png" Aspect="AspectFill"/>
    </AbsoluteLayout>
</ContentPage>

Code-Behind: 代码隐藏:

protected override void OnAppearing()
{
    var imageWidth = this.Width;
    var imageHeight = this.Height * 0.25;
    AbsoluteLayout.SetLayoutBounds(image, new Rectangle(0, 0, imageWidth, imageHeight));
    AbsoluteLayout.SetLayoutFlags(image, AbsoluteLayoutFlags.None);
}

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM