Простой вопрос, который задавали раньше, но никто не дал мне правильного ответа, поэтому я собираюсь быть очень конкретным.
Мне нужно прочитать две настройки формы IsolatedStorage
в Windows Phone. Настройки ExitAlert
и OrientationLock
. Я уже настроил настройки, они сохраняются просто отлично, мне просто нужно знать, как их читать с другой страницы. Страница, на которой установлены переменные, Settings.cs
, и мне нужно прочитать настройки с MainPage.xaml.cs
. Мне также нужно знать, как сделать что-то, только если переменная имеет значение true или false. Я думаю, что должен использовать оператор if-then-else, но я просто хочу перепроверить.
Мое приложение написано на C #. Это код, который я использую для хранения настроек:
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();
}
}
}
}
Если что-то неясно, просто дайте мне знать, чтобы я мог объяснить дальше. Спасибо.