Unable to pass back a value from the RG popup dialog

Viewed 190

So I am trying to get a value out of an rgPopupPage which I have set as a property of list.

I am calling the popup page as such.

private  async void btnAddExistingPlayer_Clicked(object sender, EventArgs e)
{
    var team = grdteamManagment.SelectedItem as Team;
    if (team != null)
    {
        var page = new PlayerSelection(team.Id,true);
        await PopupNavigation.Instance.PushAsync(page); // lets show the rg popup page

        //normally in c# land I would this     
        var returnIds = page.PlayersId;

    }
}

But for some reason it wont work cause it pops the navigation back in before hand.

Here is my button code where I set my PlayersIds

private async void btnSelectPlayers_Clicked(object sender, EventArgs e)
{ 
PlayersId = new List<int>();
var item = lvPlayers.SelectedItem as PlayersSelectViewModel;
txtPlayerToInclude.Text = item.Id.ToString();
if (rbMultiplePlayers.IsChecked)
{
    var multiPlayers = lvPlayers.SelectedItems;
    string playersIds = string.Empty;
    foreach (PlayersSelectViewModel players in multiPlayers)
    {
        PlayersId.Add(players.Id);
        lblPlayerName.Text += players.FullName;

    }
    txtPlayerToInclude.Text = String.Join(",", PlayersId);
}
else
{
    var player = lvPlayers.SelectedItem as PlayersSelectViewModel;
    if (player != null)
    {
        PlayersId.Add(player.Id);

        txtPlayerToInclude.Text = string.Join("", PlayersId);
        lblPlayerName.Text += player.FullName;
    }
}
 await PopupNavigation.Instance.PopAsync();

}

I have looked at the messaging examples but don't really understand how that bolts together.

https://github.com/rotorgames/Rg.Plugins.Popup

2 Answers

Ok So I figured it out as such the best approach seems to be.

In my Calling Popup I have this. Ie on my Button close

MessagingCenter.Send<App, string>(App.Current as App, "PlayersSelected",
txtPlayerToInclude.Text);

Then in the function where I first call the popup I can subsubscribe to the name that is what is important here.

private async void btnAddExistingPlayer_Clicked(object sender, EventArgs e)
{
    var team = grdteamManagment.SelectedItem as Team;

    if (team != null)
    {
        var page = new PlayerSelection(team.Id, true);

        MessagingCenter.Subscribe<App, string>(App.Current, 
        "PlayersSelected", (snd, arg) =>
        {
            var items = arg.ToString();
        });
        await PopupNavigation.Instance.PushAsync(page);


    }
}

write event handler in you popup like this

public EventHandler OnTapped;

and then invoke it when you pop out popup like this

OnTapped?.Invoke(sender, e);

now you have to get in EventArgs value like this

Page.OnTapped += OnGridTapped;

private void OnGridTapped(object sender, EventArgs e)
{
    Debug.WriteLine(e);
}
Related