Reputation: 1959
Hi I have four buttons in my xamarin.forms application.Each button click will open a listview in a Popup.I am trying to open same popup page on each button click.I am using messeging centre for returning the listview selected item back to button page. Where I am stuck is how can I distinguish the button click in popup page?Should I use a flag or somethong?
My Button page
void Button1_Tapped(object sender, EventArgs e)
{
PopupNavigation.PushAsync(new AnswerPopup(tranzaction));
MessagingCenter.Subscribe<MyMessage>(this, "AnsData", (value) =>
{
string receivedData = value.Myvalue;
Answer1.Text = receivedData;
});
}
void Button2_Tapped(object sender, EventArgs e)
{
PopupNavigation.PushAsync(new AnswerPopup(tranzaction));
MessagingCenter.Subscribe<MyMessage>(this, "AnsData", (value) =>
{
string receivedData = value.Myvalue;
Answer2.Text = receivedData;
});
}
void Button3_Tapped(object sender, EventArgs e)
{
PopupNavigation.PushAsync(new AnswerPopup(tranzaction));
MessagingCenter.Subscribe<MyMessage>(this, "AnsData", (value) =>
{
string receivedData = value.Myvalue;
Answer3.Text = receivedData;
});
}
My popup page
private string selectedItem;
private void AnsList_Tapped(object sender, SelectedItemChangedEventArgs e)
{
var selectedCategory = e.SelectedItem as Answer;
if (selectedCategory != null)
selectedItem = selectedCategory.Text;
MessagingCenter.Send(new MyMessage() { Myvalue = selectedItem.ToString() }, "AnsData");
PopupNavigation.PopAsync();
}
Upvotes: 0
Views: 159
Reputation: 89102
first, you don't need to subscribe multiple times, just do it once per page (in the constructor, typically)
second, add a property to MyMessage
that will tell you which button as called
MessagingCenter.Subscribe<MyMessage>(this, "AnsData", (value) =>
{
string receivedData = value.Myvalue;
switch (value.Question) {
case "Q1":
Answer1.Text = receivedData;
break;
case "Q2":
Answer2.Text = receivedData;
break;
case "Q3":
Answer3.Text = receivedData;
break;
}
});
finally, when calling AnswerPopup
, pass a key for the question (which it will then need to pass back via MyMessage
when calling MessagingCenter.Send()
void Button1_Tapped(object sender, EventArgs e)
{
// use "Q2", "Q3", etc as appropriate
PopupNavigation.PushAsync(new AnswerPopup(tranzaction, "Q1"));
}
Upvotes: 1