从隔离存储设置中重新修订设置
本文关键字:设置 新修订 存储 隔离 | 更新日期: 2023-09-27 17:57:08
我想添加一个功能来打开或关闭应用程序中的振动或声音。
我创建了类"cUstawienia"(它在应用程序命名空间中),它保存在cUstawienia中.cs
(来自此示例 http://msdn.microsoft.com/en-us/library/windowsphone/develop/ff769510(v=vs.105).aspx)。
现在我想在其他源文件(页面
)上读取此设置我想在主页上读取值.cs但我不知道怎么做。
我试图从这里激发灵感 http://hotcomputerworks.wordpress.com/2011/08/07/save-user-application-specific-settings-in-windows-phone-7/
我在我的代码中写了这样的东西:
我的 cUstawienia 代码:
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.Diagnostics;
using System.Collections.Generic;
namespace ClicknSave_v2
{
//klasa ustawień
public class cUstawienia
{
// Our settings
IsolatedStorageSettings settings;
// The key names of our settings
const string KluczUstDzwieku = "UstDzwieku";
const string KluczUstWibracji = "UstWibracji";
// The default value of our settings
const bool DomyslneUstawienieDziweku = false;
const bool DomyslneUstawienieWibracji = false;
/// <summary>
/// Constructor that gets the application settings.
/// </summary>
public cUstawienia()
{
try
{
settings = IsolatedStorageSettings.ApplicationSettings;
}
catch (System.IO.IsolatedStorage.IsolatedStorageException e)
{
// handle exception
}
}
/// <summary>
/// Update a setting value for our application. If the setting does not
/// exist, then add the setting.
/// </summary>
/// <param name="Key"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool AddOrUpdateValue(string Key, Object value)
{
bool valueChanged = false;
// If the key exists
if (settings.Contains(Key))
{
// If the value has changed
if (settings[Key] != value)
{
// Store the new value
settings[Key] = value;
valueChanged = true;
}
}
// Otherwise create the key.
else
{
settings.Add(Key, value);
valueChanged = true;
}
return valueChanged;
}
/// <summary>
/// Get the current value of the setting, or if it is not found, set the
/// setting to the default setting.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="Key"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public T GetValueOrDefault<T>(string Key, T defaultValue)
{
T value;
// If the key exists, retrieve the value.
if (settings.Contains(Key))
{
value = (T)settings[Key];
}
// Otherwise, use the default value.
else
{
value = defaultValue;
}
return value;
}
/// <summary>
/// Save the settings.
/// </summary>
public void Save()
{
settings.Save();
}
//Ustawienia dźwięku
public bool UstawieniaDziwieku
{
get
{
return GetValueOrDefault<bool>(KluczUstDzwieku, DomyslneUstawienieDziweku);
}
set
{
if (AddOrUpdateValue(KluczUstDzwieku, value))
{
Save();
}
}
}
public bool UstawieniaWibracji
{
get
{
return GetValueOrDefault<bool>(KluczUstWibracji, DomyslneUstawienieWibracji);
}
set
{
if (AddOrUpdateValue(KluczUstWibracji, value))
{
Save();
}
}
}
}
}
我试图在其他源文件(主页)上读取设置的代码女巫:
ClicknSave_v2.cUstawienia = new ClicknSave_v2.cUstawienia();
cUstawienia.UstawieniaDziwieku = result.Dzw;
cUstawienia.UstawieniaWibracji = result.Wib;
您必须像这样在应用程序中创建一个全局类
public class AppSettings
{
public static bool vibrations;
public static bool VIBRATIONS
{
get
{
return vibrations;
}
set
{
vibrations = value;
}
}
}
在 App.xaml 的application_activated事件处理程序中.cs添加一行代码以从独立存储中检索设置
if (IsolatedStorageSettings.ApplicationSettings.Contains("vibration"))
{
AppSettings.vibrations = (bool)IsolatedStorageSettings.ApplicationSettings["vibration"];
}
在 App.xaml 中,将本地资源定义为
<local:AppSettings x:Name="ApplicationSettings"/>
现在,您可以使用它来绑定应用程序中的任何位置。例如,如果你有一个拨动开关,请使用类似的东西
IsChecked="{Binding Path=VIBRATIONS, Source={StaticResource ApplicationSettings}}"
在切换开关 XAML 代码中
查看示例
<phone:PhoneApplicationPage
x:Class="YourClasss.yourPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:controls="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:xtraControls="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
xmlns:ClicknSave_v2="clr-namespace:ClicknSave_v2"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="800"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="False">
<phone:PhoneApplicationPage.Resources>
<ClicknSave_v2:cUstawienia x:Key="cUstawienia"/>
<phone:PhoneApplicationPage.Resources>
<xtraControls:ToggleSwitch IsChecked="{Binding UstawieniaWibracji, Source={StaticResources cUstawienia}, Mode=TwoWay}" />
</phone:PhoneApplicationPage>