如何监听变量 属性 随外部程序变化

How to listen variable property changes with external program

我的程序有问题,它的核心使用一个 class 变量来连接、控制和从名为 PowerShape 的 CAD/CAM 软件中提取信息。

我想做的是听这个 class 变量来检测它的属性变化,如果你在 Powershape 中做一些事情就会发生这种变化。这些将包括活动的 window 或 Powershape 内部的模型更改。 class 变量在进行更改时正在更新,但我不知道如何检测它。

当声明 class 变量时,它连接到 Powershape,然后您可以访问它的属性:

Dim powershapeRoot As New PSAutomation(Delcam.ProductInterface.InstanceReuse.UseExistingInstance)
Dim PSmodelname = PowershapeRoot.activemodel.name

现在我想听变量 属性 "PowershapeRoot.activemodel.name" 看看它是否改变

如何操作?

要检测属性的更改,您可以使用 INotifyPropertyChanged 接口。

您将从 MSDN 中找到 here 文档。

在 属性 的 setter 中,您需要包含引发事件的代码。您可以在下面的 VB.NET 中找到示例:

Public Class Demo Implements INotifyPropertyChanged

    Private nameValue As String = String.Empty

    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

    Private Sub NotifyPropertyChanged(ByVal info As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
    End Sub

    Public Property name() As String
        Get
            Return Me.nameValue
    End Get

    'Raise the event in the setter
    Set(ByVal value As String)
        If Not (value = nameValue) Then
            Me.nameValue = value
            NotifyPropertyChanged("name")
        End If
    End Set
    End Property
End Class