1

我创建了一个按钮来创建多个复选框。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>

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

谁能帮我保存多个复选框的状态。在此先感谢您的帮助!

创建的复选框的图像

4

3 回答 3

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是一种在应用程序墓碑后保持控件状态的快速方法。所以,是的,对于应用程序关闭后的持久存储,请使用独立存储。

于 2013-04-09T19:57:12.657 回答
0

您需要将数据保存到应用程序未运行的时间。对于我使用的那个员工IsolatedStorage。你可以保存任何你需要的东西。我找到了很棒的教程,它是如何实现的。希望有帮助。

于 2013-04-10T11:17:07.970 回答
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.ViewModelApp.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);
    }
}
于 2013-04-10T11:26:00.520 回答