[英]Retreving settings from IsolatedStorageSettings
I would like to add a feature to switch on or off vibrations or sounds in app. 我想添加一个功能来打开或关闭应用程序中的振动或声音。
I created class "cUstawienia" (its in app namespace) it's saved in cUstawienia.cs 我创建了类“ cUstawienia”(在应用程序名称空间中),并将其保存在cUstawienia.cs中
(from this sample http://msdn.microsoft.com/en-us/library/windowsphone/develop/ff769510(v=vs.105).aspx ). (来自此示例http://msdn.microsoft.com/zh-cn/library/windowsphone/develop/ff769510(v=vs.105).aspx )。
Now I would like to read this settings on other source files (pages) 现在,我想在其他源文件(页面)上阅读此设置
I would like to read values in mainpage.cs but I dont know how. 我想读取mainpage.cs中的值,但我不知道如何。
I tried to inspire from here http://hotcomputerworks.wordpress.com/2011/08/07/save-user-application-specific-settings-in-windows-phone-7/ 我试图从这里启发http://hotcomputerworks.wordpress.com/2011/08/07/save-user-application-specific-settings-in-windows-phone-7/
I wrote to my code something like this: 我在代码中写了这样的内容:
My cUstawienia code: 我的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();
}
}
}
}
}
And my piece of code witch i tried to read setting on other source file (mainpage): 我的一段代码巫婆试图读取其他源文件(主页)上的设置:
ClicknSave_v2.cUstawienia = new ClicknSave_v2.cUstawienia();
cUstawienia.UstawieniaDziwieku = result.Dzw;
cUstawienia.UstawieniaWibracji = result.Wib;
You will have to make a global class in your app like this 您必须像这样在应用程序中进行全局类
public class AppSettings
{
public static bool vibrations;
public static bool VIBRATIONS
{
get
{
return vibrations;
}
set
{
vibrations = value;
}
}
}
In the application_activated event handler of App.xaml.cs add a line of code to retrieve the settings from isolated storage 在App.xaml.cs的application_activated事件处理程序中,添加一行代码以从隔离存储中检索设置
if (IsolatedStorageSettings.ApplicationSettings.Contains("vibration"))
{
AppSettings.vibrations = (bool)IsolatedStorageSettings.ApplicationSettings["vibration"];
}
In App.xaml define a local resource as 在App.xaml中,将本地资源定义为
<local:AppSettings x:Name="ApplicationSettings"/>
Now you could use it for binding it anywhere in the application. 现在,您可以将其用于绑定到应用程序中的任何位置。 For example if you have a toggle switch use something like 例如,如果您有一个拨动开关,请使用类似
IsChecked="{Binding Path=VIBRATIONS, Source={StaticResource ApplicationSettings}}"
in the toggle switch XAML code 在切换开关XAML代码中
View example 查看范例
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.