2015-02-06 4 views
0

유니버설 앱을 만들고 있는데 설정 컨트롤을 공유 설정 클래스에 바인딩하는 데 어려움을 겪고 있습니다. 내 컨트롤에 바인딩 소스를 설정하려면 어떻게 설정 클래스를 참조해야합니까?참조 XAML의 바인딩 용 공유 클래스

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); 
     } 
    } 

감사

당신이 당신의 공유 인스턴스를 만들 위치에 따라 달라집니다

답변

0

.

하나 가능한 해결책 : App.xaml에서 리소스로 만듭니다.

App.xaml :

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

Page.xaml는 :

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

이 가장 간단한 솔루션입니다. 주로 앱 리소스에서 인스턴스를 직접 생성하지는 않지만,이를 처리하는 서비스 로케이터가 있어야합니다.

+0

응답 주셔서 감사합니다. 당신이 제안한 것을했는데 라디오 버튼의 소스 바인딩을'SharedSettings'으로 설정했지만 아직도 제대로 바인딩되지 않았습니다. 첫 번째 라디오 버튼은 내가 페이지를 탐색 할 때 확인 된 것으로 표시되어야합니다. 내 xaml 코드를 업데이트했습니다. – nos9

+0

죄송합니다. 그냥 대답에 bindng 표현을 업데이 트 ... –

+0

나는 그것을 알아 냈다. 'Source = {Static Resource SharedSettings} '를 제어 항목에 직접 추가해야했습니다. 도와 주셔서 감사합니다. – nos9