rotorgames / Rg.Plugins.Popup

Xamarin Forms popup plugin
MIT License
1.15k stars 337 forks source link

Get a return value in Xamarin Forms #716

Closed DavidSpecter closed 2 years ago

DavidSpecter commented 2 years ago

🚀 Feature Requests

Get a return value in Xamarin Forms

Describe the feature

Simply return true or false when button in popup is pressed

Platforms affected (mark all that apply)

LuckyDucko commented 2 years ago

@DavidSpecter i would recommend using my plugin or following acaliaro's guide

Both of these are based on Rg

davidbuckleyni commented 2 years ago

@DavidSpecter did u get a solution for this I would rather use something that more widley used

davidbuckleyni commented 2 years ago

I actually achieved this using the messaging centre

LuckyDucko commented 2 years ago

@davidbuckleyni messagingcentre also works a treat in this scenario, its what i first before i made my plugin. just remember to unsubscribe when you 'close' your popup, otherwise there will be unusual headaches, as well as a performance impact

DavidSpecter commented 2 years ago

Hi, I resolve like that: In popup page add a TaskCompletionSource private TaskCompletionSource<bool> taskCompletionSource; public Task<bool> PopupClosedTask { get { return taskCompletionSource.Task; } }

protected override void OnAppearing() { base.OnAppearing(); taskCompletionSource = new TaskCompletionSource<bool>(); }

and than set the result when button is pressed private void ButtonClicked(object sender, EventArgs args) { taskCompletionSource.SetResult(true); Navigation.RemovePopupPageAsync(this); }

and the caller is like Popup popup = new popup(...) await PopupNavigation.Instance.PushAsync(popup); if (await popup.PopupClosedTask) { //do some stuff... }

Hope this can be helpfull.

David.

davidbuckleyni commented 2 years ago

@davidbuckleyni messagingcentre also works a treat in this scenario, its what i first before i made my plugin. just remember to unsubscribe when you 'close' your popup, otherwise there will be unusual headaches, as well as a performance impact

Yeah I found out about unloading the hard way wondering why my app started freezing i placed it in the close as seemed logical their.