Tim*_*ord 21 settings add-in visual-studio-2010 visual-studio
目前我在注册表中存储我的自定义插件的设置,但这似乎是一个kludge.我想知道是否有正式的地方存储加载项设置.我倾向于将它们存储在Visual Studio存储设置的位置,以便可以轻松导出和导入它们.
是否可以使用Visual Studio设置存储加载项设置,还是有更好的方法?
这是一个快速教程,可以让您了解如何简单地实现这一点(一旦完成它就非常简单).
我从我在扩展中使用的代码中导出了以下代码; 它在VB.NET中,但可以很容易地转换为C#.
首先,只需将此类添加到扩展项目中.它应包含您需要存储的每个值的属性.您甚至可以按类别排列它们.您可以在此处查看MSDN以获取受支持的类型(对于更复杂的情况,您可以参考"自定义选项页面",这是MSDN在此处介绍的主题).
Imports Microsoft.VisualBasic
Imports System
Imports System.Diagnostics
Imports System.Globalization
Imports System.Runtime.InteropServices
Imports System.ComponentModel.Design
Imports Microsoft.Win32
Imports Microsoft.VisualStudio
Imports Microsoft.VisualStudio.Shell.Interop
Imports Microsoft.VisualStudio.OLE.Interop
Imports Microsoft.VisualStudio.Shell
Imports System.Threading
Imports System.Text.RegularExpressions
Imports System.ComponentModel
<ClassInterface(ClassInterfaceType.AutoDual)>
<CLSCompliant(False), ComVisible(True)>
Public Class OptionPageGrid
Inherits DialogPage
Private _MyBooleanSetting As Boolean = False
<Category("The name or an alias of my extension name")>
<DisplayName("Simple name of this setting displayed for the user")>
<Description("Longer description of this setting")>
Public Property MyBooleanSetting() As Boolean
Get
Return Me._MyBooleanSetting
End Get
Set(ByVal value As Boolean)
Me._MyBooleanSetting = value
End Set
End Property
Private _MyIntegerSetting As Integer = 2
<Category("The name or an alias of my extension name")>
<DisplayName("Simple name of this setting displayed for the user")>
<Description("Longer description of this setting")>
Public Property MyIntegerSetting() As Integer
Get
Return Me._MyIntegerSetting
End Get
Set(ByVal value As Integer)
Me._MyIntegerSetting = value
End Set
End Property
Private _MyStringSetting As String = "DefaultStringValue"
<Category("The name or an alias of my extension name")>
<DisplayName("Simple name of this setting displayed for the user")>
<Description("Longer description of this setting")>
Public Property MyStringSetting() As Integer
Get
Return Me._MyStringSetting
End Get
Set(ByVal value As Integer)
Me._MyStringSetting = value
End Set
End Property
End Class
Run Code Online (Sandbox Code Playgroud)
然后,在主包类之前添加以下属性.
<ProvideOptionPage(GetType(OptionPageGrid), "The name or an alias of my extension name", "The name of a category of settings", 0, 0, True)>
Public NotInheritable Class MyExtensionMainClass
Inherits Package
Run Code Online (Sandbox Code Playgroud)
现在,要轻松访问设置,您可以在主包类中添加以下属性:
Protected ReadOnly Property Settings() As OptionPageGrid
Get
Return CType(GetDialogPage(GetType(OptionPageGrid)), OptionPageGrid)
End Get
End Property
Run Code Online (Sandbox Code Playgroud)
这样就可以使用友好的方式从类中的任何位置访问值:
If (Me.Settings.MyBooleanSetting) Then MsgBox("It works!");
Run Code Online (Sandbox Code Playgroud)
Visual Studio将负责持久保存设置,并且在使用导入/导出功能(或任何类似此设置的同步扩展)时应包含它们.
编辑
我对这个主题的原始答案有几个问题,经过多年的使用我发现了.为了完整起见,我在下面列出了它,但这是我对此的最新想法.
在VSIX中使用应用程序设置不是版本安全的.存储的设置文件路径的位置部分包括可执行文件的版本字符串和散列.当Visual Studio安装官方更新时,这些值会更改,从而更改设置文件路径.Visual Studio本身不支持使用应用程序设置,因此它不会尝试将此文件迁移到新位置,并且所有信息都基本上丢失了.支持的设置方法是WritableSettingsStore.它与应用程序设置非常相似,并且很容易通过SVsServiceProvider进行访问
public static WritableSettingsStore GetWritableSettingsStore(this SVsServiceProvider vsServiceProvider)
{
var shellSettingsManager = new ShellSettingsManager(vsServiceProvider);
return shellSettingsManager.GetWritableSettingsStore(SettingsScope.UserSettings);
}
Run Code Online (Sandbox Code Playgroud)
原始答案
最直接的方法是使用.Net的应用程序设置基础结构来存储任何设置.它是一个成熟的框架,设计人员支持为您的项目添加设置基础结构.
但是它没有与Visual Studio的导入/导出设置基础结构集成.让它工作是一个非常复杂的过程,包括将自己注册为VSPackage,实现设置模式等...我通常发现它真的不值得运行的麻烦(从未成功)
| 归档时间: |
|
| 查看次数: |
4409 次 |
| 最近记录: |