我創建了一個自定義類
Public Class MyFSW
Inherits FileSystemWatcher
Public Property ParentForm As Form
Public Property TabPage As TabPage
End Class
現在我想向此類添加一個自定義事件,當 FileSystemWatcher 的屬性“EnableRaisingEvents”更改時觸發?
有沒有機會做到這一點?
uj5u.com熱心網友回復:
該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
只要您通過 type 的參考設定屬性,這將起作用FileSystemWatcherEx。因為該屬性被隱藏而不是被覆寫,所以通過型別的參考設定它將FileSystemWatcher繞過派生屬性實作并且不會引發事件。你真的無能為力。
uj5u.com熱心網友回復:
作為“先前示例”的延續,您可以通過一些小的更改來實作這一點,如下例所示:
在此示例中,通過單擊按鈕更改屬性,但您可以根據自己的需要實作處理程式。
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:\Users\UserName\Desktop\Test\",
.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
uj5u.com熱心網友回復:
謝謝你!這就是我現在得到的(在我的類檔案中):
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 之類的東西,但沒有運氣。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/521933.html
