Reputation: 390
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());
}
}
}
Upvotes: 0
Views: 130
Reputation: 496
The Xamarin documentation on Hierarchical Navigation covers a section on a couple of ways to pass data when navigating.
This includes passing the information directly through your page's constructors; the simplest technique and maybe the best for your scenario given the little amount of information required to pass to your page.
However if more information is needed, or richer objects are needed to be passed; Xamarin suggest passing it through Binding Contexts.
Lastly, it might be overkill if solely needed for simple id's or string parameters, but there are libraries like Prism that tackle this patterns almost seamlessly for a richer app dev experience.
Upvotes: 0
Reputation: 390
//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");
}
}
}
Upvotes: 0
Reputation: 89102
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"));
Upvotes: 1