在自定义控件中引发事件

时间:2009-12-08 09:49:42

标签: wpf vb.net events custom-controls textblock

我正在编写一个自定义文本块控件,用于填充超链接并在单击超链接时引发事件。

我写了这段代码但是我被困了。

我的代码是:

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)中出错

错误是:如果没有类的expliticit实例,则无法从共享方法或共享成员初始化程序中引用类的实例成员。

感谢您的回答, 阿尔珀

2 个答案:

答案 0 :(得分:0)

问题在异常消息中明确说明。 t_Click方法是Shared(这对于类的所有实例都是通用的),因此它不能引发特定于类实例的Event。您应该只从未共享的方法中引发事件。

答案 1 :(得分:0)

做这样的事情 -

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