DialogPage - 字符串数组不持久 [英] DialogPage - string array not persisted

查看:200
本文介绍了DialogPage - 字符串数组不持久的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发的Visual Studio的扩展。

I'm developing an extension for 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; }
}



酒吧财产完美的作品,并坚持着。

The Bar property works perfectly and is persisted.

FOOS 属性不也是工作(它甚至给你一个很好的弹出选项页面,您可以每行输入一个字符串),这意味着我可以将它设置并以我的扩展使用它,但它不写入注册表/存储。从的

The Foos Property does also work (it even gives you a nice popup in the options page where you can enter one string per line), which means I can set it and also use it in my extension but it is not written to the registry/storage. When I close VS and open it again it's always empty.

报价http://msdn.microsoft.com/en-us/library/ bb166195.aspx> MSDN

Quote from MSDN:

DialogPage的默认实现支持具有相应的转换器,或者是结构或数组的属性该可扩展成具有适当的转换器的特性。对于转换器的列表,请参阅System.ComponentModel命名空间。在Visual Studio扩展样品管理整型,字符串和System.Drawing.Size属性。

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 命名空间。

In my understanding I'm using valid components from the System.ComponentModel namespace.

那么,我做错了什么?我必须以某种方式区别对待数组?

So what am I doing wrong? Do I have to treat arrays somehow differently?

推荐答案

您需要实现和一个自定义类型转换器为您FOOS属性相关联。

You will need to implement and associate a custom TypeConverter for your Foos property.

有没有股票转换器,它涵盖了这种情况,因为当你转换字符串数组为一个字符串,你必须有某种分隔符,所以你可以重新该阵列从字符串。并且,将根据各个程序员应用而变化。因此,需要一个自定义类型转换器。

There is no stock converter that covers this scenario, because when you are converting an array of strings to a string, you have to have some sort of delimiter, so you can reconstitute the array from the string. And that will vary depending upon the individual programmers application. Hence the need for a custom TypeConverter.

所以,你必须获得来自System.ComponentModel.TypeConverter一个新的类,然后将它与你的财产FOOS关联。例如:

So you have to derive a new class from System.ComponentModel.TypeConverter, and then associate it with your Foos property. For example:

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

一个快速的互联网搜索应该拉了几个例子让你指出正确的方向。

A quick internet search should pull up a few examples to get you pointed in the right direction.

的DialogPage.SaveSettingsToStorage遍历页的的PropertyDescriptor集合,检索每个propertie的转换器,将调用CanConvertTo和CanConvertFrom以确保该属性可以从一个字符串被转换成/,然后调用转换器的ConvertToInvariantString坚持属性的注册表值。

The DialogPage.SaveSettingsToStorage loops through a collection of the page's PropertyDescriptors, retrieves each propertie's Converter, invokes CanConvertTo and CanConvertFrom to ensure the property can be converted to/from a string, then calls the converter's ConvertToInvariantString to persist the property to the registry value.

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

Conversely, the DialogPage.LoadSettingsfromStorage loops through the registry values, finds the PropertyDescriptor matching the property on the DialogPage, retrieves its Coverter, then calls CanCovertFrom to ensure the string can be converted back to that particular property type, and then calls the converter's ConvertFromIvariantString, and then assigns the value back to the property.

这篇关于DialogPage - 字符串数组不持久的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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