簡體   English   中英

如何防止將長字符串寫入resx文件

[英]How to prevent long strings from being written to resx file

即使我的表單未設置為本地化,如果我有一個文本長度超過200個字符的標簽,Visual Designer也會將文本放入.resx中,而不是直接在代碼中。

這是有問題的,因為我使用的是自定義本地化方法,該方法從代碼中提取可本地化的字符串,但它不搜索resx文件。

有沒有辦法防止這種行為? 我可以在代碼中對字符串進行硬編碼,以便提取程序可以獲取它,但這很煩人,因為在Designer中文本看起來不正確。 另外,我可以使用多個標簽,但是感覺很不對。

我見過其他人在其他論壇上也提出類似的問題,但沒有答案。

我沒有完整的解決方案,但是可以掃描與類型相關聯的資源,並找到設計人員決定執行此操作的字符串。

            var resources = new ResourceManager(type);
            using (var set = resources.GetResourceSet(CultureInfo.InvariantCulture, true, false))
            {
                if (set != null)
                {
                    foreach (DictionaryEntry res in set)
                    {
                        var key = res.Key as string;
                        var val = res.Value as string;
                        if (key == null || val == null)
                            continue;
                        if (!key.EndsWith(".Text"))
                            continue;
                        key = key.Substring(0, key.Length - ".Text".Length);
                        // key is now the name of your control
                        // val is the string the designer stored as its Text in the default resource
                    }
                }
            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM