How do i dynamically create an Entry on a vertical stack layout?

Daniel Salami 1 Reputation point
2022-12-26T01:51:15.287+00:00

I have a page with a vertical stack layout. I ask the user a question, and based on their response, I can either show an entry or take them to another page. I.E. This Input entry would only appear if the user clicked "Add" Button. I can't seem to find anything that'll help. Aby help would be much appreciated.

Developer technologies | .NET | .NET MAUI
{count} votes

1 answer

Sort by: Most helpful
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 36,436 Reputation points Microsoft External Staff
    2022-12-27T04:38:18.947+00:00

    Hello @Daniel Salami ,

    You can add the Entry on the page and set IsVisible false, then set IsVisible true according to user's response. You can refer to the following code :
    XAML

    <VerticalStackLayout  
              >  
                <Label  
                    Text="Question 1"  
                    FontSize="18"/>  
                <Entry x:Name="Question1Entry" Placeholder="Please enter the answer"></Entry>  
      
                <StackLayout x:Name="SecondQuestionLayout" IsVisible="false">  
                    <Label  
                    Text="Question 2"  
                    FontSize="18"  
                     />  
                    <Entry Placeholder="Please enter the answer"></Entry>  
                </StackLayout>  
                 
                <Button  
                    x:Name="CounterBtn"  
                    Text="Add"  
                    Clicked="OnCounterClicked"  
                    HorizontalOptions="Center" />  
            </VerticalStackLayout>  
    

    "Add" button click event

    private void OnCounterClicked(object sender, EventArgs e)  
          {  
          if (  Question1Entry.Text == "111")// base on the response, show the next entry  
                {  
                SecondQuestionLayout.IsVisible = true;  
                }  
                else  
                {  
                      //go to the next page  
                }  
        }  
    

    It's recommended that you use Data binding and MVVM and handle logic in the ViewModel..

    Best Regards,
    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.