【问题标题】:Add event to FileSystemWatcher when property EnableRaisingEvents changed属性 EnableRaisingEvents 更改时向 FileSystemWatcher 添加事件
【发布时间】:2022-10-27 19:46:23
【问题描述】:

我创建了一个自定义类

Public Class MyFSW
    Inherits FileSystemWatcher
    Public Property ParentForm As Form
    Public Property TabPage As TabPage
End Class

现在我想向此类添加一个自定义事件,当 FileSystemWatcher 的属性“EnableRaisingEvents”更改时触发?

有没有机会做到这一点?

【问题讨论】:

    标签: vb.net events filesystemwatcher


    【解决方案1】:

    EnableRaisingEvents 属性未声明为 Overridable,因此您无法覆盖它。你可以隐藏它:

    Public Class FileSystemWatcherEx
        Inherits FileSystemWatcher
    
        Public Property ParentForm As Form
        Public Property TabPage As TabPage
    
        Public Shadows Property EnableRaisingEvents As Boolean
            Get
                Return MyBase.EnableRaisingEvents
            End Get
            Set(value As Boolean)
                If MyBase.EnableRaisingEvents <> value Then
                    MyBase.EnableRaisingEvents = value
                    OnEnableRaisingEventsChanged(EventArgs.Empty)
                End If
            End Set
        End Property
    
        Public Event EnableRaisingEventsChanged As EventHandler
    
        Protected Overridable Sub OnEnableRaisingEventsChanged(e As EventArgs)
            RaiseEvent EnableRaisingEventsChanged(Me, e)
        End Sub
    
    End Class
    

    只要您通过FileSystemWatcherEx 类型的引用设置属性,这将起作用。因为该属性被隐藏而不是被覆盖,所以通过 FileSystemWatcher 类型的引用设置它将绕过派生属性实现并且不会引发事件。你真的无能为力。

    【讨论】:

      【解决方案2】:

      作为“先前示例”的延续,您可以通过一些小的更改来实现这一点,如下例所示:

      在此示例中,通过单击按钮更改属性,但您可以根据自己的需要实现处理程序。

      Option Strict On
      Imports System.IO
      
      
      Public Class Form1
          Private Fsw As CustomFSW
      
      
          Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
              CreateFSW()
          End Sub
      
          Sub CreateFSW()
      
              Fsw = New CustomFSW With {
                      .Name = "Tab1", 'Adding this you can specify the object is sending the event
                      .Path = "C:UsersUserNameDesktopTest",
                      .Filter = "*.*",
                      .IncludeSubdirectories = True,
                      .EnableRaisingEvents = True
                      }
      
      
              AddHandler Fsw.Created, AddressOf Fsw_Event
              AddHandler Fsw.Changed, AddressOf Fsw_Event
              AddHandler Fsw.Renamed, AddressOf Fsw_Event
              AddHandler Fsw.Deleted, AddressOf Fsw_Event
      
              'Here the handler of your custom event
              AddHandler Fsw.MyCutomEvent, AddressOf Fsw_CutomEvent
      
          End Sub
      
          Private Class CustomFSW
              Inherits FileSystemWatcher
      
              Private counter As Integer = 0
              Public Property Name As String
      
                'You can use the base proeprty insted of this for specific needs
              Private _EnableRaisingEvents As Boolean
              Public Overloads Property EnableRaisingEvents As Boolean
                  Get
                      Return _EnableRaisingEvents
                  End Get
                  Set(value As Boolean)
                      If Not value = _EnableRaisingEvents Then
                          counter += 1
                          RaiseEvent MyCutomEvent(Me, "Ciaooo, EnableRaisingEvents is changed " & counter.ToString & " times")
                      End If
                      _EnableRaisingEvents = value
                  End Set
              End Property
      
              'Rename this on your needs 
              Public Event MyCutomEvent(sender As Object, e As String)
      
      
          End Class
      
      
          Private Sub Fsw_CutomEvent(sender As Object, e As String)
              ' Do your stuf here 
              MsgBox(e)
          End Sub
      
      
          Private Sub Fsw_Event(sender As Object, e As FileSystemEventArgs)
              Dim FSW As CustomFSW = CType(sender, CustomFSW)
              If Not String.IsNullOrEmpty(FSW.Name) Then
                  Select Case FSW.Name
                      Case "Tab1"
                          'Do something
                          Debug.WriteLine("Event genarated from: " & FSW.Name)
      
                      Case "Tab2"
                          'Do something else 
                          Debug.WriteLine("Event genarated from: " & FSW.Name)
      
                  End Select
              End If
      
          End Sub
      
          Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
              If Fsw IsNot Nothing Then
                  Fsw.EnableRaisingEvents = Not Fsw.EnableRaisingEvents
              End If
          End Sub
      
      
      
      End Class
      

      【讨论】:

        【解决方案3】:

        谢谢你! 这就是我现在得到的(在我的类文件中):

        Public Class MyFSW
            Inherits FileSystemWatcher
            Public Property ParentForm As Form
            Public Property TabPage As TabPage
        
            Public Shadows Property EnableRaisingEvents As Boolean
                Get
                    Return MyBase.EnableRaisingEvents
                End Get
                Set(value As Boolean)
                    If MyBase.EnableRaisingEvents <> value Then
                        MyBase.EnableRaisingEvents = value
                        OnEnableRaisingEventsChanged(EventArgs.Empty)
                    End If
                End Set
            End Property
        
            Public Event EnableRaisingEventsChanged As EventHandler
        
            Protected Overridable Sub OnEnableRaisingEventsChanged(e As EventArgs)
                RaiseEvent EnableRaisingEventsChanged(Me, e)
            End Sub
        End Class
        

        在我的 Form-Class 中得到了这个:

        Sub CreateFSW()
            w.Watcher = New MyFSW With {
                    .ParentForm = f,
                    .TabPage = tp,
                    .Path = w.Path,
                    .Filter = w.Filter,
                    .IncludeSubdirectories = w.IncludeSubdirs,
                    .NotifyFilter = DirectCast(w.NotifyFilter, NotifyFilters)
                    }
        
            AddHandler w.Watcher.Created, AddressOf Fsw_EventRaise
            AddHandler w.Watcher.Changed, AddressOf Fsw_EventRaise
            AddHandler w.Watcher.Renamed, AddressOf Fsw_EventRaise
            AddHandler w.Watcher.Deleted, AddressOf Fsw_EventRaise
        
            AddHandler w.Watcher.EnableRaisingEventsChanged, AddressOf Fsw_Event
        End Sub
        
        Private Sub Fsw_Event(sender As Object, e As FileSystemEventArgs)
            MessageBox.Show("Works")
        End Sub
        

        编译器说:

        Option Strict On does not allow narrowing in implicit type conversions between method Fsw_Event(sender As Object, e As FileSystemEventArgs) and delegate Delegate Sub EventHandler(sender As Object, e As EventArgs)
        

        似乎有些类型不匹配。我试图将 FileSystemEventArgs 更改为 EventArgs 之类的东西,但没有运气。

        【讨论】:

        • 我想我明白了。更改了这一行:Public Shared Sub Fsw_EventOnOff(sender As Object, e As EventArgs)
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-08
        • 2014-07-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多