如何从不同的类访问 ThisAddIn 中的 VB.net 计时器

发布于 2024-11-26 21:59:48 字数 842 浏览 0 评论 0原文

我已经在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酸甜透明夹心 2024-12-03 21:59:48

您的代码不起作用,因为 myCaller 未设置为您的 ThisAddIn 的特定实例(理论上,可能有许多 ThisAddIn 实例浮动在您的记忆)。

但是,由于您正在开发 Office 加载项,因此实际上只有一个 ThisAddIn 实例。访问计时器的一个简单解决方法是将 Timer1 设为共享变量:

Friend Shared WithEvents Timer1 As System.Timers.Timer

这将允许您以 ThisAddIn.Timer1 的形式访问计时器。

(请注意,一般,可公开访问的共享字段(也称为全局变量)是一个坏主意。但是,就您的情况而言,它可能只是允许您执行您想要的操作的最简单的解决方案。 )

PS:您可能想了解类的实例之间的区别。


编辑:由于您有一个附加到计时器的事件处理程序,因此您

(a)也需要共享该事件处理程序(Private Shared Sub Timer1_Elapsed...) ,其缺点是您的事件处理程序无法再访问 ThisAddIn 的实例变量,或者

(b) 使 ThisAddIn 的实例通过单独的属性进行访问,而不是使字段共享:

Public Class ThisAddIn

    Friend WithEvents Timer1 As System.Timers.Timer

    Private Shared _instance As ThisAddIn
    Public ReadOnly Shared Property Instance() As ThisAddIn
        Get
            Return _instance
        End Get
    End Property

    Private Sub ThisAddIn_Startup() Handles Me.Startup
        _instance = Me

        Me.Timer1 = New System.Timers.Timer()
        Me.Timer1.Interval = 500
        Me.Timer1.Enabled = True
     End Sub

...

Public Class ServerRibbon
    Private Sub respondToClick()
        ThisAddIn.Instance.Timer1.Enabled = True
    End Sub
End Class

Your code doesn't work because myCaller is not set to your particular instance of ThisAddIn (theoretically, there could be many ThisAddIn 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:

Friend Shared WithEvents Timer1 As System.Timers.Timer

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:

Public Class ThisAddIn

    Friend WithEvents Timer1 As System.Timers.Timer

    Private Shared _instance As ThisAddIn
    Public ReadOnly Shared Property Instance() As ThisAddIn
        Get
            Return _instance
        End Get
    End Property

    Private Sub ThisAddIn_Startup() Handles Me.Startup
        _instance = Me

        Me.Timer1 = New System.Timers.Timer()
        Me.Timer1.Interval = 500
        Me.Timer1.Enabled = True
     End Sub

...

Public Class ServerRibbon
    Private Sub respondToClick()
        ThisAddIn.Instance.Timer1.Enabled = True
    End Sub
End Class
涙—继续流 2024-12-03 21:59:48

您可以使计时器本身共享,但更优雅的方法是将启动计时器的函数/子函数设为公共函数并调用它

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

孤君无依 2024-12-03 21:59:48

如果这对其他人有用,您还可以通过 Globals.ThisAddIn 以及项目中的其他位置访问实例化的 ThisAddIn

对象

Public Class ThisAddIn

    Public N As Integer = 42

    ...

End Class

Public Class MyClass

    Private Sub DoSomething()
        Dim myint= Globals.ThisAddIn.N
    End Sub
End Class

In case this is useful to anyone else, you can also access the instantiated ThisAddIn object via Globals.ThisAddIn

So,

Public Class ThisAddIn

    Public N As Integer = 42

    ...

End Class

and elsewhere in your project:

Public Class MyClass

    Private Sub DoSomething()
        Dim myint= Globals.ThisAddIn.N
    End Sub
End Class
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文