Simple question, asked before but no one gave me the right answer so I’m going to be very specific.
I need to read two settings form IsolatedStorage in Windows Phone. The settings are ExitAlert and OrientationLock. I have already set up the settings, they save just fine, I just need to know how to read them from another page. The page the variables are set on is Settings.cs, and I need to read the settings from MainPage.xaml.cs. I also need to know how to only do something if the variable is true or false. I think I’m supposed to use an if-then-else statement, but I just want to double check.
My app is written in C#. This is the code that I’m using to store the settings:
using System;
using System.IO.IsolatedStorage;
using System.Diagnostics;
namespace Google_
{
public class AppSettings
{
IsolatedStorageSettings isolatedStore;
// isolated storage key names
const string ExitWarningKeyName = "ExitWarning";
const string OrientationLockKeyName = "OrientationLock";
// default values
const bool ExitWarningDefault = true;
const bool OrientationLockDefault = false;
public AppSettings()
{
try
{
// Get the settings for this application.
isolatedStore = IsolatedStorageSettings.ApplicationSettings;
}
catch (Exception e)
{
Debug.WriteLine("Exception while using IsolatedStorageSettings: " + e.ToString());
}
}
public bool AddOrUpdateValue(string Key, Object value)
{
bool valueChanged = false;
// If the key exists
if (isolatedStore.Contains(Key))
{
// If the value has changed
if (isolatedStore[Key] != value)
{
// Store the new value
isolatedStore[Key] = value;
valueChanged = true;
}
}
// Otherwise create the key.
else
{
isolatedStore.Add(Key, value);
valueChanged = true;
}
return valueChanged;
}
public valueType GetValueOrDefault<valueType>(string Key, valueType defaultValue)
{
valueType value;
// If the key exists, retrieve the value.
if (isolatedStore.Contains(Key))
{
value = (valueType)isolatedStore[Key];
}
// Otherwise, use the default value.
else
{
value = defaultValue;
}
return value;
}
public void Save()
{
isolatedStore.Save();
}
public bool ExitWarning
{
get
{
return GetValueOrDefault<bool>(ExitWarningKeyName, ExitWarningDefault);
}
set
{
AddOrUpdateValue(ExitWarningKeyName, value);
Save();
}
}
public bool OrientationLock
{
get
{
return GetValueOrDefault<bool>(ExitWarningKeyName, OrientationLockDefault);
}
set
{
AddOrUpdateValue(OrientationLockKeyName, value);
Save();
}
}
}
}
If anything is unclear, just let me know so I can explain further. Thanks.
What I do is add the following to my AppSettings class:
Then you can just use AppSettings.Default from anywhere in your project:
Let me know if you need more information.