From e8b3de6afe5d425571f984784dc6284e596d6f64 Mon Sep 17 00:00:00 2001 From: lindexi Date: Thu, 12 Jan 2017 11:29:55 +0800 Subject: [PATCH 1/3] Add to read the configure --- Options_Page/C#/OptionsPagePackage.cs | 43 ++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/Options_Page/C#/OptionsPagePackage.cs b/Options_Page/C#/OptionsPagePackage.cs index b00e94c9..b3525eb2 100644 --- a/Options_Page/C#/OptionsPagePackage.cs +++ b/Options_Page/C#/OptionsPagePackage.cs @@ -11,6 +11,8 @@ IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR using Microsoft.VisualStudio.Shell; using System; using System.Runtime.InteropServices; +using Microsoft.VisualStudio; +using Microsoft.VisualStudio.Shell.Interop; namespace Microsoft.Samples.VisualStudio.IDE.OptionsPage { @@ -19,7 +21,7 @@ namespace Microsoft.Samples.VisualStudio.IDE.OptionsPage /// The package class uses a number of registration attributes to specify integration parameters. /// [PackageRegistration(UseManagedResourcesOnly = true)] - [ProvideOptionPageAttribute(typeof(OptionsPageGeneral),"My Options Page (C#)","General", 100, 101, true, new string[] { "Change sample general options (C#)" })] + [ProvideOptionPageAttribute(typeof(OptionsPageGeneral), "My Options Page (C#)", "General", 100, 101, true, new string[] { "Change sample general options (C#)" })] [ProvideProfileAttribute(typeof(OptionsPageGeneral), "My Options Page (C#)", "General Options", 100, 101, true, DescriptionResourceID = 100)] [ProvideOptionPageAttribute(typeof(OptionsPageCustom), "My Options Page (C#)", "Custom", 100, 102, true, new string[] { "Change sample custom options (C#)" })] [InstalledProductRegistration("My Options Page (C#)", "My Options Page (C#) Sample", "1.0")] @@ -35,5 +37,44 @@ protected override void Initialize() base.Initialize(); // TODO: add initialization code here } + + public static OptionsPagePackageCS EnsurceOptionsPagePackageCs() + { + IVsShell shell = Package.GetGlobalService(typeof(SVsShell)) as IVsShell; + if (shell != null) + { + IVsPackage package; + Guid guid = new Guid(GuidStrings.GuidPackage); + if (ErrorHandler.Succeeded(shell.IsPackageLoaded(ref guid, out package))) + { + return package as OptionsPagePackageCS; + } + if (ErrorHandler.Succeeded(shell.LoadPackage(ref guid, out package))) + { + return package as OptionsPagePackageCS; + } + } + return null; + } + + /// + /// Get the OptionsPageGeneral + /// + /// + public static OptionsPageGeneral OptionsPageGeneral() + { + OptionsPagePackageCS package = EnsurceOptionsPagePackageCs(); + return package?.GetDialogPage(typeof(OptionsPageGeneral)) as OptionsPageGeneral; + } + + /// + /// Get the OptionsPageCustom + /// + /// + public static OptionsPageCustom OptionsPageCustom() + { + OptionsPagePackageCS package = EnsurceOptionsPagePackageCs(); + return package?.GetDialogPage(typeof(OptionsPageCustom)) as OptionsPageCustom; + } } } From 077dfbb27eb45828563da3cb836ed2d95fe97537 Mon Sep 17 00:00:00 2001 From: lindexi Date: Wed, 11 Apr 2018 14:26:57 +0800 Subject: [PATCH 2/3] Fix spelling. --- Options_Page/C#/OptionsPagePackage.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Options_Page/C#/OptionsPagePackage.cs b/Options_Page/C#/OptionsPagePackage.cs index b3525eb2..29f66d42 100644 --- a/Options_Page/C#/OptionsPagePackage.cs +++ b/Options_Page/C#/OptionsPagePackage.cs @@ -38,13 +38,14 @@ protected override void Initialize() // TODO: add initialization code here } - public static OptionsPagePackageCS EnsurceOptionsPagePackageCs() + public static OptionsPagePackageCS EnsurePackageIsLoaded() { IVsShell shell = Package.GetGlobalService(typeof(SVsShell)) as IVsShell; if (shell != null) { IVsPackage package; Guid guid = new Guid(GuidStrings.GuidPackage); + if (ErrorHandler.Succeeded(shell.IsPackageLoaded(ref guid, out package))) { return package as OptionsPagePackageCS; @@ -63,7 +64,7 @@ public static OptionsPagePackageCS EnsurceOptionsPagePackageCs() /// public static OptionsPageGeneral OptionsPageGeneral() { - OptionsPagePackageCS package = EnsurceOptionsPagePackageCs(); + OptionsPagePackageCS package = EnsurePackageIsLoaded(); return package?.GetDialogPage(typeof(OptionsPageGeneral)) as OptionsPageGeneral; } @@ -73,7 +74,7 @@ public static OptionsPageGeneral OptionsPageGeneral() /// public static OptionsPageCustom OptionsPageCustom() { - OptionsPagePackageCS package = EnsurceOptionsPagePackageCs(); + OptionsPagePackageCS package = EnsurePackageIsLoaded(); return package?.GetDialogPage(typeof(OptionsPageCustom)) as OptionsPageCustom; } } From 7f43bb25f5900d9c3f0f7f2cde88a02544ec3e00 Mon Sep 17 00:00:00 2001 From: lindexi Date: Wed, 11 Apr 2018 14:28:01 +0800 Subject: [PATCH 3/3] calling LoadPackage will just return the package if it's already been loaded --- Options_Page/C#/OptionsPagePackage.cs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Options_Page/C#/OptionsPagePackage.cs b/Options_Page/C#/OptionsPagePackage.cs index 29f66d42..9d69fab5 100644 --- a/Options_Page/C#/OptionsPagePackage.cs +++ b/Options_Page/C#/OptionsPagePackage.cs @@ -46,10 +46,6 @@ public static OptionsPagePackageCS EnsurePackageIsLoaded() IVsPackage package; Guid guid = new Guid(GuidStrings.GuidPackage); - if (ErrorHandler.Succeeded(shell.IsPackageLoaded(ref guid, out package))) - { - return package as OptionsPagePackageCS; - } if (ErrorHandler.Succeeded(shell.LoadPackage(ref guid, out package))) { return package as OptionsPagePackageCS;