具体的C#转VB问题

时间:2011-01-26 09:41:03

标签: c# .net vb.net events c#-to-vb.net

我正在尝试将以下C#代码转换为VB。我试过的在线转换页面的结果对我来说没有意义,VS2010标志着它们有缺陷。我对有关事件的有限C#知识还不足以解决这个问题......

MVVM示例使用此接口:

public interface IRequestCloseViewModel
{
  event EventHandler RequestClose
} 

它在这个基类中使用:

public class ApplicationWindowBase : Window
{
    public ApplicationWindowBase()
    {
        this.DataContextChanged += new DependencyPropertyChangedEventHandler(this.OnDataContextChanged);
    }

    private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        if (e.NewValue is IRequestCloseViewModel)
        {
              // if the new datacontext supports the IRequestCloseViewModel we can use
              // the event to be notified when the associated viewmodel wants to close
              // the window
             ((IRequestCloseViewModel)e.NewValue).RequestClose += (s, e) => this.Close();
        }
    }
} 

正确的VB .NET翻译是什么样的?

1 个答案:

答案 0 :(得分:2)

这应该适合你:

Public Interface IRequestCloseViewModel

    //Event RequestClose As EventHandler
    Event RequestClose(ByVal sender As Object, ByVal e As EventArgs)

End Interface

Public Class ApplicationWindowBase
    Inherits Window

    Public Sub New()
        AddHandler Me.DataContextChanged, AddressOf OnDataContextChanged
    End Sub

    Private Sub OnDataContextChanged(ByVal sender As Object, ByVal e As DependencyPropertyChangedEventArgs)

        Dim request = TryCast(e.NewValue, IRequestCloseViewModel)

        If request IsNot Nothing Then
            AddHandler request.RequestClose, Sub(sender, event) Me.Close
            //Bear in mind you cannot do Sub(x,y) in VS pre 2010    
        End If

    End Sub

End Class