Wpf 在自定义控件中引发事件
我正在编写一个自定义textblock控件,用于填充超链接,并在单击超链接时引发事件 我写了这段代码,但我被绊倒了 我的代码是:Wpf 在自定义控件中引发事件,wpf,vb.net,events,custom-controls,textblock,Wpf,Vb.net,Events,Custom Controls,Textblock,我正在编写一个自定义textblock控件,用于填充超链接,并在单击超链接时引发事件 我写了这段代码,但我被绊倒了 我的代码是: Imports System.Text.RegularExpressions Public Class CustomTextBlock Inherits TextBlock Public Event Klik As EventHandler(Of EventArgs) Public ReadOnly InlineCollectionProperty As Depen
Imports System.Text.RegularExpressions
Public Class CustomTextBlock
Inherits TextBlock
Public Event Klik As EventHandler(Of EventArgs)
Public ReadOnly InlineCollectionProperty As DependencyProperty = DependencyProperty.Register("InlineCollection", GetType(String), GetType(CustomTextBlock), New PropertyMetadata(New PropertyChangedCallback(AddressOf CustomTextBlock.InlineChanged)))
Private Shared Sub InlineChanged(ByVal sender As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
DirectCast(sender, CustomTextBlock).Inlines.Clear()
Dim kelimeler = Split(e.NewValue, " ")
For i = 0 To kelimeler.Length - 1
If Regex.Match(kelimeler(i), "(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?").Success Then
Dim x = New Hyperlink(New Run(kelimeler(i)))
x.AddHandler(Hyperlink.ClickEvent, New RoutedEventHandler(AddressOf t_Click))
x.ToolTip = kelimeler(i)
x.Tag = kelimeler(i)
DirectCast(sender, CustomTextBlock).Inlines.Add(x)
If Not i = kelimeler.Length Then DirectCast(sender, CustomTextBlock).Inlines.Add(" ")
Else
DirectCast(sender, CustomTextBlock).Inlines.Add(kelimeler(i))
If Not i = kelimeler.Length Then DirectCast(sender, CustomTextBlock).Inlines.Add(" ")
End If
''//Console.WriteLine(kelime(i).ToString.StartsWith("@"))
Next
kelimeler = Nothing
End Sub
Public Property InlineCollection As String
Get
Return DirectCast(GetValue(InlineCollectionProperty), String)
End Get
Set(ByVal value As String)
SetValue(InlineCollectionProperty, value)
End Set
End Property
Private Shared Sub t_Click(ByVal sender As Hyperlink, ByVal e As System.Windows.RoutedEventArgs)
e.Handled = True
RaiseEvent Klik(sender, EventArgs.Empty)
End Sub
End Class
此代码在RaiseEvent Klik(发送方,EventArgs.Empty)处出错
错误是:在没有类的显式实例的情况下,无法从共享方法或共享成员初始值设定项中引用类的实例成员
谢谢你的回答,
Alper异常消息中明确说明了问题。t_Click方法是共享的(这意味着该类的所有实例都是公共的),因此它不能引发特定于该类实例的事件。您应该只从未共享的方法引发事件。执行类似操作-
Imports System
Imports System.Text.RegularExpressions
Public Class CustomTextBlock
Inherits TextBlock
Public Event Klik As EventHandler(Of System.EventArgs)
Public ReadOnly InlineCollectionProperty As DependencyProperty = DependencyProperty.Register("InlineCollection", GetType(String), GetType(CustomTextBlock), New PropertyMetadata(New PropertyChangedCallback(AddressOf CustomTextBlock.InlineChanged)))
Private Shared Sub InlineChanged(ByVal sender As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
Dim d As CustomTextBlock = DirectCast(sender, CustomTextBlock)
d.Inlines.Clear()
d.OnInlineChanged(CType(e.NewValue, String))
End Sub
Private Sub OnInlineChanged(ByVal Value As String)
Dim kelimeler = Split(Value, " ")
For i As Integer = 0 To kelimeler.Length - 1
If Regex.Match(kelimeler(i), "(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?").Success Then
Dim x = New Hyperlink(New Run(kelimeler(i)))
x.AddHandler(Hyperlink.ClickEvent, New RoutedEventHandler(AddressOf t_Click))
x.ToolTip = kelimeler(i)
x.Tag = kelimeler(i)
Me.Inlines.Add(x)
If Not i = kelimeler.Length Then Me.Inlines.Add(" ")
Else
Me.Inlines.Add(kelimeler(i))
If Not i = kelimeler.Length Then Me.Inlines.Add(" ")
End If
''//Console.WriteLine(kelime(i).ToString.StartsWith("@"))
Next
kelimeler = Nothing
End Sub
Public Property InlineCollection As String
Get
Return DirectCast(GetValue(InlineCollectionProperty), String)
End Get
Set(ByVal value As String)
SetValue(InlineCollectionProperty, value)
End Set
End Property
Private Sub t_Click(ByVal sender As Hyperlink, ByVal e As System.Windows.RoutedEventArgs)
e.Handled = True
RaiseEvent Klik(sender, System.EventArgs.Empty)
End Sub
End Class
好的,但当我从t_Click中删除shared时,我无法使用以下代码将处理程序添加到超链接:x.AddHandler(hyperlink.ClickEvent,New RoutedEventHandler(t_Click的地址)),这是因为您的InlineChanged Sub也是共享的,所以它无法访问不再共享的t_Click。因此,您还应该从InlineChanged中删除共享关键字。好的,但另一个问题是:当我从InlineChanged中删除共享关键字时,在此行获取错误公共只读InlineCollectionProperty作为DependencyProperty=DependencyProperty.Register(“InlineCollection”、GetType(String)、GetType(CustomTextBlock),此部分的新PropertyMetadata(新PropertyChangedCallback(AddressOf CustomTextBlock.InlineChanged)):AddressOf CustomTextBlock.InlineChanged错误是:对非共享成员的引用需要对象引用。再次感谢:(如果不知道您想要实现什么,很难回答,但底线是您的事件和与此事件交互的函数都是共享的,或者两者都不是共享的。如果您不知道共享意味着什么,请阅读主题;-)