Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

.net - What's the VB.NET equivalent of this C# code for wiring up and declaring an event?

I'm working on a tutorial to build a media player in Silverlight and am trying to wire up an EventHandler to the timer.Tick event of a DispatchTimer object so that the time of the video is synced with a Slider object.

The sample code is in C# and I can't for the life of me figure out the proper syntax in VB.NET with RaiseEvent and/or Handles to wire up the event. Below is the relevant C# code. I'll include comments on where I'm getting stuck.

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;
     }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Like this:

AddHandler timer.Tick, AddressOf timer_Tick

Alternatively,

Private WithEvents timer as DispatcherTimer


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

End Sub

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...