简体   繁体   中英

Xamarin.Forms - KeyNotFoundException: The given key was not present in the dictionary C#

I am using Xamarin.Forms and I getting this error:

KeyNotFoundException: The given key was not present in the dictionary

For this line:

bool isLogin = (bool)Application.Current.Properties["isLogin"];

I tried to check to see if the key existed like so:

if(Application.Current.Properties.ContainsKey("isLogin"))
            {
                bool isLogin = (bool)Application.Current.Properties["isLogin"];

                string username = Application.Current.Properties["username"].ToString();

                if (isLogin)
                {
                    Application.Current.MainPage.Navigation.PushAsync(new MenuPage(), true);
                }
            }

But checking the key always returns false even after setting the key and value:

Application.Current.Properties["isLogin"] = true;

Application.Current.Properties will be exists even if you kill the application and restart it again. Just try in this way unplug your device and try to login and check, it will work. While debugging it won't work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM