Home > Enterprise >  How do i check which button was clicked in a previous page on a new page that it opened in xamarin
How do i check which button was clicked in a previous page on a new page that it opened in xamarin

Time:07-21

In a Xamarin.Form App, I have a CreateAccountPage.xaml with 2 buttons, now each button takes me to the HomePage.xaml, but I'd like to check and return a message on the HomePage.xaml specific to the button that was clicked. How do check in HomePage.xaml, which Button was Click from CreateAccountPage.xaml, see code behind below

using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xamarin.Essentials;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using AppCustomer.Models;


namespace AppCustomer.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class CreateAccountPage : ContentPage
    {
       

        public CreateAccountPage()
        {     
            InitializeComponent();
        }
     private void ButtonOne_Clicked(object sender, EventArgs e)
            {
               Navigation.PushAsync(new HomePage());
            }
    
     private void ButtonTwo_Clicked(object sender, EventArgs e)
            {
               Navigation.PushAsync(new HomePage());
            }
    }
}

CodePudding user response:

Pages are just C# classes, and you can pass data to them using any C# mechanism - constructor parameter, public property, public method, etc

for example

Navigation.PushAsync(new HomePage("Button One"));

or

Navigation.PushAsync(new HomePage("Button Two"));

CodePudding user response:

//CreateAccountPage Code Behind
//================================
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xamarin.Essentials;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using AppCustomer.Models;


namespace AppCustomer.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class CreateAccountPage : ContentPage
    {
       

        public CreateAccountPage()
        {     
            InitializeComponent();
        }
     private void ButtonOne_Clicked(object sender, EventArgs e)
            {
           string btnClicked = "ButtonOne"; 
               Navigation.PushAsync(new HomePage(btnClicked));
            }
    
     private void ButtonTwo_Clicked(object sender, EventArgs e)
            {
           string btnClicked = "ButtonTwo";     
               Navigation.PushAsync(new HomePage(btnClicked));
            }
    }
}



//HomePage Code Behind
//===============================
using AppCustomer.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace AppCustomer.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class HomePage : ContentPage
    {

        
        public HomePage(string btnClicked)
        {
            
            
            if (btnClicked == "ButtonOne")
            {
                DisplayAlertMessage(btnClicked);

            }

            if (btnClicked == "ButtonTwo")
            {
                DisplayAlertMessage(btnClicked);
            }



            InitializeComponent();
        }


        private void DisplayAlertMessage(string messages)
        {
            DisplayAlert("Message", "You Clicked the "   messages   " Button", "Ok");
        }



    }
}
  • Related