2013-04-09 82 views
1

我创建了一个按钮来创建多个复选框。点击wp7。在我用它的代码下面。为wp7保存多个复选框的状态

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
    <TextBox x:Name="txtNewTask" HorizontalAlignment="Left" Height="72" TextWrapping="Wrap" VerticalAlignment="Top" Width="328"/> 
    <Button x:Name="btnAdd" Content="add" HorizontalAlignment="Left" Margin="328,0,0,0" VerticalAlignment="Top" Width="123" Click="btnAdd_Click"/> 
    <ListBox x:Name="lbToDoList" Margin="0,72,0,0"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
     <CheckBox Click="CheckBox_Click" Background="{x:Null}"> 
      <StackPanel Orientation="Horizontal"> 
      <TextBlock Text="{Binding}" Name="tbkTextName" VerticalAlignment="Center"  Margin="5,0,5,0" /> 
      </StackPanel> 
     </CheckBox> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
    </ListBox> 
</Grid> 

现在,当我退出并重新打开我的应用程序时,我注意到的复选框的选中是(默认状态)和它的状态不会被保存。你能帮我保存多个复选框的值或状态吗?

有人可以帮我保存多个复选框的状态。在此先感谢您的帮助!

Image of the created checkboxes

回答

0

使用IsolatedStorage.ApplicationSettings

以下是访问的应用程序设置

/// Get the current value of the setting, or if it is not found, set the 
    /// setting to the default setting. 
    protected valueType GetValueOrDefault<valueType>(string Key, valueType defaultValue) 
    { 
     valueType value; 
     object storedValue = null; 

     try 
     { 
      if (_isolatedStore.TryGetValue(Key, out storedValue)) 
      { 
       value = (valueType)(_isolatedStore[Key] ?? defaultValue); 
      } 
      else 
      { 
       //the key was not found 
       value = defaultValue; 
      } 
     } 
     catch (Exception ex) 
     { 
      value = defaultValue; 
      Logger.Error(ex, "Exception while getting IsolatedStorageSettings: "); 
     } 


     return value; 
    } 

    protected bool AddOrUpdateValue(string Key, Object value) 
    { 
     bool valueChanged = false; 
     object storedValue = null; 

     try 
     { 
      if (_isolatedStore.TryGetValue(Key, out storedValue)) 
      { 
       if (storedValue != value) 
       { 
        _isolatedStore[Key] = value; 
        valueChanged = true; 
       } 
      } 
      else 
      { 
       //the key was not found 
       _isolatedStore.Add(Key, value); 
      } 
     } 
     catch (Exception ex) 
     { 
      Logger.Error(ex, "Exception while adding IsolatedStorageSettings."); 
     } 

     return valueChanged; 
    } 

然后你就可以创建由IsolatedStorage支持的一些设置类或视图模型属性两个辅助方法喜欢这个。

string CheckBoxValueKeyName = "checkbox_value"; 
    bool CheckBoxValueDefault = false; 

    public bool CheckBoxValue 
    { 
     get 
     { 
      return GetValueOrDefault<bool>(CheckBoxValueKeyName, CheckBoxValueDefault); 
     } 
     set 
     { 
      AddOrUpdateValue(CheckBoxValueKeyName, value); 
     } 
    } 

如果您不想立即应用检查框独立存储的变化,WP7 Tombstone Helper是应用程序的墓碑后,以保存控件的状态的快捷方式。所以,是的,对于关闭应用程序后的持久性存储,请使用独立存储。

+0

我试过wp7墓碑助手,但没有工作。我的应用中没有任何复杂的场景。只需要保存这些值。 – 2013-04-10 10:56:07

+0

当你的应用被墓碑化时,墓碑助手会帮助你;你正在寻找持久存储。隔离存储是一条可行的路线,布尔将很好地适用于由@ Rico-E建议的为应用程序设置保留的IsolatedStorage部分。这是一个简单数据类型的字典。但是,他的示例不检查空值。使用TryGetValue并在空情况下返回默认值。该页面链接到jimpanzer的文章中提供了示例,其中包含http://www.geekchamp.com/tips/all-about-wp7-isolated-storage-store-data-in-isolatedstoragesettings。 – kindasimple 2013-04-10 19:30:32

0

您需要将数据保存到应用程序未运行时。对于那个职员我使用IsolatedStorage。你可以保存任何东西,你需要什么。我发现伟大的tutorial,它是如何实现的。希望这是帮助。

0

我认为最好的办法是,当它改变时立即保存复选框的值。 要做到这一点,你可以做到以下几点:

假设在myPage.xaml复选框的样子:

<CheckBox Content="{Binding Title}" Name="myAutoSavingCheckBox" Click="myAutoSavingCheckBox_Click"/> 

在myPage.xaml.cs你必须定义如下方法:

private void myAutoSavingCheckBox_Click(object sender, RoutedEventArgs e) 
    { 
     App.ViewModel.MyProperty = myAutoSavingCheckBox.IsChecked; 
    } 

App.ViewModel在App.xaml.cs声明:

public partial class App : Application 
{ 
... 
    public static MainViewModel ViewModel 
    { 
     get 
     { 
      // Erstellung des Ansichtsmodells verzögern bis erforderlich 
      if (viewModel == null) 
       viewModel = new MainViewModel(); 

      return viewModel; 
     } 
    } 
... 
} 

现在您在MainViewModel.cs中定义您的属性和保存功能,如下所示:

public class MainViewModel 
{ 
    private bool? myProperty; 
    public bool? MyProperty 
    { 
     get 
     { 
      return myProperty; 
     } 
     set 
     { 
      if (value != myProperty) 
      { 
       myProperty = value; 
       SaveSetting("MyProperty", myProperty); 
      } 
     } 
    } 

    public void SaveSettings(string whatShallBeSavedKey, object whatShallBeSavedValue) 
    { 
     if (IsolatedStorageSettings.ApplicationSettings.Contains("whatShallBeSavedKey")) 
      IsolatedStorageSettings.ApplicationSettings["whatShallBeSavedKey"] = whatShallBeSavedValue; 
     else 
      IsolatedStorageSettings.ApplicationSettings.Add("whatShallBeSavedKey", whatShallBeSavedValue); 
    } 
} 
+0

我已经试过这段代码,它只保存一个复选框(myAutoSavingCheckBox)。但是我想保存已经创建的多个复选框的值。看到我编辑的帖子。 – 2013-04-24 14:55:09