Forum General
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

how do i change label text in second page with a button clicked on the first page

raddradd Member ✭✭
edited August 4 in General

hi everyone,
so in my app i have 2 pages and i would like to change the label text in the second page by clicking on a button in the first page.
the label i the second page already contains a text value and i would like to update it only the button is click. i cant seem to figure out how to start. it

first page

second page

Tagged:

Answers

  • Adeel984Adeel984 Member ✭✭✭

    Either use MVVM approach or make a static variable that holds the reference of your Lable.
    Here is an example

    public partial class secondPage {
    
    public static Label StaticLabel;
    
    public secondPage()
            {
                InitializeComponent();
        StaticLabel = myLabel; // myLabel is the name of your Label in XAML.
            }
    

    And from first page use it like

    secondPage.StaticLabel.Text = "some text";
    
  • raddradd Member ✭✭

    @Adeel984 thank you very much. i appreciate it. so one more question. so when i click on the button in the firstpage i want it to navigate to a thirdpage which is a popuppage but at the same time change the label text in secondpage. i have multiple label. could you please elaborate with a code. thanks soo much

  • raddradd Member ✭✭

    @Adeel984 so i tried using mvvm but came across some challenges. all i am tryying to do here is that, when the user click on the first button in the mainpage i want it to change the labels text in the second page but navigate me to a popup page which is bound to the second page. so whatever the input is in the popup page i want it to display in the second page. when i click on the second page button in the first page everything should be there. could you please take a look at this code

    mvvm

    public class Mainviewmodel:baseview
    {
    public Mainviewmodel()
    {
    Mandem = true;
    Mandem1 = false;
    BtnCommand = new Command(() =>
    {
    Mandem = !Mandem;

            });
    
            BtnCommand1 = new Command(() =>
            {
                Mandem1 = !Mandem1;
            });
            }
        bool mandem;
        public bool Mandem
        {
            set
            {
                mandem = value;
                OnPropertyChanged();
            }
            get => mandem;
        }
    
        bool mandem1;
        public bool Mandem1
        {
            set
            {
                mandem1 = value;
                OnPropertyChanged();
            }
            get => mandem1;
        }
         public ICommand BtnCommand { set; get; }
        public ICommand BtnCommand1 { set; get; }
    
    
    
    
        //firstname:
        string firstname = string.Empty;
        public string Firstname
        {
            get => firstname;
            set
            {
                if (firstname == value)
                    return;
                else
                {
                    firstname = value;
                    OnPropertyChanged(nameof(Firstname));
                    OnPropertyChanged(nameof(DisplayFirstname));
                }
            }
        }
        public string DisplayFirstname => $"Firstname: {Firstname}";
    }
    

    }

    Then in my firstpage which is the main page i have the following





    behind the main page i have the following in the clicked event for each button

    async void btnMens_Clicked(object sender, EventArgs e)
    {
    var Prostate = new prostate
    {
    orientation = "Orientation:",
    site = "Site:",
    Bladderfilling = "Bladder Filling:",
    }
    var secondPage = new SecondPage();
    secondPage.BindingContext = Prostate;
    await Navigation.PushAsync(secondPage);
    await PopupNavigation.Instance.PushAsync(new helo());
    }

    async void btnWomen_Clicked(object sender, EventArgs e)
    {
    var breastboard = new Breastboard
    {
    laterality = "Laterality:",
    orientation1 = "Orientation:"
    }
    var secondPage = new SecondPage();
    secondPage.BindingContext = Prostate;
    await Navigation.PushAsync(secondPage); when i click on this button or the previous on i dont want it to navigate me to the second page but rather to the popup page but at the same time change the labels in the second page.
    await PopupNavigation.Instance.PushAsync(new helo());

    private async void btnSecond_Clicked(object sender, EventArgs e)
    {
    await Navigation.PushAsync(new SecondPage());
    }

    on the second page i am using isvisbile property to hide the stacklayout

    <Grid.ColumnDefinitions>

            </Grid.ColumnDefinitions>
        <StackLayout IsVisible="{Binding Mandem1}" Grid.Column="0" Grid.Row="0" >
                <StackLayout Orientation="Horizontal">
                    <Label Text="{Binding orientation}"/>
                    <Label Text="{Binding DisplayFirstname}"  />
                </StackLayout>
    




    and last the popup page

    Entry x:Name="Enter_Firstname" Text="{Binding Firstname}"
    FontSize="Default" Placeholder="Enter First name" WidthRequest="200"
    VerticalOptions="Center" BackgroundColor="WhiteSmoke" >

  • Adeel984Adeel984 Member ✭✭✭

    @radd said:
    @Adeel984 so i tried using mvvm but came across some challenges. all i am tryying to do here is that, when the user click on the first button in the mainpage i want it to change the labels text in the second page but navigate me to a popup page which is bound to the second page. so whatever the input is in the popup page i want it to display in the second page. when i click on the second page button in the first page everything should be there. could you please take a look at this code

    mvvm

    public class Mainviewmodel:baseview
    {
    public Mainviewmodel()
    {
    Mandem = true;
    Mandem1 = false;
    BtnCommand = new Command(() =>
    {
    Mandem = !Mandem;

            });
    
            BtnCommand1 = new Command(() =>
            {
                Mandem1 = !Mandem1;
            });
            }
        bool mandem;
        public bool Mandem
        {
            set
            {
                mandem = value;
                OnPropertyChanged();
            }
            get => mandem;
        }
    
        bool mandem1;
        public bool Mandem1
        {
            set
            {
                mandem1 = value;
                OnPropertyChanged();
            }
            get => mandem1;
        }
         public ICommand BtnCommand { set; get; }
        public ICommand BtnCommand1 { set; get; }
    
    
    
    
        //firstname:
        string firstname = string.Empty;
        public string Firstname
        {
            get => firstname;
            set
            {
                if (firstname == value)
                    return;
                else
                {
                    firstname = value;
                    OnPropertyChanged(nameof(Firstname));
                    OnPropertyChanged(nameof(DisplayFirstname));
                }
            }
        }
        public string DisplayFirstname => $"Firstname: {Firstname}";
    }
    

    }

    Then in my firstpage which is the main page i have the following





    behind the main page i have the following in the clicked event for each button

    async void btnMens_Clicked(object sender, EventArgs e)
    {
    var Prostate = new prostate
    {
    orientation = "Orientation:",
    site = "Site:",
    Bladderfilling = "Bladder Filling:",
    }
    var secondPage = new SecondPage();
    secondPage.BindingContext = Prostate;
    await Navigation.PushAsync(secondPage);
    await PopupNavigation.Instance.PushAsync(new helo());
    }

    async void btnWomen_Clicked(object sender, EventArgs e)
    {
    var breastboard = new Breastboard
    {
    laterality = "Laterality:",
    orientation1 = "Orientation:"
    }
    var secondPage = new SecondPage();
    secondPage.BindingContext = Prostate;
    await Navigation.PushAsync(secondPage); when i click on this button or the previous on i dont want it to navigate me to the second page but rather to the popup page but at the same time change the labels in the second page.
    await PopupNavigation.Instance.PushAsync(new helo());

    private async void btnSecond_Clicked(object sender, EventArgs e)
    {
    await Navigation.PushAsync(new SecondPage());
    }

    on the second page i am using isvisbile property to hide the stacklayout

    <Grid.ColumnDefinitions>


    </Grid.ColumnDefinitions>


    and last the popup page


    Entry x:Name="Enter_Firstname" Text="{Binding Firstname}"
    FontSize="Default" Placeholder="Enter First name" WidthRequest="200"
    VerticalOptions="Center" BackgroundColor="WhiteSmoke" >

    You BindingContext should be Mainviewmodel as you are maintaining data. NOT Prostate

Sign In or Register to comment.