Đây là hướng dẫn nhanh có thể cung cấp cho bạn một ý tưởng về cách để đạt được điều này đơn giản (nó khá đơn giản một khi bạn đã thực hiện nó một lần).
Tôi đã xuất phát mã dưới đây từ những gì tôi sử dụng trong my extensions; nó là trong VB.NET, nhưng có thể dễ dàng chuyển đổi thành C#.
Để bắt đầu, chỉ cần thêm lớp này vào dự án tiện ích mở rộng của bạn. Nó phải chứa một thuộc tính cho mỗi giá trị bạn cần lưu trữ. Bạn thậm chí có thể sắp xếp chúng trong các danh mục. Bạn có thể xem at MSDN here đối với các loại được hỗ trợ (đối với các trường hợp phức tạp hơn, bạn có thể tham khảo "trang tùy chọn được tùy chỉnh", chủ đề được bao gồm 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
Sau đó, thêm các thuộc tính sau ngay trước lớp gói chính của bạn.
<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
Bây giờ để dễ dàng truy cập các thiết lập, bạn có thể thêm các tài sản sau đây trong lớp gói tổng thể của bạn:
Protected ReadOnly Property Settings() As OptionPageGrid
Get
Return CType(GetDialogPage(GetType(OptionPageGrid)), OptionPageGrid)
End Get
End Property
Điều này làm cho nó có thể truy cập một giá trị từ bất cứ nơi nào trong lớp sử dụng thân thiện:
If (Me.Settings.MyBooleanSetting) Then MsgBox("It works!");
Visual Studio sẽ chăm sóc kiên trì các thiết lập, và họ nên được bao gồm khi bạn sử dụng chức năng nhập khẩu/xuất khẩu (hoặc bất kỳ phần mở rộng cài đặt đồng bộ như this one).
Tôi có thể tắt vì đây là lần đầu tiên tôi thử cách này nhưng cách duy nhất tôi có thể sử dụng mã mới hơn của bạn là thay đổi loại tham số từ SVsServiceProvider thành IServiceProvider. – jschroedl