在扩展选项页面中使用自定义类型 [英] Using custom types in option page for an extension

查看:51
本文介绍了在扩展选项页面中使用自定义类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为Visual Studio 2017的VSIX扩展添加一些更复杂的自定义设置/选项。我们目前都使用的是15.4.2版本,并且不需要向后兼容性。扩展已经有一个自定义选项页面,其中一些设置为
,存储为简单类型(字符串,整数,布尔等),但我试图添加第二个选项页面,其中包含一组对象的设置自定义类型,我很难获得这些设置来正确保存和加载。
页面代码如下所示:

I'm trying to add some more complex custom settings/options to a VSIX extension for Visual Studio 2017. We're all currently on version 15.4.2, and backwards-compatibility is not a requirement. The extension already has a custom option page with some settings that are stored as simple types (strings, ints, bools, etc.), but I am trying to add a second option page with settings that include a collection of objects of a custom type, and I'm having difficulty getting these settings to save and load correctly. The page code looks something like this:

public class MySettingsGroup : UIElementDialogPage
{
    [TypeConverter(typeof(SettingsConverter))]
    public IList<MySettings> { get; set; }

    public class SettingsConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(string))
            {
                return true;
            }
            return base.CanConvertFrom(context, sourceType);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value is string str)
            {
                return JsonConvert.DeserializeObject<IList<ProjectSettings>>(str);
            }
            return base.ConvertFrom(context, culture, value);
        }

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            return base.CanConvertTo(context, destinationType);
        }

        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                return JsonConvert.SerializeObject(value);
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }
    }
}

如您所见,我应用了一个自定义TypeConverter来尝试告诉Visual Studio如何序列化集合自定义设置。这似乎部分有效:如果我查看Documents\Visual Studio 2017 \Settings\Exp,我会看到一个CurrentSettings文件,其中包含
我输入的数据,序列化为JSON,就像我期望的类型转换器一样。但是,每当我重新启动并查看"选项"对话框时,它都无法加载设置。在运行Visual Studio的单个会话期间,一切似乎都很好(我有一个自定义的
WPF UI来与这些设置进行交互,它工作正常),它只是保存和加载,所以我宁愿避免完全接管我自己保存和加载设置数据,并为Visual Studio提供足够的信息以便以
me来处理它。

As you can see, I applied a custom TypeConverter to attempt to tell Visual Studio how to serialize the collection of custom settings. This seems to partially work: if I look in Documents\Visual Studio 2017\Settings\Exp I see a CurrentSettings file that contains the data I enter, serialized as JSON as I would expect from the type converter. However, whenever I restart and look in the Options dialog, it fails to load the settings. During a single session of running Visual Studio, everything seems fine (I have a custom WPF UI to interact with these settings and it works fine), it's just saving and loading, so I would prefer to avoid completely taking over the saving and loading of settings data myself and just provide enough information for Visual Studio to handle it for me.

是否有任何限制(例如自定义选项页面中各个设置/属性的最大长度?让Visual Studio保存和加载存储在自定义数据类型中的设置的最佳/正确方法是什么?

Are there any limitations (e.g. maximum length) on individual settings/properties in a custom option page? What's the best/correct way to get Visual Studio to save and load settings stored in custom data types?

推荐答案

我的系统今天安装了VS 15.6.4当有多个VS实例时,其中一个修复是VS不保存设置。这可能不相关,但我想提一下。
My system installed VS 15.6.4 today and one of the fixes is for VS not saving settings when there are multiple instances of VS. That probably is not relevant but I wanted to mention it.


这篇关于在扩展选项页面中使用自定义类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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