我在C#中有一个代码片段,我想将其转换为VB.Net
public class ModuleAViewOne : UserControl
{
public ModuleAViewOne(ModuleAViewOneViewModel model)
{
InitializeComponent();
this.Loaded += (s, e) =>
{
this.DataContext = model;
}
}
}
我用这种方式将其转换为VB.Net
Partial Public Class ModuleAViewOne
Inherits UserControl
Public Sub New(ByVal model As ModuleAViewOneViewModel)
InitializeComponent()
Me.model = model
AddHandler Loaded, AddressOf _Loaded
End Sub
Dim model As ModuleAViewOneViewModel
Private Sub _Loaded(ByVal s As Object, ByVal e As RoutedEventArgs)
DataContext = model
End Sub
End Class
但是可以在构造函数中编写事件,这样我就不必创建新的全局model
变量吗?
答案 0 :(得分:3)
您可以简单地使用lambda表达式
Public Sub New(model As ModuleAViewOneViewModel)
InitializeComponent()
AddHandler Loaded, Sub(s, e) DataContext = model
End Sub
或者,自VB 10.0(Visual Studio 2010附带)以来,您可以使用多行lambda:
Public Sub New(model As ModuleAViewOneViewModel)
InitializeComponent()
AddHandler Loaded, Sub(s, e)
DataContext = model
End Sub
End Sub