1
votes

Have done a lot of trawling and found some bits of info I needed, but still haven't quite worked out how to fit all the pieces together. I either get 1. errors that prevent build, 2. no message appearing, or 3. message appears but program continued on in the meantime regardless.

So, I want to ask a question of the user which relates to reloading info from their previous session (or start fresh). It should wait for this answer. I know I need async/await in there somewhere, and I also need the answer (as a boolean or string). I also have found I need Device.BeginInvokeOnMainThread and/or OnAppearing in order for the message to actually appear in the first place. Most examples I've seen revolve around tying it to a button click or other event, but I want this to run at the beginning, and not do anything else until I have the answer (since the loading of info on the first screen depends on the answer).

So, for DisplayAlert("Resume?","Do you want to reload your data from last session?","yes","no"), how do I wrap that up so that it appears upon start-up, and nothing happens until answered (so that I'm either reloading or reinitialising), and I can access that answer from elsewhere (i.e. stick the result in some kind of variable to be accessed by the rest of the code)? It's my first time using DisplayAlert, my first time using async/await,and first time using Device.BeginInvoke... - I keep tinkering with different variations but keep getting one of the 3 aforementioned results. :-(

3
It only works from a Page and you have to use awaitSten Petrov
Think I have found why this isn't working. Reading between the lines in Charles Petzold's book you can't do this on your main page.... "All the other pages in the application are intrinsically different from that start page, however, because they fall into two different categories: modal pages and modeless pages. In user interface design, “modal” refers to something that requires user interaction before the application can continue". I think what I need to do is write a menu as the main page where user selects to resume with previous info or start fresh and go from there.donaldp
I'm glad to know someone is reading that book. No wonder things don't work :) Here's a better read developer.xamarin.com/guides/xamarin-forms/user-interface/…Sten Petrov
And here's a whole sample project from Xamarin Samples github: github.com/xamarin/xamarin-forms-samples/tree/…Sten Petrov
Thanks Sten, but already saw that page and it didn't really help me to get it working. It tells you how to use DisplayAlert, but not what other code you need to wrap around it to get it working the way I want (which is not to call the constructor until you have a response - however I believe I now know what I need to about OnAppearing from someone else to get it working the way I want). Will check out those samples later. :-)donaldp

3 Answers

2
votes

You can use the DisplayAlert in the OnAppearing Method of the page.

protected async override void OnAppearing()
    {
        var answer = await DisplayAlert("Title", "Message", "Positive Button - Yes", "Nagative Button - No");
        if (answer)
        {
            //Do your code here.
        }
    }

Hope this will work for you.

0
votes

I just received a notification recently that I've received a "popular question" award for this. When I came back to look at it I discovered I never posted my working solution - so sorry about that! I shall fix that now (drags out the 2 year old code...)

Some other people in this thread gave me pieces of the puzzle (thank you Sten Petrov and Chandresh Khambhayata - not letting me tag for some reason), but the missing one (that I got somewhere else - don't even remember where now, but probably forums.xamarin.com) was that I also needed base.OnAppearing() in there. Here is the complete block of relevant code...

protected async override void OnAppearing() {
    var Response=await DisplayAlert("Resume?", "Do you want to resume from where you left off?", "Resume", "Start fresh");
    base.OnAppearing();
    string Resuming=Response.ToString();
    if (Resuming=="True") { //your Resuming and other remaining code here
-1
votes

I use Acr.Userdialogs nuget package. With this you can call dialogs from your mvvm code. You can do

var needToResume = await UserDialogs.Instance.ConfirmAsync(
   message: "Do you want to reload your data from last session?",
   title: "Resume?",
   okText: "yes",
   cancelText: "no",
   cancelToken: cancelationTokenSource.Token);

from an async method. Or

var needToResume = UserDialogs.Instance.ConfirmAsync(
   message: "Do you want to reload your data from last session?",
   title: "Resume?",
   okText: "yes",
   cancelText: "no",
   cancelToken: cancelationTokenSource.Token).Result;

from general methods and place code to navigate/do_other_things right after dialog call. Also you can use callback:

UserDialogs.Instance.Confirm(new ConfirmConfig
   {
      AndroidStyleId = null,
      Title = "Resume?",
      Message = "Do you want to reload your data from last session?",
      OkText = "yes",
      CancelText = "no",
      OnAction = DoThings
   });

private void DoThings(bool needToResume)
{
    //do things
}