我将.NET 2.0 WinForms应用程序升级到.NET 4.5.2,并且在此代码块上收到两条警告:
Public Sub ShowFormAsMdiChild(ByVal newFormType As Type, _
ByVal mdiParentType As Type, _
Optional ByVal SearchID As String = "",
Optional ByVal curType As SearchType = SearchType.Residential)
If SearchID = "" Then
Dim F As Form
F = CType(Activator.CreateInstance(newFormType), Form)
F.MdiParent = CType(Activator.CreateInstance(mdiParentType), Form).ActiveForm
F.Show()
Else
Dim F As Form
Dim args(1) As Object
args(0) = SearchID
args(1) = curType
F = CType(Activator.CreateInstance(newFormType, args), Form)
F.MdiParent = CType(Activator.CreateInstance(mdiParentType), Form).ActiveForm
F.Show()
End If
End Sub
我在设置F.MdiParent的两行上收到以下警告:
访问共享成员,常量成员,枚举成员或嵌套类型 通过一个实例;不会评估合格表达。
问题是ActiveForm属性是共享属性,并且因为代码正在创建Form的新实例,所以调用Form的共享属性会导致警告。
有没有更好的方法在没有警告的情况下实施? VB.NET中的首选答案,但如果您只知道C#中的内容,我可以翻译。
答案 0 :(得分:0)
无需创建MDI父类型的新实例,然后将其强制转换为Form
。 ActiveForm
是Form
类的共享属性,因此您应该通过类名访问它,而不是通过实例访问它。
F.MdiParent = Form.ActiveForm