如何从不同的类访问 ThisAddIn 中的 VB.net 计时器
我已经在 Office 插件(在 VB.net 中开发)中设置了一个计时器,我可以使用以下代码将其设置得很好:
Public Class ThisAddIn
Friend WithEvents Timer1 As System.Timers.Timer
Private Sub ThisAddIn_Startup() Handles Me.Startup
Me.Timer1 = New System.Timers.Timer()
Me.Timer1.Interval = 500
Me.Timer1.Enabled = True
End Sub
Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
Me.Timer1.Enabled = False
MsgBox("Code Ran!")
End Sub
当用户更改插件功能区中的设置时,我想调用此计时器,但我不能似乎可以访问该事件。如果我使用代码:
Public Class ServerRibbon
Public myCaller As ThisAddIn
private sub respondToClick()
Dim blah As System.Timers.Timer
blah = myCaller.Timer1
blah.Enabled = True
end sub
我收到“对象引用未设置到对象实例”的错误。谁能解释一下如何设置计时器?
提前致谢,
I have set up a timer in an Office addin (being developed in VB.net) that I can set going fine, using the code:
Public Class ThisAddIn
Friend WithEvents Timer1 As System.Timers.Timer
Private Sub ThisAddIn_Startup() Handles Me.Startup
Me.Timer1 = New System.Timers.Timer()
Me.Timer1.Interval = 500
Me.Timer1.Enabled = True
End Sub
Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
Me.Timer1.Enabled = False
MsgBox("Code Ran!")
End Sub
I would like to call this timer when a user changes a setting in the addin's ribbon, but I cannot seem to access the event. If I use the code:
Public Class ServerRibbon
Public myCaller As ThisAddIn
private sub respondToClick()
Dim blah As System.Timers.Timer
blah = myCaller.Timer1
blah.Enabled = True
end sub
I get an error of 'object reference not set to an instance of an object'. Can anyone explain how I can set the timer going?
Thanks in advance,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的代码不起作用,因为
myCaller
未设置为您的ThisAddIn
的特定实例(理论上,可能有许多ThisAddIn
实例浮动在您的记忆)。但是,由于您正在开发 Office 加载项,因此实际上只有一个 ThisAddIn 实例。访问计时器的一个简单解决方法是将 Timer1 设为共享变量:
这将允许您以
ThisAddIn.Timer1
的形式访问计时器。(请注意,一般,可公开访问的共享字段(也称为全局变量)是一个坏主意。但是,就您的情况而言,它可能只是允许您执行您想要的操作的最简单的解决方案。 )
PS:您可能想了解类和类的实例之间的区别。
编辑:由于您有一个附加到计时器的事件处理程序,因此您
(a)也需要共享该事件处理程序(
Private Shared Sub Timer1_Elapsed...
) ,其缺点是您的事件处理程序无法再访问 ThisAddIn 的实例变量,或者(b) 使
ThisAddIn
的实例通过单独的属性进行访问,而不是使字段共享:Your code doesn't work because
myCaller
is not set to your particular instance ofThisAddIn
(theoretically, there could be manyThisAddIn
instances floating around your memory).However, since you are developing an office add-in, there is in fact only a single instance of ThisAddIn. A simple workaround to get access to the timer would be to make Timer1 a shared variable:
This would allow you to access the timer as
ThisAddIn.Timer1
.(Note that, in general, publicly accessible shared fields (aka global variables) are a bad idea. However, in your case, it might just be the simplest solution that allows you to do what you want.)
PS: You might want to read up on the difference between a class and an instance of a class.
EDIT: Since you have an event handler attached to the timer, you either
(a) need to make the event handler shared as well (
Private Shared Sub Timer1_Elapsed...
), which has the disadvantage that your event handler cannot access instance variables of ThisAddIn anymore, or(b) Make the instance of
ThisAddIn
accessible through a separate property instead of making the field shared:您可以使计时器本身共享,但更优雅的方法是将启动计时器的函数/子函数设为公共函数并调用它
You could make the Timer itself shared, but a more elegant way would be to make a function/sub thats starts the timer a public function and call this
如果这对其他人有用,您还可以通过 Globals.ThisAddIn 以及项目中的其他位置访问实例化的 ThisAddIn
对象
:
In case this is useful to anyone else, you can also access the instantiated ThisAddIn object via
Globals.ThisAddIn
So,
and elsewhere in your project: