How do I get IMenuCommandService? #349
-
I am trying to modify the disabled state of various menu buttons in my extension based on dynamic factors. I think I see how to do this, but all the examples seem to require getting a reference to the IMenuCommandService, and every way I've tried getting it always returns null (or throws an exception, in the case of VS.GetRequiredService). I was following other examples, at least in code, but I might be missing something somewhere else, like in the manifest or something, I dunno. In all these cases, I've trying to call it in InitializeAsync of the ToolkitPackage, my thought is that maybe this is too early, but I have seen other examples (for instance here), though admittedly this is optional. I am autoloading at the NoSolution level, though delaying that to SolutionExists and CodeWindow didn't seem to help either, so I don't know if that matters. I can show code examples if I need to, but I was matching the above code from the FileNesting extension, and that doesn't work, so I must be missing something somewhere else that I don't know about. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
The So in your package class, instead of calling this: commandService = await VS.GetServiceAsync<IMenuCommandService, IMenuCommandService>();
// ❌ Will throw an exception. You instead need to call this: commandService = await this.GetServiceAsync<IMenuCommandService, IMenuCommandService>(); |
Beta Was this translation helpful? Give feedback.
The
IMenuCommandService
can only be retrieved from a VSPackage or tool window (see #92 (comment)).So in your package class, instead of calling this:
You instead need to call this: