1

I'm doing a simple try/catch (in a PCL project) to validate the users connection to the app, but I cant seem to find the DisplayAlert() method used in the Xamarin websites example.

Here are my usings:

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Security;
using System.Diagnostics;

Here is the code:

public async Task Connexion()
        {
            // on met en place un try catch pour déceler toute erreur dans la procédure de connexion
            try
            {
                // url de récupération du json de l'acteur
                string urlActeur = "http://10.0.0.5/ppe3JoJuAd/gsbAppliFraisV2/webservices/w_visiteur.php" + "?" + "login=" + Login + "&" + "pass=" + Pass;

                //instanciation du client http qui envoi un header json
                HttpClient clientActeur = new HttpClient();
                clientActeur.DefaultRequestHeaders.Accept.Clear();
                clientActeur.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                //réponse à la requête Http
                var response = await clientActeur.GetAsync(urlActeur);
                var json = response.Content.ReadAsStringAsync().Result;
                var acteurJson = JsonConvert.DeserializeObject<ActeurJson>(json);

                //on vérifie les informations de connexion du user (ici cela se ait avec oldMdp car pas d'implémentation du SHA1 actuellement en Xamarin, auquel cas nous auions converti le contenu du champ pass en sha1 puis vérification avec le champ mdp de l'acteur)
                if (acteurJson.Acteur.login == login && acteurJson.Acteur.mdp == acteurJson.Acteur.oldMdp)

                    App.Current.MainPage = new VisitePage();
            }
            catch
            {
                await DisplayAlert()//intelisense does not find the using or the required dll

            }

where should I look or what should I do to display the message ?

Sharada Gururaj
  • 13,471
  • 1
  • 22
  • 50
En_Tech_Siast
  • 83
  • 1
  • 12
  • `DisplayAlert` is public method in the `Page` class within the `Xamarin.Forms` namespace. Obtain the currently displayed `Page` and then you can call `DisplayAlert` on it. – SushiHangover May 07 '17 at 19:53

3 Answers3

4

You shouldn't do a DisplayAlert from a Task. You should relay a message back to the calling class about a failure or just raise the exception to the calling class. For a task to come back into the UI and raise a message is bad.

Also your use of HttpClient is off. HttpClient is meant to be used as a singleton method. Try and create one per project or modules as a static singleton.

All that being said, try this:

public class ConnexionHelper
{
    public async Task Connexion()
    {
        try
        {
            System.Diagnostics.Debug.WriteLine("trying stuff");
        }
        catch( Exception ex )
        {
            Xamarin.Forms.Page ourPage = App.Current.MainPage.Navigation.NavigationStack.LastOrDefault();
            if (ourPage != null)
            {
                await ourPage.DisplayAlert("eeek", "error has occurrred", "not ok");
            }
        }
    }
Joe Healy
  • 5,769
  • 3
  • 38
  • 56
1

Application.Current.MainPage.DisplayAlert should works

Alessandro Caliaro
  • 5,623
  • 7
  • 27
  • 52
1

Its better to add userdialogs plugin for xamarin. It comes up with different type of alerts,toasts etc for showing messages in the UI. Also it gives a better UI.

You can install the userdialogs from https://www.nuget.org/packages/Acr.UserDialogs/

After installing, you can show alert as follows: UserDialogs.Instance.Alert("","","OK">);

You can also display alert as toasts.

Renjith
  • 682
  • 4
  • 19