Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

How to await a DisplayAlert from a page (without async-Button-event)..?

FredyWengerFredyWenger CHInsider ✭✭✭✭✭

Hi all

This problem follows me, since I work with XF... :-)

On my search-page, I have various search-options (ListViews, SearchBars, etc.).
If the user has searched already and then search once again, I want to ask him, if he want to reset the search-options or not (I have to know this before the page-content is created).
Therefore, I want to display a simple DisplayAlert from the page-definition-code (where the content of the page is defined).
Example:

var AnswerYes = await DisplayAlert("Question","Do you want to reset the search-options?", "Yes", "No");
if (AnswerYes)
 {
  // Reset the search-options
 }
 else
  {
     // Do nothing
  }

The problem is simply, that I have to await the DisplayAlert and the page-definition-code is not async.
If I put the DisplayAlert in a async-function (Task) and call the function without await (the async-function then returns true or false), I got the error-message "A implicit convert from type 'System.Threading.Tasks.Task' in 'bool' is not possible.

So... how to solve this nasty problem?

Or.. is this simply not possible and the only way is, to show the DisplayAlert before the search-page is created?

Thanks for any answer...

Best Answer

Answers

  • MihaMarkicMihaMarkic SI ✭✭✭✭

    DisplayAlert("Question","Do you want to reset the search-options?", "Yes", "No").ContinueWith( ...)
    Make sure you pass TaskScheduler.FromCurrentSynchronizationContext argument to ContiueWith (use proper overload) so that it continues in UI thread (if you wish so).

  • FredyWengerFredyWenger CHInsider ✭✭✭✭✭
    edited November 2014

    @MihaMarkic‌
    Thanks a lot for taking the time to answer me :-)

    I was able to implement the code. Full example (for other users):

    DisplayAlert("Question", "Do you want to reset the search-options?", "Yes", "No").ContinueWith(t =>
     {
        if (t.Result)
        {
           // Reset search-options
           GV.SucheGuides.cKeySpezialitaet = "";
           }
          }, TaskScheduler.FromCurrentSynchronizationContext()); 
    

    The code works (the alert is displayed and I have access to t.Result (true/false)), but the code is running further...
    The effect is, that the search-page is displayed in background and over the search-page, the alert is displayed.
    I need the result (Yes/Now), to build the page, so the code has to stop, until the user has tapped Yes or Now...
    Is there a wait to stop the code...?
    Thanks once again...

    Update: Strange wise, the alert is not displayed in iOS (on my iPhone5) - in WP (Nokia Lumia 930) it is displayed.

  • FredyWengerFredyWenger CHInsider ✭✭✭✭✭

    @MihaMarkic‌:
    In this case (can't wait for result), I think, it's better to place the DisplayAlert in the Search-Icon event handler of the "mother-page" which is async.

    • the Alert then is displayed over the "mother-page"
    • the code of the search-page then is more readable (without ContinueWidth)
    • there is no "empty" page displayed in the background then
    • ...and also I don't have to care about the strange behavior (Alert is not displayed in iOS)

    But you helped me a lot... once again thanks for taking the time :-)

  • rsilesrsiles Member
    edited January 2018

    ..

Sign In or Register to comment.