4

In a Xamarin.Forms app I'm using Application.Current.Properties to save settings. When my LoginPage loads I want to see if settings exist but I'm getting an exception saying Application.Current is Null. This is the (sanitized) code, which is in the LoginPage:

 private bool AuthTokenExists()
        {
            // The following line throws the exception
            if (Application.Current.Properties.ContainsKey("First") && Application.Current.Properties.ContainsKey("Second"))
            {
                if (Application.Current.Properties["First"] as string != null &&
                    Application.Current.Properties["Second"] as string != null)
                return true;
            }
            else
            {
            return false;
            }
        }

I've read you can't call Application.Current.Properties in the App constructor, but this is in a ContentPage. Any thoughts why I'm getting the exception? Thanks in advance!

2
  • I haven't tried Xamarin, but I'd suggest you try debugging and see which one out of Current and Properties is null. It might help. Commented Feb 26, 2015 at 2:22
  • I appreciate the feedback. Signs are now pointing to a bug in Xamarin, so I'm pursuing a different path. Commented Feb 26, 2015 at 16:40

2 Answers 2

4

It's null until you call LoadApplication () from your platform projects.

Just wirte LoadApplication( new App() ); in MainActivity.cs and your App class in PCL or Shaired must be a Applciation not a content Page.

Then you can use your exsisting code any where in application (including content page as you mention) , It will not give Null exception.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. At this point, the App() has loaded and has loaded LoginPage , which is unable to access the settings. This may be fixed in latest version, but I've already worked around it.
Let me clear what I am getting from your comment, i.e. by calling APP() , you solved Null exception but not able to acess settings from LoginPage ? Are you taking about same ?
Sorry, no. At the time I worked around the Null problem by storing the values in a local db. I couldn't spend too much time on it, but it seemed the version of Forms I was using couldn't access Properties until the first content page was visible. So even though I had loaded the App() and the App was loading LoginPage, the Null was still thrown. But on the next page after LoginPage (eg after the UI was initialized) everything worked fine. As I said, this all may be fixed in the newest version of Forms...
Ok, got it now..!! Thanks for discussing it.
1

Right now, there is a (one more :-) bug in the Xamarin-VS extension, so that the application-object is not visible in the debugger. Therefore, I have filled a bug in bugzilla. But at RunTime (without debugger) it should run (at least with the newer XF-Versions). Additionally there are some problems with some devices (e.g. my Android tablet SM-T900), so that the Properties are not stored properly (sounds good:-) In one of the last XF-Versions, there is now possible, to store the properties explicitly (new method).

So, I suggest you, to:
- Update to the version that allow the explicit store (if not already in use)
- Set default values by app-start
- Save it with the new method
- Deploy to device
- Retest, if it works then (e.g. show a DisplayAlert with the Property-value)

If not, you should fill a bug in BugZilla.

Hope this helps...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.