Reputation: 438
Working on my first Xamarin Forms app. I put this in my App's OnStart() method:
Application.Current.Properties["Email"] = "[email protected]";
That should set the value as soon as the Application starts, right? Then I created a Login content page with an EmailAddress entry. In the page initialization, I've got this:
if (Application.Current.Properties.ContainsKey("Email"))
{
EmailAddress.Text = Application.Current.Properties["Email"] as string;
}
When I run the app, the EmailAddress Entry element is not populated.
What am I doing wrong?
Upvotes: 0
Views: 34
Reputation: 5370
OnStart command is called after App constructor and you probably creating login page in App constructor and checking in login page constructor but OnStart is not called yet. Set it before you created login/main page
Application.Current.Properties["Email"] = "[email protected]";
var navPage = new MainPage();
Upvotes: 1
Reputation: 5768
It should work.
Here is the Documentation.
You can try to use SavePropertiesAsync
The Properties dictionary is saved to the device automatically. Data added to the dictionary will be available when the application returns from the background or even after it is restarted.
Xamarin.Forms 1.4 introduced an additional method on the Application class - SavePropertiesAsync() - which can be called to proactively persist the Properties dictionary. This is to allow you to save properties after important updates rather than risk them not getting serialized out due to a crash or being killed by the OS.
Upvotes: 1