VSPackage:在运行时修改工具栏按钮文本/工具提示

发布于 2024-09-28 18:38:57 字数 2701 浏览 7 评论 0原文

我一直在开发一个 Visual Studio 扩展作为 VS2008/2010 的插件。现在的新版本将作为仅适用于 VS2010 的 VSIX 包来完成(因为无论如何它必须是 .NET 4),并且我在 UI 处理(我认为相当简单)方面遇到了一些麻烦。

该扩展主要由一个工具栏组成,其中包含一些启动各种操作、表单等的按钮,以及一些仅用作显示某些状态信息的标签。 “标签”按钮本身仅显示非常简短的信息,而工具提示则提供更多详细信息。

虽然我不认为整个外接程序非常优雅,但做这种事情非常简单(尽管我的方法可能有点业余)。将命令添加到工具栏时,我会将“标签”按钮“保存”在特定的局部变量中,以便稍后可以随意设置标题和工具提示文本。

在 VSPackage 中,与 Microsoft.VisualStudio.CommandBars.CommandBarButton 等效的项似乎是 OleMenuCommand。通过MenuCommandService查找“标签”命令没有问题,但是根据需要修改它是问题。

为了了解如何执行此操作,我只有一个工具栏,其中一组有两个按钮。 btnAction非常简单;只是一个图标和一个执行处理程序来更改另一个按钮上的文本,没有 CommandFlags。 btnLabel 在 .vsct 中看起来像这样:

<Button guid="guidVSPackageBuilderTutorialCommandSet" id="btnLabel" priority="0x0100">
    <CommandFlag>DefaultDisabled</CommandFlag>
    <CommandFlag>DontCache</CommandFlag>
    <CommandFlag>NoCustomize</CommandFlag>
    <CommandFlag>TextChanges</CommandFlag>
    <CommandFlag>TextOnly</CommandFlag>
    <Strings>
        <CommandName>cmdidbtnLabel</CommandName>
        <ButtonText>btnLabel</ButtonText>
        <MenuText>btnLabel</MenuText>
        <ToolTipText>Tooltip btnLabel</ToolTipText>
    </Strings>
</Button>

第一个问题是,当我使用 TextChanges 时,ToolTipText 字符串被忽略,并且 < em>ButtonText 最初也用于工具提示。

操作按钮的处理程序代码如下:

private int iClickCount = 0;

protected override void btnActionExecuteHandler(object sender, EventArgs e)
{
    var svc = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;

    CommandID idBtnLabel = new CommandID(GuidList.guidVSPackageBuilderTutorialCmdSet, (int)PkgCmdIDList.btnLabel);
    var cmd = svc.FindCommand(idBtnLabel) as OleMenuCommand;

    cmd.Text = "Clicked " + (++iClickCount) + " times";
}

这会按预期更改 btnLabel 的标题,但由于无法显式指定工具提示(OleMenuCommand 对象只是有一个 Text 属性,与 CommandBarButton 不同,它同时具有 CaptionTooltipText),工具提示始终设置为与标题相同的字符串。据我了解,这是因为使用 FindCommand() 我实际上并没有获得 UI 按钮,而只是获得了底层命令,它不关心工具提示。

更令人困惑的是,当我使用 TextChangesButton CommandFlag 而不是 TextChanges 时会发生什么。该按钮现在将正确显示 .vsct 中定义的工具提示文本,但当我单击另一个按钮时,标题和工具提示都不会更改 - 尽管当我检查 btnLabel 命令的 Text< /em> 属性,它设置为我所期望的(“点击 x 次”)。 TextChangesButton 是否“解耦”了命令和按钮的属性?如果是这样,这几乎就是我想要的(我不关心命令,因为无论如何都没有什么可执行的;btnLabel将始终被禁用),但是我如何访问按钮和它的特定字符串属性?

我查看了各种 IV 接口和 SV 服务,但找不到合适的东西,而且文档(和 IntelliSense 帮助)似乎不是很广泛。

I have been developing a Visual Studio extension as an Add-in for VS2008/2010. The new version now is to be done as a VSIX package for VS2010 only (as it has to be .NET 4 anyway), and I am having some troubles with (rather simple, I would think) UI handling.

The extension primarily consists of a toolbar with some buttons that launch various actions, forms etc., and a few that are only used as labels to display some state information. The "label" buttons themselves just show very short and concise information, while the tooltips provide more detail.

While I didn't think the whole Add-in thing was very elegant, doing this kind of stuff was pretty straightforward (though my approach may have been a little amateurish). When adding the commands to the toolbar, I would "save" the "label" buttons in specific local variables, allowing me to later set the caption and tooltip text at will.

In a VSPackage, the equivalent to Microsoft.VisualStudio.CommandBars.CommandBarButton appears to be OleMenuCommand. Finding the "label" command through the MenuCommandService is no problem, modifying it as needed is, however.

For the purpose of finding out how to do this, I just have a toolbar with two buttons in a group. btnAction is very simple; just an icon and an execute handler to change the text on the other button, no CommandFlags.
btnLabel looks like this in the .vsct:

<Button guid="guidVSPackageBuilderTutorialCommandSet" id="btnLabel" priority="0x0100">
    <CommandFlag>DefaultDisabled</CommandFlag>
    <CommandFlag>DontCache</CommandFlag>
    <CommandFlag>NoCustomize</CommandFlag>
    <CommandFlag>TextChanges</CommandFlag>
    <CommandFlag>TextOnly</CommandFlag>
    <Strings>
        <CommandName>cmdidbtnLabel</CommandName>
        <ButtonText>btnLabel</ButtonText>
        <MenuText>btnLabel</MenuText>
        <ToolTipText>Tooltip btnLabel</ToolTipText>
    </Strings>
</Button>

The first problem with this is that when I use TextChanges, the ToolTipText string is ignored and the ButtonText is initially used for the tooltip as well.

The handler code for the action button is as follows:

private int iClickCount = 0;

protected override void btnActionExecuteHandler(object sender, EventArgs e)
{
    var svc = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;

    CommandID idBtnLabel = new CommandID(GuidList.guidVSPackageBuilderTutorialCmdSet, (int)PkgCmdIDList.btnLabel);
    var cmd = svc.FindCommand(idBtnLabel) as OleMenuCommand;

    cmd.Text = "Clicked " + (++iClickCount) + " times";
}

This changes the caption of btnLabel as expected, but as there is no way of explicitly specifying the tooltip (the OleMenuCommand object just has a Text property, unlike CommandBarButton, which has both Caption and TooltipText), the tooltip is always set to the same string as the caption. From what I understand, this is because with FindCommand() I am not actually getting the UI button, but only the underlying command, which doesn't care about tooltips.

What's even more confusing is what happens when I use the TextChangesButton CommandFlag instead of TextChanges. The button will now correctly display the tooltip text defined in the .vsct, but neither the caption nor the tooltip will change when I click the other button - though when I check the btnLabel command's Text property, it is set to what I expect ("Clicked x times"). Does TextChangesButton kind of "decouple" the properties of the command and the button ? If so, this is pretty much what I want (I don't care about the command, as there is nothing to execute anyway; btnLabel will always be disabled), but how can I access the button and its particular string properties ?

I looked through the various IVs interfaces and SVs services but couldn't find something appropriate, and the documentation (and IntelliSense help) doesn't seem to be very extensive.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴骨ら 2024-10-05 18:38:57

我自己回答这个问题 - 根据 VS 团队的某人的说法,新的可扩展性结构没有提供如此详细地访问控件的这些属性的方法。对于我想要实现的目标,有必要采用老式方法来获取 DTE 对象,找到我的工具栏和控件,然后将它们简单地作为 CommandBarButton 进行处理,就像我在外接程序中所做的那样。

To answer this myself - according to someone from the VS team, the new extensibility structure does not offer ways to access these properties of controls in such detail. For what I want to achieve it will be necessary to go the old-fashioned way of getting hold of the DTE object, finding my toolbar and my controls and simply handle them as CommandBarButtons, just as I did in my Add-in.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文