gpt4 book ai didi

c# - 动态 HeightRequest 不适用于 StackLayout

转载 作者:可可西里 更新时间:2023-11-01 08:46:37 25 4
gpt4 key购买 nike

在我的 XAML 中,我有这个 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的高度

在 OnAppearing 中,我为堆栈布局设置了特定的高度

FooterWrapper.HeightRequest = 196

在 iOS 中它的工作,我的新高度设置为 View 但在 android 中我的高度被忽略。

最佳答案

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

更改 HeightRequest 后,我们需要告诉 Xamarin.Forms 通过调用 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

enter image description here

关于c# - 动态 HeightRequest 不适用于 StackLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49445744/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com