【问题标题】:How to save selected index of a combobox in UWP and display it back when the user reloads the page?如何在 UWP 中保存组合框的选定索引并在用户重新加载页面时将其显示回来?
【发布时间】:2016-11-13 17:02:20
【问题描述】:

我的 UWP 应用程序中有一个 ComboBox 控件,当用户选择一个选项时,我需要保存它的选定索引!然后,我需要将此索引保存在本地设置中,当用户返回此页面时,我希望ComboBox 将此保存的索引作为选定索引。我的设置页面需要这个功能!谁能帮我?

这是我的代码:

private void fuelTypeSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var localSettings = ApplicationData.Current.LocalSettings;

    try
    {
        if(localSettings.Values.ContainsKey("selectedIndex"))
        {
            int index = (int)localSettings.Values["selectedIndex"];
            fuelTypeSelector.SelectedIndex = index;

            //update the saved index

            if(fuelTypeSelector.SelectedIndex!=index)
            {
                localSettings.Values["selectedIndex"] =
                    fuelTypeSelector.SelectedIndex;
            }
        }
        else
        {
            // index does not exist
            localSettings.Values.Add("selectedIndex",
                fuelTypeSelector.SelectedIndex);
        }
    }
    catch(Exception ex)
    {

    }
}

【问题讨论】:

    标签: c# combobox uwp selectedindex


    【解决方案1】:

    要获取ComboBox的选中项,可以处理它的SelectionChanged event,这里举例:

    <ComboBox SelectionChanged="ComboBox_SelectionChanged">
        <ComboBoxItem>Item 1</ComboBoxItem>
        <ComboBoxItem>Item 2</ComboBoxItem>
        <ComboBoxItem>Item 3</ComboBoxItem>
        <ComboBoxItem>Item 4</ComboBoxItem>
        <ComboBoxItem>Item 5</ComboBoxItem>
    </ComboBox>
    

    后面的代码:

    private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        //you can get the selected item like this:
        var combo = sender as ComboBox;
        var selecteditem = combo.SelectedItem;
    
        //or, since ComboBox DOESN'T support multiple selection, you can get the item like:
        var selecteditems = e.AddedItems.FirstOrDefault();
    }
    

    或者如果你只需要这个项目的索引,你可以使用第一种方法,把代码改成这样:var selectedindex = combo.SelectedIndex;。当然,我们也可以通过data binding 将项目添加到ComboBox 的集合中。

    通过保存选中的项目,我个人认为最好在你的应用处于挂起阶段时保存本地设置,并在启动阶段读取设置数据。查看UWP app的生命周期,官方文档Launching, resuming, and background tasks会帮到你。这意味着您必须在应用运行时保存页面状态,为此,您可以缓存您的设置页面,有关页面状态的更多信息,您可以参考我在UWP page state manage 中的回答。

    关于保存和检索设置部分,这里是官方文档:Store and retrieve settings and other app data,这个文档中有一些示例代码。

    最后,由于您是 UWP 应用开发的新手,您可以参考How-to articles for UWP apps on Windows 10 开始。 GitHub 上有很多 official UWP samples 也可能有所帮助。

    【讨论】:

    • 我正在尝试保存 selectedIndex,因此当用户返回此页面时,我希望组合框将其之前选择的 selectedIndex 作为 selectedIndex。我怎样才能做到这一点?如果我在每个 selection_changed 事件中保存 selectedIndex 并尝试在组合框中将其设置为默认值,组合框总是采用相同的索引!我做错了什么?我的逻辑正确吗?
    • @TasosTheodosiou,您可以将所选项目与保存的项目进行比较,以确定这是否是新设置,如果您不比较它们,当您在页面打开后设置最后一个设置时, selection_changed 事件只会引发一次,为什么您认为组合框总是采用相同的索引?仅当最后一个项目与新项目不同或最后一个选定项目为空时,才会触发此事件。正如我所说,在运行时,您可以缓存页面的内容。
    • @TasosTheodosiou,我知道你是UWP的新手,所以我花了一些时间写了一个settings demo,你可以看看。
    • 是否要保存页面状态并在用户返回页面时重新加载所有控件的状态?
    猜你喜欢
    • 2013-07-13
    • 2017-09-17
    • 2021-11-22
    • 2020-03-11
    • 2019-04-30
    • 2015-03-28
    • 1970-01-01
    • 2013-02-12
    • 1970-01-01
    相关资源
    最近更新 更多