Visual Studio 设计器 - 限制属性网格以显示语言属性的某些特定语言 [英] Visual Studio Designer - Limit property grid to show some specific languages for Language property
问题描述
我正在Visual Studio 2013(C#)中进行开发,正在寻找一种使Windows窗体的语言选择更易于本地化的方法.
I am developing in Visual Studio 2013 (C#) and am searching for a way to make the selection of languages for a windows form easier for localization.
在设计器中,您可以在"设计"→"语言"下选择所需的语言,Visual Studio通过创建必要的本地化文件来完成其余工作如果您需要新的本地化表格.
In the designer you have the possibility to select the language you want under "Design"→"Language" and Visual Studio does the rest by creating the necessary localization files if you need a new localized form.
问题是:目前,我只使用两种不同的语言,并且每次在这些本地化表单之间切换时,它会向我显示所有可能语言的列表,如果单击不正确,可能会导致Visual Studio创建本地化版本.我不想要的语言,这很烦人,并且使项目中包含了更多文件.
The problem is: Currently I only work with two different languages, and every time when I switch between those localized forms it shows me a list of all possible languages, where a wrong click can cause visual studio to create localized version of a language I don't want, which is just annoying and clutters the project with more files.
有人知道将设计器选项中显示的语言限制为所需数量和选择的方法吗?
Does anyone know a way to limit the languages shown in the designer options to a desired amount and selection?
推荐答案
Language属性是仅设计时属性,不属于 Form
类.它是使用扩展程序提供程序添加到表单设计时的扩展属性.它是 CultureInfo
的类型,并使用 TypeConverter
显示所有可用的区域性.
Language property is a design-time only property which doesn't belong to Form
class. It's an extended property added using an extender provider to design-time of form. It is of type of CultureInfo
and uses a TypeConverter
which shows all available cultures.
作为一种解决方法,您可以拥有一个 BaseForm
,其中包含 FormLanguage
之类的属性,并且在 get
中,返回 Language的值
属性,然后在 set
中设置 Language
属性的值.然后从此 BaseForm
继承所有表单.这样,更改 FormLanguage
属性就足够了.
As a workaround , you can have a BaseForm
containing a property like FormLanguage
and in the get
, return value of Language
property and in the set
, set the value of Language
property. Then inherit all your forms from this BaseForm
. This way, it's enough to change FormLanguage
property.
还为 CultureInfo
创建一个自定义类型转换器,该转换器仅显示您想要的区域性,然后更改语言,更改 FormLanguage
属性就足够了.
Also create a custom type converter for CultureInfo
which shows only those culture which you want, then to change language, it's enough to change FormLanguage
property.
这是我用作解决方法的代码.不要忘记从此 BaseClass
继承表单.
Here is the code I used as workaround. Don't forget to inherit your forms from this BaseClass
.
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Globalization;
using System.Linq;
using System.Windows.Forms;
public partial class BaseForm : Form
{
[TypeConverter(typeof(MyCultureInfoConverter))]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public CultureInfo FormLanguage
{
get
{
return TypeDescriptor.GetProperties(this)["Language"]
.GetValue(this) as CultureInfo;
}
set
{
TypeDescriptor.GetProperties(this)["Language"].SetValue(this, value);
}
}
}
public class MyCultureInfoConverter : CultureInfoConverter
{
public override StandardValuesCollection
GetStandardValues(ITypeDescriptorContext context)
{
var values = CultureInfo.GetCultures(CultureTypes.SpecificCultures |
CultureTypes.NeutralCultures)
.Where(x => x.Name == "fa-IR" || x.Name == "en-US").ToList();
values.Insert(0, CultureInfo.InvariantCulture);
return new StandardValuesCollection(values);
}
}
当您从 FormLanguage
中选择 fa-IR
时,由于以下原因, Language
会自动变为波斯语
我们用 FormLanguage
的二传手写的.您可以在 MyCultureInfoConverter
中添加所需的任何其他语言.
When you select fa-IR
from FormLanguage
then the Language
becomes Persian
automatically because of the code which we wrote in setter of FormLanguage
. You can add any other language which you need in MyCultureInfoConverter
.
这篇关于Visual Studio 设计器 - 限制属性网格以显示语言属性的某些特定语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!