我需要保存用户在运行时更改的组合框值,并在下次加载页面时将其显示为组合框默认值。我在为我的 UWP 应用程序问这个问题。我在 Settings.xaml 中定义了组合框,如下所示:
<ComboBox Name="CLengthCombo" SelectionChanged="ComboBox_SelectionChanged">
<ComboBoxItem Content="24"/>
<ComboBoxItem Content="25"/>
<ComboBoxItem Content="26" IsSelected="True"/>
<ComboBoxItem Content="27"/>
</ComboBox>
在我的 Settings.xaml.cs 中,我定义了一个名为“localSettings_CycleLength”的全局变量,以保存更改后的组合框值,使其在应用程序启动之间保持不变:
Windows.Storage.ApplicationDataContainer localSettings_CycleLength = Windows.Storage.ApplicationData.Current.LocalSettings;
然后我的 Settings.xaml.cs 中有以下代码:
public Settings()
{
this.InitializeComponent();
if (localSettings_CycleLength.Values["CycleLength"] != null)
{
this.CLengthCombo.SelectedItem = localSettings_CycleLength.Values["CycleLength"];
}
}
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var comboBoxItem = e.AddedItems[0] as ComboBoxItem;
if (comboBoxItem == null) return;
comboBoxItem.IsSelected = true;
var content = comboBoxItem.Content as string;
if (content != null)
{
localSettings_CycleLength.Values["CycleLength"] = content;
}
}
现在,上面的代码没有做我需要的,我不知道为什么。你能帮帮我吗?提前谢谢!