什么是用于连接和声明事件的C#代码的VB.NET等价物?

时间:2010-05-26 01:20:17

标签: c# .net vb.net silverlight events

我正在使用tutorial to build a media player in Silverlight并尝试将EventHandler连接到timer.Tick对象的DispatchTimer事件,以便视频的时间为与Slider对象同步。

示例代码在C#中,我不能在我的生活中找出VB.NET中使用RaiseEvent和/或Handles来连接事件的正确语法。以下是相关的C#代码。我将对我遇到的问题进行评论。

private DispatchTimer timer;

public Page()
{
    //...
    timer = new DispatchTimer();
    timer.Interval = TimeSpan.FromMilliseconds(50);
    timer.Tick += new EventHandler(timer_Tick); // <== I get stuck here b/c
        // I can't do "timer.Tick += ..." in VB.NET
}

void timer_Tick(object sender, EventArgs e)
{
     if (VideoElement.NaturalDuration.TimeSpan.TotalSeconds > 0)
     {
         sliderScrubber.Value = VideoElement.Position.TotalSeconds /
             VideoElement.NaturalDuration.TimeSpan.TotalSeconds;
     }
}

1 个答案:

答案 0 :(得分:15)

像这样:

AddHandler timer.Tick, AddressOf timer_Tick

或者,

Private WithEvents timer as DispatcherTimer


Sub timer_Tick(sender As Object, e As EventArgs) Handles timer.Tick

End Sub