maui:布局末尾的按钮通常不起作用



这太烦人了…一如既往的"新"框架;它们都有自己的问题,但在这种情况下,这是一个基本的功能。

简而言之,如果我在网格中有一个按钮,它是最后一个元素(在本例中是第二行),我这样做:

<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<Layout Grid.Row="0">
</Layout>
<Button Margin="0,50,0,50" Grid.Row="1" WidthRequest="180" Text="{x:Static res:Strings.TestBeenden}" Clicked="Button_Clicked"/>

按钮将呈现在它应该出现的位置,但是它不能被点击。

只有当我给第一个RowDef一个像这样的值

<Grid.RowDefinitions>
<RowDefinition Height="100"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>

按钮是可以点击的。但是我不能给它一个静态的大小——这是不可行的。

还有谁有这个问题,或者可能找到一个解决方案?

我有更多的信息;当数据稍后被填充到滚动视图中时,问题就出现了。它不会调整布局的大小。一种解决方法是为scrollview中的所有子布局添加一个事件处理程序,这些布局可能会因布局变化而调整大小(项目被删除或添加),然后在scrollview上调整大小:

private void VerticalStackLayout_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
(scrollView as IView)?.InvalidateMeasure(); // if not done, everything else will fail 
}

在XAML:

<VerticalStackLayout PropertyChanged="VerticalStackLayout_PropertyChanged"(将此添加到scrollview内部的每个布局中,并且可以在运行时更改其内容)

this in the end works.

所以我的猜测是,按钮周围的点击框不会被向下移动,而UI元素本身会。

我测试了您提供的代码。我在按钮上设置了点击事件,它运行得很好。当我点击按钮时,标签会改变。

下面是我测试的代码:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<StackLayout Grid.Row="0" BackgroundColor="Aqua">
<Label x:Name="mylabel" ></Label>
</StackLayout>
<Button Margin="0,50,0,50" Grid.Row="1" WidthRequest="180" Text="estBeenden" Clicked="Button_Clicked" />

</Grid>

背后的代码:

int count = 0;
public MainPage()
{
InitializeComponent();
BindingContext = this;

}
private void Button_Clicked(object sender, EventArgs e)
{
count++;
mylabel.Text = $"{count}";
SemanticScreenReader.Announce(mylabel.Text);
}