0
votes

I'm creating a Xamarin.Forms app using c#.

There is a Button in a Page and i want to add a new Label in StackLayout that is in the Main Page when the Button is clicked.

I tried to set the FieldModifier property of the StackLayout public in the XAML file, but it didn't work...

This is the layout code (i want to add the label into MainStackLayout) and the cs method of the Button click:

 <ContentPage.Content>
    <ScrollView>
        <StackLayout BackgroundColor="white">
            <StackLayout Orientation="Horizontal" x:Name="MainStackLayout" x:FieldModifier="public" >
            </StackLayout>
            <Button 
                Text="Add Counter"
                BackgroundColor="darkgreen"
                HorizontalOptions="FillAndExpand"
                CornerRadius="0"
                HeightRequest="80"
                TextColor="white"
                FontAttributes="Bold"
                x:Name="AddCounter_btn"
               Clicked="AddCounter_btn_Clicked"
               >

            </Button>
        </StackLayout>
    </ScrollView>
</ContentPage.Content>

private void StartCount_btn_Clicked(object sender,EventArgs e)
{
    Label NCounterName =new Label();
    NCounterName.Text = counter_txt.Text.ToString();
    Label NCounterNumber = new Label();
    NCounterNumber.Text = "0000";`enter code here`
}
2
Would you like that when you Click the button both NCounterName and NCounterNumber are added to the MainStackLayout?Deczaloth
And, did you meant AddCounter_btn_Clicked instead of StartCount_btn_Clicked?Deczaloth
directly modifying one view from another page is generally a pretty horrible idea. Using MessagingCenter to do this would be a much better approach.Jason
Does your problem resolved?Jack Hua

2 Answers

0
votes

Let's say you have a button in Page1 which is StartCount_btn, and when you click this button, you want to add some labels to MainStackLayout in Page2.

In Page1, send a addLabelNotification when you click the StartCount_btn button:

private void StartCount_btn_Clicked(object sender, EventArgs e)
{
    MessagingCenter.Send<Object>(this, "addLabelNotification");
}

In Page2, Subscribe to the message and add labels when receive the message:

public partial class Page2 : ContentPage
{
    public Page2()
    {
        InitializeComponent();

        MessagingCenter.Subscribe<Object>(this, "addLabelNotification", (sender) =>
        {
            // Do something whenever the "addLabelNotification" message is received

            Label NCounterName = new Label();
            NCounterName.Text = "counter_txt";
            Label NCounterNumber = new Label();
            NCounterNumber.Text = "0000";

            MainStackLayout.Children.Add(NCounterName);
            MainStackLayout.Children.Add(NCounterNumber);

        });
    }
}

Refer: messaging-center

0
votes

I wrote this code here, could not check it but it will work. Your button gives you enough information to reach any View. You just use parent-child controls.

private void StartCount_btn_Clicked(object sender,EventArgs e)
{
    if(sender is Button myButton)
    {
        if(myButton.Parent is StackLayout myStackLayout)
        {
            Label NCounterName =new Label();
            //set your label's text
            myStackLayout.Add.Children(NCounterName);
        }
    }
}