如何允许项目用户设置为 List类型?
我的应用程序当前使用用户设置系统(通过项目属性)来存储用户特定数据。我使用这些设置而不是写入 App.Config 或从 App.Config 写入数据,因为当 ClickOnce 更新其构建时我需要保留数据。
现在我需要用户能够指定要存储在用户设置中的字符串列表。我已经处理了 C# PropertyGrid 中的设置,并且已经验证,如果我设置 MyPropertyGrid.SelectedObject = new List
不幸的是,当我进入项目首选项 ->设置并单击浏览类型,我无法选择列表或找到任何用于字符串列表的好类型。有人对如何在用户设置中存储字符串列表有任何建议吗?
My app currently uses the user settings system (via project properties) to store user specific data. I use these settings instead of writing to/from App.Config because I need the data to persist when ClickOnce updates their build.
Right now I am needing the user to be able to specify a list of strings to be stored in user settings. I already deal with settings in a C# PropertyGrid, and I have verified that if I set MyPropertyGrid.SelectedObject = new List<string>()
, it gives me a proper editor to edit a list of strings.
Unfortunately, when I go to Project Preferences -> Settings and click Browse for the type, I cannot select List or find any good types to use for a string list. Does anyone have any suggestions for how to store a list of strings in the user settings?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当设计器不允许您选择类型时,您可以手动添加自己的设置。对于某些内置类型和许多您自己的自定义类型,可能会发生这种情况。这是关于SO的类似问题的链接:
为什么我无法从与设置文件相同的项目/程序集中为设置选择自定义类型?
这里有一个答案的链接(在 VB 中) 。网但原则适用):
http://blog.coretech.dk/jgs/add-custom-data-type-struct-to-mysettings-in-vbnet-wpf/
You can add your own settings manually when the designer won't let you choose the types. This can happen with some built-in types and with a lot of your own custom types. Here's a link to a similar question on SO:
Why am I unable to select a custom Type for a setting from the same project/assembly as the settings file?
and here's a link to an answer (it's in VB.NET but the principles apply):
http://blog.coretech.dk/jgs/add-custom-data-type-structure-to-mysettings-in-vbnet-wpf/
嗯...我可以在类型列表中看到
System.Collections.Specialized.StringCollection
。你为什么不直接用这个呢?您可以在
StringCollection
和List
之间使用以下转换:Well... I can see
System.Collections.Specialized.StringCollection
in the list of types. Why don't you just use this?You can use the following conversions between
StringCollection
andList<string>
:您无法由设计者执行此操作,但您可以手动编辑设置文件。
问题是,VS 允许在设置文件中默认序列化 int[] 类型 - 它只是不允许您默认选择它。因此,创建一个具有所需名称的设置(例如 SomeTestSetting)并将其设置为任何类型(例如默认为字符串)。保存更改。
现在转到您的项目文件夹并使用文本编辑器(例如记事本)打开“Properties\Settings.settings”文件,或者您可以通过在解决方案资源管理器中右键单击“->属性->设置”在VS中打开它.settings”,选择“打开方式...”,然后选择“XML 编辑器”或“源代码(文本)编辑器”。在打开的 xml 设置中找到您的设置(如下所示):
将“Type”参数从 System.String 更改为 List。现在该部分将如下所示:
You are not able to do it by the designer, but you can manual edit the settings file.
The thing is, that VS allows to serialize int[] type by default in the settings file - it just doesn't allow you to select it by default. So, create a setting with desired name (e.g. SomeTestSetting) and make it of any type (e.g. string by default). Save the changes.
Now go to your project folder and open the "Properties\Settings.settings" file with text editor (Notepad, for example) Or you can open it in VS by right-clicking in Solution Explorer on " -> Properties -> Settings.settings", select "Open With..." and then choose either "XML Editor" or "Source Code (Text) Editor". In the opened xml settings find your setting (it will look like this):
Change the "Type" param from System.String to List. Now this section will look like this: