Reputation: 171
I'm using Rg.Plugins.Popup.Pages for popup page, when I press a button the popup appears in the middle of the page and I have a radiobuttons to select in it and when I finish, I touch on the transparent background to exit. My problem is that in the background page I have a label I need to modify according to what I chose in the popup page on the onAppearing method of the page, whenever I touch on the background of the popup page disappear and the onAppearing of the background page never get called!
The button is placed in the background page
protected override void OnAppearing()
{
for (int i = 0; i < TestingClass.multiselection.Count; i++)
{
if (TestingClass.multiselection[i].is_selected)
btn.Text += TestingClass.multiselection[i].description;
}
base.OnAppearing();
}
I'm getting the values from a static class when the popup is appeared the values are saved like this:
private void RadioButton_Clicked(object sender, System.EventArgs e)
{
var rb = sender as RadioButton;
var selection = rb.BindingContext as multi_selection;
if (selection != null && selection.is_selected == true)
{
selection.is_selected = false;
}
else
if (selection != null && selection.is_selected == false)
{
selection.is_selected = true;
TestingClass.multiselection.Add(selection);
}
}
Upvotes: 1
Views: 1095
Reputation: 666
There are two ways of doing it (as I know):
You can create EventHandler
in your PopupPage
and subscribe it while initializing this popupPage
:
MyPopupPage popup = new MyPopupPage();
// Subscribe event
popup.Save += SaveEventHandlerArgs;
Navigation.PushPopupAsync(popup);
In your popupPage
after selecting a value, call your eventHandler and pass value as argument. After that SaveEventHandlerArgs
method would be called, where you can get your value and do with it what you want;
You can use MessagingCenter. Examples are in the link.
Hope it helps!
Upvotes: 2
Reputation: 15816
Both OnApperearing and OnDisappearing will not be get called when you add a popup in the view.
In Apple document,it says:
viewWillAppear:
This method is called before the view controller's view is about to be added to a view hierarchy and before any animations are configured for showing the view.
In Android:
OnCreate
OnCreate is the first method to be called when an activity is created.
Adding a popup or removing a popup is neither create/remove a new view or add/remove the view to/from hierarchy. So, these lifecycle methods won't be called at that time.
Solution:
About how to set the button's text after selecting in popup, I agree with Денис Чорный
's answer, let me clarify it with code.
1.Use eventHandle
:
In the popup page, define a EventHandler
:
public EventHandler SelectedStringChanged { get; set; }
In your RadioButton_Clicked
, add a line to call SelectedStringChanged:
private void RadioButton_Clicked(object sender, System.EventArgs e)
{
var rb = sender as RadioButton;
var selection = rb.BindingContext as multi_selection;
if (selection != null && selection.is_selected == true)
{
selection.is_selected = false;
}
else
if (selection != null && selection.is_selected == false)
{
selection.is_selected = true;
TestingClass.multiselection.Add(selection);
}
//Add this line
SelectedStringChanged(sender, e);
}
In the background page, create a method to deal with btn.text:
private void Handle_SelectedStringChanged(object sender, EventArgs rags)
{
// ... your code
for (int i = 0; i < TestingClass.multiselection.Count; i++)
{
if (TestingClass.multiselection[i].is_selected)
btn.Text += TestingClass.multiselection[i].description;
}
}
In the popup method,consume the Handle_SelectedStringChanged
method:
private async void OnOpenPupup(object sender, EventArgs e)
{
_loginPopup.SelectedStringChanged += Handle_SelectedStringChanged;
await PopupNavigation.Instance.PushAsync(_loginPopup);
}
MessageingCenter
will also work.In the background page:
MessagingCenter.Subscribe<MainPage>(this, "stringSelected", (sender) => {
// do something whenever the "stringSelected" message is sent
for (int i = 0; i<TestingClass.multiselection.Count; i++)
{
if (TestingClass.multiselection[i].is_selected)
btn.Text += TestingClass.multiselection[i].description;
}
});
In you popup page:
private void RadioButton_Clicked(object sender, System.EventArgs e)
{
var rb = sender as RadioButton;
var selection = rb.BindingContext as multi_selection;
if (selection != null && selection.is_selected == true)
{
selection.is_selected = false;
}
else
if (selection != null && selection.is_selected == false)
{
selection.is_selected = true;
TestingClass.multiselection.Add(selection);
}
MessagingCenter.Send<MainPage>(this, "stringSelected");
}
Refer: messaging-center
Upvotes: 1
Reputation: 34148
Depending on the platform the OnApperearing
might only be called once. Maybe you can turn it around and hook into the OnDisappearing
of the popup page and use that to update your values?
Upvotes: 0