如何将字符串集合保存到c#wpf中的应用程序设置? [英] How do I save a string collection to application settings in c# wpf?

查看:64
本文介绍了如何将字符串集合保存到c#wpf中的应用程序设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在努力解决这个问题。我想将树视图状态默认保存为应用程序设置。



我有这个代码将所有isChecked项目保存到列表中。

而且我还添加了带有字符串集合类型的CheckedPaths,但是当我试图将它保存到settings.default时,它不会保存,而是会产生一个错误保存到字符串集合。



(PS。很抱歉这个noob问题,但我还是初学者。谢谢:))



I'm struggling with this problem right now. I wanted to save the treeview state default to an application settings.

I have this code that saves all isChecked items to the list.
And i've also added "CheckedPaths" with string collection type, but when
I've tried to save it to the settings.default, it won't save, instead it gives an error that cannot save to string collection.

(PS. Sorry for this noob question but I'm still a beginner for this. Thanks :) )

List<string> hashs = new List<string>();

        public bool? IsChecked
        {
            get { return _isChecked; }
            set
            {
                _isChecked = value;
                SetIsChecked(_isChecked, true, false);
                if (_isChecked == true)
                {
                    hashs.Add(this.FullPathName);

                }
                else
                {
                    hashs.Remove(this.FullPathName);
                }
            }
}

推荐答案

StringCollection是一种特定类型。您无法从List< t>转换它。但是,您实际上不必使用StringCollection。这只是在设计师中。



你有两个选择

(1)将你的字符串保存到StringCollection而不是

(2)在Settings.Designer.cs中更改自动生成的代码



我推荐第一个,因为更改自动生成的代码可能会有问题。我知道的一个问题是,您需要在设计师添加设置时重复更改。



第二个选项很容易实现。打开Settings.Designer.cs文件,将System.Collections.Specialized.StringCollection更改为System.Collections.Generic.List< string>。就是这样,那时你可以编写正常的代码来操作设置(不管你是不是要使用设计师的价值)。



StringCollection is a specific type. You cannot convert from List<t> to it. However, you don't really have to use StringCollection. That's just in the designer.

You have two choices
(1) Save your strings to StringCollection instead
(2) Change the auto-generated code in Settings.Designer.cs

I recommend the first, because changing auto-generated code can be problematic. The one issue I know is that you'll need to repeat the change anytime you add a setting through the designer.

The second option is simple to implement. Open the Settings.Designer.cs file and change the System.Collections.Specialized.StringCollection to System.Collections.Generic.List<string>. That's it, at that point you can write your normal code to manipulate the setting (it's not like you're going to use the designer anyway for the value).

if (Settings.Default.TestSetting == null) {
   // create collection
   Settings.Default.TestSetting = myValues;
   Settings.Default.Save();
}

// do whatever


感谢您的帮助!想通了。





Thanks for the help! figured it out.


System.Collections.Specialized.StringCollection _checkedPaths = CheckedList.CheckedLists.CheckPaths;
public bool? IsChecked
{
    get { return _isChecked; }
    set
    {
        _isChecked = value;
        SetIsChecked(_isChecked, true, false);
        if (_isChecked == true)
        {
                if (UserSettings.Default.CheckedNodes == null)
                {
                    UserSettings.Default.CheckedNodes = new System.Collections.Specialized.StringCollection();
                }

                if (CheckIfPathExists(this.FullPathName) == false)
                {
                    UserSettings.Default.CheckedNodes.Add(this.FullPathName);
                    UserSettings.Default.Save();
                }
        }
        else
        {
            UserSettings.Default.CheckedNodes.Remove(this.FullPathName);
            UserSettings.Default.Save();
        }
    }
}
}





在我的视图模型中,我将此添加到我之前构建树视图的代码中。





and in my view model, I added this to my previous code that builds thee treeview.

foreach (string _fileItem in UserSettings.Default.CheckedNodes)
                        {
                            if (UserSettings.Default.CheckedNodes.Contains(item.FullPathName))
                            {
                                item.IsChecked = true;
                            }
                        }


这篇关于如何将字符串集合保存到c#wpf中的应用程序设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆