gpt4 book ai didi

asynchronous - 如何在 VS2019 的异步扩展中获取 ShellSettingsManager?

转载 作者:行者123 更新时间:2023-12-05 02:56:33 25 4
gpt4 key购买 nike

我想创建一个小扩展来向 VS2019 添加外部工具列表。快速搜索在 https://learn.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 接口(interface)或 IVsSettings 接口(interface)。鉴于您的 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);
}
}
}
}

真诚的

关于asynchronous - 如何在 VS2019 的异步扩展中获取 ShellSettingsManager?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60432161/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com