locked
How generate a layout with a Button ? RRS feed

  • Question

  • User395338 posted

    Hello, I'm looking for a way to generate on a page (let's call it ArticlePage) a stacklayout that would have entries thanks to a button (let's call it Add), and when generating it, we stay on the ArticlePage, the stack is added with the Add button underneath and so on... I don't really see how I can make something clean, an idea ? Thank you!

    Monday, June 22, 2020 10:32 PM

Answers

  • User371688 posted

    You can refer to the following code:

    MainPage.xaml

    <?xml version="1.0" encoding="utf-8" ?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:d="http://xamarin.com/schemas/2014/forms/design"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                 mc:Ignorable="d"
                 x:Class="FormApp1.MainPage">
    
        <StackLayout x:Name="layout">
            <Button Text="add" Clicked="Addbutton"/>
        </StackLayout>
    </ContentPage>
    

    MainPage.xaml.cs

     public partial class MainPage : ContentPage
        {
            // Define a field for StackLayout
            StackLayout parent;
    
            public MainPage()
            {
                InitializeComponent();
            }
    
            private void Addbutton(object sender, EventArgs e)
            {
                // Define a new button
                Button newButton = new Button { Text = "New Button" };
    
                // Set StackLayout in XAML to the class field
                parent = layout;
    
    
                StackLayout child_stackLayout = new StackLayout {
                    Orientation = StackOrientation.Vertical,
                    HorizontalOptions = LayoutOptions.FillAndExpand,
                    Margin = new Thickness(20),
                    Padding = 20,
                    BackgroundColor=Color.Accent
                };
    
                // Add the new button to the StackLayout
                child_stackLayout.Children.Add(newButton);
    
                // Add the new stackLayout to the parent StackLayout
                parent.Children.Add(child_stackLayout);
            }
        }
    

    The result is:

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, June 23, 2020 8:15 AM

All replies

  • User371688 posted

    You can refer to the following code:

    MainPage.xaml

    <?xml version="1.0" encoding="utf-8" ?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:d="http://xamarin.com/schemas/2014/forms/design"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                 mc:Ignorable="d"
                 x:Class="FormApp1.MainPage">
    
        <StackLayout x:Name="layout">
            <Button Text="add" Clicked="Addbutton"/>
        </StackLayout>
    </ContentPage>
    

    MainPage.xaml.cs

     public partial class MainPage : ContentPage
        {
            // Define a field for StackLayout
            StackLayout parent;
    
            public MainPage()
            {
                InitializeComponent();
            }
    
            private void Addbutton(object sender, EventArgs e)
            {
                // Define a new button
                Button newButton = new Button { Text = "New Button" };
    
                // Set StackLayout in XAML to the class field
                parent = layout;
    
    
                StackLayout child_stackLayout = new StackLayout {
                    Orientation = StackOrientation.Vertical,
                    HorizontalOptions = LayoutOptions.FillAndExpand,
                    Margin = new Thickness(20),
                    Padding = 20,
                    BackgroundColor=Color.Accent
                };
    
                // Add the new button to the StackLayout
                child_stackLayout.Children.Add(newButton);
    
                // Add the new stackLayout to the parent StackLayout
                parent.Children.Add(child_stackLayout);
            }
        }
    

    The result is:

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, June 23, 2020 8:15 AM
  • User395338 posted

    Ok, i see the principle, i'll go in that direction, thank you very much.

    Tuesday, June 23, 2020 11:52 AM