我有一个我使用的 DSL 工具项目,[ProvideAutoLoad]
因为它向 Visual Studio 添加了一组菜单命令,以允许用户转换代码(我们有很多文本模板)以及其他一些东西。
由于 VS2019不再允许自动加载同步包,即使启用自动加载选项,我也会收到烦人的警告。
显然,微软没有计划提供异步版本ModelingPackage
(我实际上开始质疑他们是否不会完全停止使用 DSL 工具)。
有人找到解决此问题的方法吗?
我尝试使用另一个包 - 构建为AsyncPackage
- 以便加载我的 DSL-tools 包,但我最终使用该服务InitializeAsync()
遇到各种异常。IVsShell
protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<VSShell.ServiceProgressData> progress)
{
// Default behavior
await base.InitializeAsync(cancellationToken, progress).ConfigureAwait(false);
// Load the service designer package
this.EnsureDesignerPackage();
// Switche to the UI thread
await JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);
// Menu commands
this.InitializeMenuCommands();
}
private void EnsureDesignerPackage()
{
// Package already loaded?
if (!this.IsDesignerPackageLoaded())
{
this.LoadDesignerPackage();
}
}
private bool IsDesignerPackageLoaded()
{
// Package identifier
Guid packageId = new Guid(GlobalConstants.ServiceDesignerPackageId);
// Is loaded?
IVsShell service = this.VsShellService;
int hr = this.VsShellService.IsPackageLoaded(ref packageId, out IVsPackage package);
if (ErrorHandler.Succeeded(hr) && package != null)
{
return true;
}
// Default result
return false;
}
private void LoadDesignerPackage()
{
// Package identifier
Guid packageId = new Guid(GlobalConstants.ServiceDesignerPackageId);
// Not loaded?
int hr = this.VsShellService.IsPackageLoaded(ref packageId, out IVsPackage package);
if (hr != VSConstants.S_OK || package == null)
{
// Load
hr = this.VsShellService.LoadPackage(ref packageId, out package);
if (ErrorHandler.Failed(hr))
{
string message = "Service designer loading failed: {0}.".Format().With(hr);
this.ShowException(message);
}
}
}