【发布时间】:2020-05-10 11:28:08
【问题描述】:
我是 Xamarin 和编码的初学者,我正在尝试使用 Button Clicked Event 动态添加条目。 我设法在一些旧答案的帮助下放置了一个新条目,如下所示: How to add entry fields dynamically during run time in Xamarin.Forms
但是,我不知道如何在布局中定义新条目的位置。 它将条目放在底部。
我的 XAML - 代码
<ContentPage.Content>
<StackLayout x:Name="EntriesStackLayout">
<Grid VerticalOptions="CenterAndExpand" Margin="20" RowSpacing="20">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Entry Placeholder="Label" x:Name="EntryTag" Grid.Row="0"/>
<Button Text="Attribut hinzufügen" Grid.Row="1" Clicked="Button_Clicked"/>
</Grid>
</StackLayout>
</ContentPage.Content>
我的 XAML CS - 代码
int x = 1;
private void Button_Clicked(object sender, EventArgs e)
{
AddEntry(EntriesStackLayout, "Attribut " + x.ToString());
x++;
}
private void AddEntry(StackLayout sl, string name)
{
Entry entry = new Entry() { Placeholder = name,
};
sl.Children.Add(entry);
}
【问题讨论】:
-
您正在使用 StackLayout,它只是一个接一个地垂直堆叠控件。您想如何放置新的 Entry 控件?
-
我很想在第一个条目和按钮之间播放条目,或者换句话说,在按钮上方播放条目。我应该用网格试试吗?