You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Visual Studio 2022中如何让悬停GetGuiText时显示实际文本?

在Visual Studio 2022中让多语言文本方法悬停显示实际内容的解决方案

针对你遇到的问题,这里提供几种实用方案,无需跳转翻译文件就能在悬停时看到对应文本:

方案一:定义具名常量并添加注释

把硬编码的数字索引替换成带注释的具名常量,调用时使用常量而非直接写数字,悬停时就能直接看到对应文本信息:

// 新增索引常量类
public static class TranslationKeys
{
    /// <summary>
    /// 文本内容:英文=English text | 德文=German text
    /// </summary>
    public const int WelcomeText = 0;
    /// <summary>
    /// 文本内容:英文=English text 2 | 德文=German text 2
    /// </summary>
    public const int ConfirmText = 1;

    public static class Languages
    {
        /// <summary>
        /// 英文语言
        /// </summary>
        public const int English = 0;
        /// <summary>
        /// 德文语言
        /// </summary>
        public const int German = 1;
    }
}

// 调用示例
var text = Translations.GetGuiText(TranslationKeys.ConfirmText, TranslationKeys.Languages.German);

这样悬停TranslationKeys.ConfirmText或TranslationKeys.Languages.German时,就能直接看到注释里的对应文本,同时还能避免硬编码数字带来的维护隐患。

方案二:给GetGuiText方法添加XML文档注释

直接在方法上补充包含索引对应关系的注释,悬停方法调用时就能看到参数说明里的文本信息:

public class Translations
{
    private static readonly string[,] _text = new string[,] {
        {"English text", "German text"},
        {"English text 2", "German text 2"},
    };
    
    /// <summary>
    /// 获取指定索引的多语言文本
    /// </summary>
    /// <param name="textIndex">文本索引:
    /// 0 = English text(英文)/ German text(德文);
    /// 1 = English text 2(英文)/ German text 2(德文)
    /// </param>
    /// <param name="languageIndex">语言索引:0=英文,1=德文</param>
    /// <returns>对应语言的文本内容</returns>
    public static string GetGuiText(int textIndex, int languageIndex) => _text[textIndex, languageIndex];
}

这种方式无需额外定义类,适合文本量较少的场景,但文本更新时需要同步修改注释,维护成本略高。

方案三:自定义Visual Studio IntelliSense扩展(进阶)

如果项目文本量极大,可编写自定义Roslyn分析器或VS扩展,实现动态解析索引对应的文本并显示在悬停提示中。不过这种方式需要具备VS扩展开发能力,适合大型团队或长期维护的项目。

内容的提问来源于stack exchange,提问作者Flippowitsch

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 16:03:33