【问题标题】:Xamarin add and place entry dynamicallyXamarin 动态添加和放置条目
【发布时间】: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 控件?
  • 我很想在第一个条目和按钮之间播放条目,或者换句话说,在按钮上方播放条目。我应该用网格试试吗?

标签: android xamarin


【解决方案1】:

将您的 Entry 包装在 StackLayout

<ContentPage.Content>
    <StackLayout x:Name="EntriesStackLayout">
        <Grid VerticalOptions="CenterAndExpand" Margin="20" RowSpacing="20">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <StackLayout x:Name="InnerEntries" Grid.Row="0">
              <Entry Placeholder="Label" x:Name="EntryTag" />
            </StackLayout>
            <Button Text="Attribut hinzufügen"  Grid.Row="1" Clicked="Button_Clicked"/>
      </Grid>
    </StackLayout>
</ContentPage.Content>

然后将新条目添加到该 StackLayout

AddEntry(EntriesStackLayout, "Attribut " + x.ToString());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-30
    • 2013-12-08
    • 1970-01-01
    • 2017-04-23
    • 2020-04-21
    • 2019-04-19
    • 1970-01-01
    相关资源
    最近更新 更多