我有这段代码可行:
'Calls after changing Thread.CurrentThread.CurrentUICulture
'and Threading.Thread.CurrentThread.CurrentCulture
'MainPagesBaseForm.vb
Imports System.ComponentModel
Protected Overridable Sub ChangeUILang()
Dim t As Type = Me.GetType
Dim resMgr As ComponentResourceManager
While (Not t.Equals(GetType(Form)))
resMgr = New ComponentResourceManager(t)
Try
ChangeUILangWork(Me, resMgr)
Catch ex As Resources.MissingManifestResourceException
'resource file doesn't exist, skip
End Try
t = t.BaseType
End While
End Sub
Private Sub ChangeUILangWork(ctr As Control, resMgr As ComponentResourceManager)
For Each ctrUnit As Control In ctr.Controls
If ctrUnit.Visible Then
'Change Lang for the control
resMgr.ApplyResources(ctrUnit, ctrUnit.Name, _
Thread.CurrentThread.CurrentUICulture)
'override visiblity in designer. Already checked to be visible
ctrUnit.Visible = True
'For image based controls
If (TypeOf ctrUnit Is ImageBaseControlI) Then
DirectCast(ctrUnit, ImageBaseControlI).ChangeLang()
End If
'Recursion
If ctrUnit.HasChildren Then
If TypeOf ctrUnit Is UserControl Then
'Look into its own .resx for user controls
Dim resMgr2 As New ComponentResourceManager(ctrUnit.GetType)
ChangeUILangWork(ctrUnit, resMgr2)
Else
ChangeUILangWork(ctrUnit, resMgr)
End If
'For panels with custom paint
ctrUnit.Invalidate()
End If
End If
Next ctrUnit
End Sub
有没有正确的方法在运行时更改UI语言,包括用户控件和内联控件,而不是将所有资源文件应用于所有这样的元素?控件不应该知道他们的本地化数据在哪里吗?
无论如何都要检查资源文件是否存在?如果可能的话,我想避免空尝试。
背景:
嗨,我有一个需要在运行时更改其UI语言的WinForm应用程序。表格的结构如下:
'This defines the layout of the main pages
Public Class MainPageBaseForm
Inherits Form
Protected LblHeader As Label 'Added and localized from designer
'...
End Class
'This defines common event handlers for pages related to books
'No localization file for this class
Public Class BooksBaseForm
Inherits MainPageBaseForm
Event BookStatusChanged(sender As Object, e As BookStatusEventArgs)
'...
End Class
'Individual pages / forms
Public Class FrmBooks001
Inherits BooksBaseForm
Protected LblDesc As Label 'Localized Label added from designer
Protected SearchKeysInputFields As BooksEnqInForm 'Localized user control
Protected BntChLang As Button 'Button to switch UI culture
'Other controls like textboxes & buttons for this page
'...
End Class
点击切换文化按钮后,应刷新表单Controls
。我找到了适用于简单表单的方式(How do I change the culture of a WinForms application at runtime):
Private Sub ChangeUILang(parentCtr As Control)
Dim resMgr As New System.ComponentModel.ComponentResourceManager(GetType(FrmBooks001))
For Each ctr As Control In parentCtr.Controls
resources.ApplyResources(ctr, ctr.Name, New CultureInfo("zh"))
if ctr.HasChildren then
ChangeUILang(ctr)
end if
Next
End Sub
但是这只适用于在FrmBooks001中直接定义的控件,而且只适用于vanilla控件(不是UserControl
)。我从这篇文章中找出原因:
Switching language (cultureinfo/globalization) does not affect ToolStripMenuItems
您正在System.Windows.Forms.ToolStripMenuItem.resx文件中查找本地化信息,但您想查看Forms资源文件。
(这里的情况与UserControls有自己的.resx文件相反)
所以唯一的解决方案(我能想到)是遍历继承树和UserControls中所有类的资源文件(并冗余地应用!)。对于这篇长篇文章感到抱歉,请查看我目前使用的顶部和问题。提前谢谢。