动态HeightRequest在Xamarin表单中无法正常工作 [英] Dynamic HeightRequest not working correctly in xamarin forms

查看:77
本文介绍了动态HeightRequest在Xamarin表单中无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的XAML中,我有以下StackLayout:

In my XAML I have this StackLayout:

<StackLayout x:Name="FooterWrapper"
             Spacing="0"
             VerticalOptions="FillAndExpand"
             HorizontalOptions="FillAndExpand"
             BackgroundColor="Transparent">
    <BoxView BackgroundColor="Transparent" 
             HorizontalOptions="Center" 
             VerticalOptions="CenterAndExpand" 
             x:Name="can_applyComplete_topspace" />
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width=".07*" />
            <ColumnDefinition Width=".86*" />
            <ColumnDefinition Width=".07*" />
        </Grid.ColumnDefinitions>
         <Grid.RowDefinitions>
            <RowDefinition Height=".40*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <controls:AndroidButton x:Name="can_applycomplete_gotitbtn" 
                                Grid.Row="0" 
                                Text="{Binding SkipButtonText}" 
                                StyleId="uit_can_applycomplete_gotitbtn" 
                                FontFamily="Avenir Next" 
                                BackgroundColor="White" 
                                Grid.Column="1" 
                                HorizontalOptions="FillAndExpand" 
                                VerticalOptions="Fill" 
                                TextColor="Black" 
                                Clicked="Handle_SkipClicked" />
        <controls:CustomLabel Grid.Row="1" 
                              Grid.Column="1" 
                              Text="{i18n:TranslateExtension Text= res_cand_candjobcompliance_profilereachedlbl_list_footer}" 
                              VerticalOptions="Center" 
                              IsVisible="{Binding IsFooterVisible}" 
                              x:Name="cand_candjobcompliance_profilereachedlbl_list_footer" 
                              StyleId="uit_cand_candjobcompliance_profilereachedlbl_list_footer" 
                              TextColor="White" 
                              FontSize="13" />
    </Grid>
</StackLayout>

我需要动态控制StackLayout的高度

I need to dynamically control the Height of the StackLayout

在OnAppearing中,我正在为stacklayout设置特定的高度

In OnAppearing i am setting particular height for the stacklayout

FooterWrapper.HeightRequest = 196

在iOS中,我的新身高设置为视图,但在android中,我的身高被忽略.

In iOS its working, My new Height is set to the view but in android my height is ignored.

推荐答案

我们可以使用HeightRequest,但请记住,这只是一个请求.如果Xamarin.Forms没有足够的像素/点来满足请求,它将尽力而为.

We can use HeightRequest, but keep in mind that it is just a request. If Xamarin.Forms doesn't have enough pixels/points to fulfill the request, it will make a best effort.

更改HeightRequest之后,我们将需要告诉Xamarin.Forms通过调用ForceLayout();重绘StackLayoutContentPage

After changing HeightRequest, we will need to tell Xamarin.Forms to redraw the StackLayout and the ContentPage by calling ForceLayout();

public partial class MyContentPage : ContentPage
{
    ...

    void ResizeFooterWrapper(double heightRequest)
    {
        // Ensure resizing is marshaled to the UI Thread
        Device.BeginInvokeOnMainThread(() => 
        {
            FooterWrapper.HeightRequest = heightRequest;
            FooterWrapper.ForceLayout();
            this.ForceLayout();
        });
    }
}

示例应用

链接到示例应用程序: https://github.com/brminnick/DynamicStackLayoutSize/

using System;

using Xamarin.Forms;

namespace DynamicStackLayoutSize
{
    public class App : Application
    {
        public App() => MainPage = new MyPage();
    }

    class MyPage : ContentPage
    {
        readonly StackLayout _adjustableStackLayout;

        public MyPage()
        {
            _adjustableStackLayout = new StackLayout
            {
                HorizontalOptions = LayoutOptions.Center,
                VerticalOptions = LayoutOptions.Center,
                BackgroundColor = Color.Green,
                Children = {
                    new Label{ Text = "Hello" },
                    new Label{ Text = "World" }
                }
            };

            var resizeButton = new Button { Text = "Resize" };
            resizeButton.Clicked += (s, e) =>
            {
                if (_adjustableStackLayout.HeightRequest == 196)
                    ResizeStackLayout(-1);
                else
                    ResizeStackLayout(196);
            };

            Content = new StackLayout
            {
                HorizontalOptions = LayoutOptions.Center,
                VerticalOptions = LayoutOptions.Center,
                BackgroundColor = Color.Red,
                Children ={
                    _adjustableStackLayout,
                    resizeButton
                }
            };
        }

        void ResizeStackLayout(double heightRequest)
        {
            Device.BeginInvokeOnMainThread(() =>
            {
                _adjustableStackLayout.HeightRequest = heightRequest;
                _adjustableStackLayout.ForceLayout();
                this.ForceLayout();
            });
        }
    }
}

示例应用Gif

这篇关于动态HeightRequest在Xamarin表单中无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆