Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the right way to verify if local Storage variable exists? - XAMARIN

Hi I'm doing an app that has a login, and it has the option to keep you login even if you turn off the app.

What is the problem? This is what I'm doing this in App.cs:

  var statusLog = Application.Current.Properties["logStatus"].ToString();
        if (statusLog == "F")
        {
            Application.Current.MainPage = new NavigationPage(new LoginPage());

        }
        else
        {
            var userStore = (Application.Current.Properties["user"].ToString());
            Task.Run(() =>  lp.GetTokenLogin()).Wait();

            MainPage = new NavigationPage(new ConfirmarViatura(userStore));




        }

It works fine, but their is one situation that it do not work, that is if I run the app for the first time in the device, it gives me a exception that the local variable "logStatus" do not exists. I understand it do not exists, but how can i do this verification? I can't do this :

          if (Application.Current.Properties["logStatus"].Equals(null))
        {
            Application.Current.Properties["logStatus"] = "F";

        }

This do not work because the variable dosen't even exists. Any ideas?

like image 470
Tiago dias Avatar asked Sep 15 '25 21:09

Tiago dias


1 Answers

You can check it this way:

if (Application.Current.Properties.ContainsKey("logStatus"))
{
    var statusLog = Application.Current.Properties["logStatus"] as string;
    // rest of your code
}
like image 112
Luis Beltran Avatar answered Sep 19 '25 14:09

Luis Beltran