(一)、引言
本地化可能有点专业术语,通俗一点说就是支持多国语言。
本地化在.Net中的实现真的是太简单了,不过对于没有接触过的朋友可能并不知道。
(二)、完成效果
先看下完成后的效果有助于提高大家的兴趣。
情节:默认中文显示 TaskVision1.0 自动更新为 TaskVision2.0
更改显示方式为英文
间隔15秒左右,客户端检测到更新
点击是,提示更新完成
点击是,运行新版本TaskVision 2.0
选择语言-》英文
点击是,启动英文版本
情节:从英文显示 TaskVision1.0 自动更新为 TaskVision2.0
更改显示方式为中文
具体省略,怕图片太多,大家加载速度会很慢,显示重点。
检测到更新
下载完成
启动新版本,切换为中文
代码下载:http://files.cnblogs.com/a-peng/SmartClient_Chapter02.rar
(三)、分析
看了上面的效果后,你心动了吗,心动的话就看下去,你很快就将拥有它。
注意:左边栏类似Windows XP左边栏的效果使用TaskVision中自带的第三方控件XPanderControl.dll。没有源码。
不过还有一个效果一样名称一样的开源控件大家可以看下:http://www.codeproject.com/KB/cpp/XPander.aspx
我们设计完主窗体MainForm后,设置其属性Localizable为true。
则主窗体中显示的文本都被嵌入到资源文件中,如下图:
我们复制该资源文件,命名为MainForm.en.resx
修改内容如下图:
修改主窗体MainForm构造函数如下:
public MainForm()


{
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en");
InitializeComponent();
}
运行程序,我们立马可以看到英文本的效果。注意修改当前UI界面文化必须在InitializeComponent()之前。
为何修改文化为en,则会显示英文呢?因为修改文化为en,则系统会自动加载相关文化的资源如MainForm.en.resx就显示英文了,同理可以添加其它语言的资源文件,则可支持其它语言。
我们还创建了两个资源:Localize.resx,Localize.en.resx用来存放程序中用到的一些文本。
注意使用ResourceManager加载该资源文件时,必须加上命名空间TaskVision。
主窗体源码如下:
using System;
using System.Windows.Forms;

using System.Resources;
using System.Threading;
using Microsoft.Win32;

namespace TaskVision


{
public partial class MainForm : Form

{
private const string m_registryKey = @"Software\Microsoft\TaskVision"; // 注册表位置
private const string m_defaultCultureName = "";
private const string m_englistCultureName = "en";

private ResourceManager m_resourceManager = new ResourceManager("TaskVision.Localize", System.Reflection.Assembly.GetExecutingAssembly());

public MainForm()

{
string selectedLanguage = Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName;
RegistryKey regKey = Registry.CurrentUser.OpenSubKey(m_registryKey);

try

{
if (regKey != null && regKey.GetValue("Language") != null)

{
selectedLanguage = regKey.GetValue("Language").ToString();
regKey.Close();
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(selectedLanguage);
}
}
catch

{
}

InitializeComponent();

if (selectedLanguage == m_englistCultureName)

{
menuChinese.Checked = false;
menuEnglish.Checked = true;
}
else

{
menuChinese.Checked = true;
menuEnglish.Checked = false;
}
}

private void MainForm_Load(object sender, EventArgs e)

{

}


菜单栏#region 菜单栏

private void menuChinese_Click(object sender, EventArgs e)

{
if (!menuChinese.Checked)

{
menuChinese.Checked = true;
menuEnglish.Checked = false;

SetLanguage(m_defaultCultureName);
}
}

private void menuEnglish_Click(object sender, EventArgs e)

{
if (!menuEnglish.Checked)

{
menuChinese.Checked = false;
menuEnglish.Checked = true;

SetLanguage(m_englistCultureName);
}
}

private void SetLanguage(string cultureName)

{
try

{
RegistryKey regKey = Registry.CurrentUser.CreateSubKey(m_registryKey);
regKey.SetValue("Language", cultureName);
regKey.Close();

DialogResult mbResult = MessageBox.Show(m_resourceManager.GetString("Restarted_application_Content") + "\n\n" + m_resourceManager.GetString("Restarted_application_Content2"), m_resourceManager.GetString("Restarted_application_Title"), MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
if (mbResult == DialogResult.Yes)

{
this.Close();
appUpdater.RestartApp();
}
}
catch

{
}
}

#endregion


AppUpdater自动更新#region AppUpdater自动更新

private void appUpdater_OnUpdateDetected(object sender, System.EventArgs e)

{
if (MessageBox.Show(m_resourceManager.GetString("UpdateDetected_application_Content") + "\n\n" + m_resourceManager.GetString("UpdateDetected_application_Content2"), m_resourceManager.GetString("UpdateDetected_application_Title"), MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly) == DialogResult.Yes)

{
appUpdater.DownloadUpdate();
}
}

private void appUpdater_OnUpdateComplete(object sender, Microsoft.Samples.AppUpdater.UpdateCompleteEventArgs e)

{
if (e.UpdateSucceeded)

{
if (MessageBox.Show(m_resourceManager.GetString("UpdateCompleteSuccess_application_Content") + "\n\n" + m_resourceManager.GetString("UpdateCompleteSuccess_application_Content2"), m_resourceManager.GetString("UpdateCompleteSuccess_application_Title"), MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly) == DialogResult.Yes)

{
appUpdater.RestartApp();
}
}
else

{
MessageBox.Show(m_resourceManager.GetString("UpdateCompleteFailure_application_Content"), m_resourceManager.GetString("UpdateCompleteFailure_application_Title"), MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

#endregion
}
}
好了,就到这里了。
希望越来越多的朋友加入到WinForms的开发中,多写一些心得体会互相交流。
比如如何不使用第三方控件,构建中类似千千静听,PPStream,腾迅qq之类专业软件的界面效果。小菜不知道如何下手。
*************************************************************************
作者:a-peng
出处:http://a-peng.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出
原文连接,否则保留追究法律责任的权利。
*************************************************************************
posted @ 2008-10-05 21:43
阿鹏 阅读(1644)
评论(9) 编辑 收藏 网摘 所属分类:
小菜之智能客户端