Xamarin Forms在按钮按下时将视图添加到页面?

我有一个视图,它是一个可折叠的列表,我希望能够按下一个按钮,上面写着添加另一个,然后它在当前视图下方添加另一个视图。这可以在 Xamarin Forms 中完成吗?


所以每次按下一个按钮时,添加另一个按钮:


<local:TimeSheetAccordion HeaderText="Customer #1" BackgroundColor="#FAFAFA" Padding="20, 20, 20, 20" />

页面代码:


<ScrollView>

        <StackLayout Padding="20, 20, 20, 20">

            <local:TimeSheetAccordion HeaderText="Customer #1" BackgroundColor="#FAFAFA" Padding="20, 20, 20, 20" />

            <local:TimeSheetAccordion HeaderText="Customer #2" BackgroundColor="#FAFAFA" Padding="20, 20, 20, 20" />


            <StackLayout Margin="0, 30, 0, 0" Orientation="Horizontal" HorizontalOptions="End">

                <Button Text="Reset" BorderRadius="6" />

                <Button Text="Submit Time Sheet" BackgroundColor="Blue" TextColor="White" BorderRadius="6" />

            </StackLayout>

        </StackLayout>

    </ScrollView>

page.xaml.cs 代码:


private void AddAnotherButton_Clicked(object sender, EventArgs e)

    {

        mainTimeLayout.Children.Add(new TimeSheetAccordion() {

            HeaderText = "Customer #1",

            BackgroundColor = Color.FromHex("#FAFAFA")

        });


    }

有没有办法可以设置它以便 HeaderText 递增?


波斯汪
浏览 106回答 1
1回答

狐的传说

您可以为您的 StackLayout 添加一个名称,以便在您的代码隐藏中引用。<ScrollView>&nbsp; &nbsp; &nbsp; &nbsp; <StackLayout x:Name="mainLayout" Padding="20, 20, 20, 20">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <local:TimeSheetAccordion HeaderText="Customer #1" BackgroundColor="#FAFAFA" Padding="20, 20, 20, 20" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <local:TimeSheetAccordion HeaderText="Customer #2" BackgroundColor="#FAFAFA" Padding="20, 20, 20, 20" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <StackLayout Margin="0, 30, 0, 0" Orientation="Horizontal" HorizontalOptions="End">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Button Text="Reset" BorderRadius="6" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Button Text="Submit Time Sheet" BackgroundColor="Blue" TextColor="White" BorderRadius="6" />&nbsp; &nbsp; &nbsp; &nbsp; </StackLayout>&nbsp; &nbsp; </StackLayout></ScrollView>在此之后,在您的 Page.xaml.csmainLayout.Children.Add(new TimeSheetAccordion());
打开App,查看更多内容
随时随地看视频慕课网APP