我想创建一个小的扩展,以将外部工具列表添加到VS2019。快速搜索显示了https://docs.microsoft.com/en-us/visualstudio/extensibility/writing-to-the-user-settings-store?view=vs-2019似乎是完美的示例代码。这添加了一个调用记事本的命令,所以我认为通过一些编辑,我的工作就完成了。
但是,此示例被编写为不推荐使用的同步扩展,因此我尝试将用于MenuItemCallBack的代码放入扩展的Execute方法中,但该行
SettingsManager settingsManager = new ShellSettingsManager(ServiceProvider);
无法编译,因为ServiceProvider现在为IAsyncServiceProvider类型,并且ShellSettingsManager构造函数需要IServiceProvider类型的参数。
据我所知,ShellSettingsManager仍然是访问设置存储的方法,但是我可以找到的所有示例都涉及将代码放入MenuItemCallback(以及已有好几年的历史),因此用于同步扩展。
所以,有人可以指出我推荐的方法来访问异步扩展中的设置存储吗?
ShellSettingsManager构造函数采用IServiceProvider接口或IVsSettings接口。给定您的AsyncPackage派生对象实现IServiceProvider,您应该能够将其作为参数传递给构造函数。以下快速演示包对我有用:
using System;
using System.ComponentModel.Design;
using System.Runtime.InteropServices;
using System.Threading;
using Microsoft;
using Microsoft.VisualStudio.Settings;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Shell.Settings;
using Task = System.Threading.Tasks.Task;
namespace UserSettingsDemo
{
[PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)]
[Guid(UserSettingsDemoPackage.PackageGuidString)]
[ProvideMenuResource("Menus.ctmenu", 1)]
public sealed class UserSettingsDemoPackage : AsyncPackage
{
public const string PackageGuidString = "cff6cdea-21d1-4736-b5ea-6736624e718f";
public static readonly Guid CommandSet = new Guid("dde1417d-ae0d-46c4-8c84-31883dc1a835");
public const int ListExternalToolsCommand = 0x0100;
protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
{
await this.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);
OleMenuCommandService commandService = await GetServiceAsync(typeof(IMenuCommandService)) as OleMenuCommandService;
Assumes.Present(commandService);
var menuItem = new MenuCommand(OnListExternalTools, new CommandID(CommandSet, ListExternalToolsCommand));
commandService.AddCommand(menuItem);
}
private void OnListExternalTools(object sender, EventArgs e)
{
ShellSettingsManager settingsManager = new ShellSettingsManager(this);
WritableSettingsStore userSettingsStore = settingsManager.GetWritableSettingsStore(SettingsScope.UserSettings);
int toolCount = userSettingsStore.GetInt32("External Tools", "ToolNumKeys");
for (int i = 0; i < toolCount; i++)
{
string tool = userSettingsStore.GetString("External Tools", "ToolCmd" + i);
VsShellUtilities.ShowMessageBox(this, tool, "External Tools", OLEMSGICON.OLEMSGICON_INFO,
OLEMSGBUTTON.OLEMSGBUTTON_OK, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
}
}
}
}
真诚的