65.9K
CodeProject 正在变化。 阅读更多。
Home

Visual Studio 中的 Windows Mobile 全球化

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.13/5 (7投票s)

2008年4月19日

Ms-PL

1分钟阅读

viewsIcon

32348

downloadIcon

215

在 Compact Framework WinForms 中支持多种语言的简单技术。

引言

Compact Framework 不支持更改 Thread.CurrentUICulture 来动态更改窗体的语言。

背景

这段代码定义了全局命名空间中 System.Globalization.ComponentResourceManager 的自定义版本,它继承自标准版本并重写了 ApplyResource() 以使用自定义区域性。在 InitializeComponent() 中生成的代码将使用此版本进行编译,并且表单编辑器不会出错,因为您没有更改生成的代码。使用“pragma warning disable 436”包装函数以抑制有关使用歧义类引用的警告。

在属性表的“引用”节点中,将 System 的别名从“global”更改为“global,ms”。

extern alias ms;
using cm = ms::System.ComponentModel;
using System.Globalization;
using Wm2005Global;

namespace System.ComponentModel
{
    class ComponentResourceManager : cm.ComponentResourceManager
    {
        public ComponentResourceManager(Type type) : base(type) { }
        public override void ApplyResources(object value, 
                        string objectName, CultureInfo culture)
        {
            if (culture == null)
            {
                culture = Program.Culture;
            }
            base.ApplyResources(value, objectName, culture);
        }
    }
}

这展示了 Visual Studio 如何生成常规代码并默认使用 ComponentResourceManager 的特殊版本。#pragma 禁用了有关使用歧义类的警告。

#region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    #pragma warning disable 436
    private void InitializeComponent()
    {
        System.ComponentModel.ComponentResourceManager resources = 
          new System.ComponentModel.ComponentResourceManager(typeof(Form1));
        this.mainMenu1 = new System.Windows.Forms.MainMenu();
        this.button1 = new System.Windows.Forms.Button();
        this.button2 = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // button1
        // 
        resources.ApplyResources(this.button1, "button1");
        this.button1.Name = "button1";
        this.button1.Click += new System.EventHandler(this.button1_Click);
        // 
        // button2
        // 
        resources.ApplyResources(this.button2, "button2");
        this.button2.Name = "button2";
        this.button2.Click += new System.EventHandler(this.button2_Click);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
        resources.ApplyResources(this, "$this");
        this.Controls.Add(this.button2);
        this.Controls.Add(this.button1);
        this.Menu = this.mainMenu1;
        this.Name = "Form1";
        this.Load += new System.EventHandler(this.Form1_Load);
        this.ResumeLayout(false);

    }
#pragma warning restore 436
    #endregion

    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
}

我认为这是 Windows Mobile 2005 上全球化最简洁的解决方案。您不必更改整个设备的默认语言,甚至可以从菜单项更改窗体的语言而无需关闭它!这还演示了一种释放窗体上的所有控件的技术,再次调用 InitializeComponent()Form_Load(),就像您的窗体在未关闭的情况下被重新创建一样。

© . All rights reserved.