什么是VB.NET相当于本C#code的布线和声明的事件? [英] What's the VB.NET equivalent of this C# code for wiring up and declaring an event?

查看:110
本文介绍了什么是VB.NET相当于本C#code的布线和声明的事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个教程建立一个媒体播放器Silverlight中,我试图连线了一个事件处理程序 timer.Tick 事件一个 DispatchTimer 对象,使视频的时间同步与滑块对象。

样品code是在C#中,我不能为我的生活搞清楚VB.NET正确的语法与的RaiseEvent 和/或手柄要连接的事件。下面是有关C#code。我会包括在那里我被卡住的意见。

 私人DispatchTimer定时器;

公共页面()
{
    // ...
    定时器=新DispatchTimer();
    timer.Interval = TimeSpan.FromMilliseconds(50);
    timer.Tick + =新的EventHandler(timer_Tick); //< ==我被困在这里B / C
        //我不能这样做timer.Tick + = ......在VB.NET
}

无效timer_Tick(对象发件人,EventArgs的)
{
     如果(VideoElement.NaturalDuration.TimeSpan.TotalSeconds大于0)
     {
         sliderScrubber.Value = VideoElement.Position.TotalSeconds /
             VideoElement.NaturalDuration.TimeSpan.TotalSeconds;
     }
}
 

解决方案

这样的:

 的AddHandler timer.Tick,AddressOf timer_Tick
 

另外,

 私有WithEvents就定时器作为DispatcherTimer


子timer_Tick(发送者为对象,E作为EventArgs的)把手timer.Tick

结束小组
 

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;
     }
}

解决方案

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

这篇关于什么是VB.NET相当于本C#code的布线和声明的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆