gpt4 book ai didi

c# - DialogPage - 字符串数组未保留

转载 作者:太空狗 更新时间:2023-10-29 18:24:56 25 4
gpt4 key购买 nike

我正在为 visual studio 开发一个扩展。

我有一个选项页面:

public class GeneralOptionsPage : DialogPage
{
[Category("General")]
[DisplayName("Foos")]
[Description("Bla Foo Bla")]
public string[] Foos { get; set; }


[Category("General")]
[DisplayName("Bar")]
[Description("Bar Foo Bar")]
public string Bar { get; set; }
}

Bar 属性完美运行并持久化。

Foos 属性也有效(它甚至在选项页面中为您提供了一个漂亮的弹出窗口,您可以在其中每行输入一个字符串),这意味着我可以设置它并在我的扩展名,但未写入注册表/存储。当我关闭 VS 并再次打开它时,它总是空的。

引自 MSDN :

The default implementation of DialogPage supports properties that have appropriate converters or that are structures or arrays that can be expanded into properties that have appropriate converters. For a list of converters, see the System.ComponentModel namespace. The Visual Studio Extensibility Samples manages int, string, and System.Drawing.Size properties.

据我所知,我正在使用 System.ComponentModel 命名空间中的有效组件。

那我做错了什么?我必须以某种不同的方式对待数组吗?

最佳答案

您需要为您的 Foos 属性实现和关联一个自定义 TypeConverter。

没有涵盖这种情况的常用转换器,因为当您将字符串数组转换为字符串时,您必须有某种定界符,以便您可以从字符串重构数组。这将根据各个程序员的应用程序而有所不同。因此需要自定义 TypeConverter。

因此您必须从 System.ComponentModel.TypeConverter 派生一个新类,然后将其与您的 Foos 属性相关联。例如:

    [Category("General")]
[DisplayName("Foos")]
[Description("Bla Foo Bla")]
[TypeConverter(typeof(FoosCoverter))]
public string[] Foos { get; set; }

快速的互联网搜索应该会找到一些示例,让您找到正确的方向。

DialogPage.SaveSettingsToStorage 循环遍历页面的 PropertyDescriptors 集合,检索每个属性的 Converter,调用 CanConvertTo 和 CanConvertFrom 以确保可以将属性转换为字符串/从字符串转换,然后调用转换器的 ConvertToInvariantString 以将属性持久保存到注册表中值(value)。

相反,DialogPage.LoadSettingsfromStorage 循环遍历注册表值,找到与 DialogPage 上的属性匹配的 PropertyDescriptor,检索其 Coverter,然后调用 CanCovertFrom 以确保字符串可以转换回该特定属性类型,然后调用转换器的 ConvertFromIvariantString,然后将值分配回属性。

关于c# - DialogPage - 字符串数组未保留,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24291249/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com