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

时间:2019-01-16 14:24:59

标签: c# xamarin 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递增?

1 个答案:

答案 0 :(得分:0)

您可以在StackLayout中添加一个名称,以在代码隐藏中引用。

<ScrollView>
        <StackLayout x:Name="mainLayout" 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

mainLayout.Children.Add(new TimeSheetAccordion());