Oto krótki samouczek to może dać ci wyobrażenie, jak to osiągnąć w prosty sposób (jest to całkiem proste, gdy raz to zrobisz).
Otrzymałem poniższy kod z tego, co używam w my extensions; jest w VB.NET, ale można go łatwo przekonwertować na C#.
Aby rozpocząć, po prostu dodaj tę klasę do swojego projektu rozszerzenia. Powinien zawierać właściwość dla każdej wartości, którą chcesz przechowywać. Możesz nawet rozmieścić je w kategoriach. Możesz sprawdzić, które typy są obsługiwane (w przypadku bardziej złożonych przypadków możesz odwoływać się do "spersonalizowanych stron opcji", które są tematem obejmującym by MSDN here).
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
Następnie dodaj następujące atrybuty tuż przed swoją klasą pakietu nadrzędnego.
<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
Teraz w łatwy sposób uzyskać dostęp do ustawień, można dodać następującą właściwość w Master Class pakietu:
Protected ReadOnly Property Settings() As OptionPageGrid
Get
Return CType(GetDialogPage(GetType(OptionPageGrid)), OptionPageGrid)
End Get
End Property
Pozwala to na dostęp do wartości z dowolnego miejsca w klasie używając przyjazne:
If (Me.Settings.MyBooleanSetting) Then MsgBox("It works!");
Visual Studio zajmie utrzymujących ustawienia, a powinny być one uwzględnione podczas korzystania z funkcji importu/eksportu (lub jakiegokolwiek rozszerzenia ustawienia synchronizacji jak this one).
Mogę być daleko, ponieważ jest to moja pierwsza próba, ale jedynym sposobem, w jaki wydaje mi się, że mogę użyć twojego nowszego kodu, jest zmiana typu parametru z SVsServiceProvider na IServiceProvider. – jschroedl