从IsolatedStorage设置中检索设置

时间:2014-04-07 21:13:44

标签: c# windows-phone-7

我想在app中添加一项功能来打开或关闭振动或声音。

我创建了类“cUstawienia”(它在应用程序命名空间中),它保存在cUstawienia.cs中 (来自此示例http://msdn.microsoft.com/en-us/library/windowsphone/develop/ff769510(v=vs.105).aspx)。

现在我想在其他源文件(页面)上阅读此设置 我想在mainpage.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;

2 个答案:

答案 0 :(得分:0)

您必须在此应用中创建全局课程

public class AppSettings
    {
        public static bool vibrations;
        public static bool VIBRATIONS
        {
            get
            {
                return vibrations;
            }
            set
            {
                vibrations = value;
            }
        }
    }

在App.xaml.cs的application_activated事件处理程序中添加一行代码以从隔离存储中检索设置

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代码

答案 1 :(得分:0)

查看示例

<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>