引用XAML中用于绑定的共享类

时间:2015-02-06 20:35:32

标签: c# xaml windows-phone-8.1 windows-8.1

我正在创建一个通用应用程序,我很难尝试将我的设置控件绑定到共享的Settings类。如何引用我的Settings类以便在我的控件上设置绑定源?

XAML代码:

<Page
x:Class="DownloaderUniversal.SettingsPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:DownloaderUniversal"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<Grid x:Name="LayoutRoot">
        <StackPanel>
        <!-- Content Section 1-->
            <StackPanel>

                <!-- Section 1 header -->
                <TextBlock Style="{StaticResource TitleTextBlockStyle}" Text="Save Location" />

                <!-- Section 1 body -->
                <RadioButton x:Name="music" Content="Music Library" IsChecked="{Binding Source={Static Resource SharedSettings}, Path=MusicFolderSetting, Mode=TwoWay}" Checked="Music_Checked"/>
                <RadioButton x:Name="sdCard" Content="SD Card" IsChecked="{Binding Source={Static Resource SharedSettings}, Path=CardFolderSetting, Mode=TwoWay}" Checked="Card_Checked"/>

            </StackPanel>

和我的设置类:

class Settings
{
    // Our local storage settings
    ApplicationDataContainer localSettings;

    // The local storage key names of our settings
    const string musicFolderKeyName = "musicRadioButton";
    const string cardFolderKeyName = "cardRadioButton";

    // The default value of our settings
    const bool musicFolderDefault = true;
    const bool cardFolderDefault = false;

    /// <summary>
    /// Constructor that gets the application settings.
    /// </summary>
    public Settings()
    {
        try
        {
            localSettings = ApplicationData.Current.LocalSettings;
            // Get the settings for this application.
            //isolatedStore = IsolatedStorageSettings.ApplicationSettings;

        }
        catch
        {
            throw;
        }
    }

    /// <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 (localSettings.Contains(Key))
        if (localSettings.Values.ContainsKey(Key))
        {
            // If the value has changed
            if (localSettings.Values[Key] != value)
            {
                // Store the new value
                localSettings.Values[Key] = value;
                valueChanged = true;
            }
        }
        // Otherwise create the key.
        else
        {
            localSettings.Values.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="valueType"></typeparam>
    /// <param name="Key"></param>
    /// <param name="defaultValue"></param>
    /// <returns></returns>
    public valueType GetValueOrDefault<valueType>(string Key, valueType defaultValue)
    {
        valueType value;

        // If the key exists, retrieve the value.
        if (localSettings.Values.ContainsKey(Key))
        {
            value = (valueType)localSettings.Values[Key];
        }
        // Otherwise, use the default value.
        else
        {
            value = defaultValue;
        }

        return value;
    }

    /// <summary>
    /// Property to get and set a Directory Folder Setting Key.
    /// </summary>
    public bool MusicFolderSetting
    {
        get
        {
            return GetValueOrDefault<bool>(musicFolderKeyName, musicFolderDefault);
        }
        set
        {
            AddOrUpdateValue(musicFolderKeyName, value);
        }
    }

    /// <summary>
    /// Property to get and set a Directory Folder Setting Key.
    /// </summary>
    public bool CardFolderSetting
    {
        get
        {
            return GetValueOrDefault<bool>(cardFolderKeyName, cardFolderDefault);
        }
        set
        {
            AddOrUpdateValue(cardFolderKeyName, value);
        }
    }

由于

1 个答案:

答案 0 :(得分:0)

这取决于您创建共享实例的位置。

一种可能的解决方案:在App.xaml中将其作为资源创建。

的App.xaml:

<App.Resources>
    <local:Settings x:Key="SharedSettings" />
</App.Resources>

Page.xaml:

<Grid DataContext="{Binding Source={StaticResource SharedSettings}}">...

这是最简单的解决方案。大多数情况下,您不会直接在应用程序资源中创建实例,而是有一个服务定位器可以为您处理它。