0

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

我写了这段代码,但我被卡住了。

我的代码是:

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(sender, EventArgs.Empty) 处给出错误

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

谢谢你的回答,阿尔珀

4

2 回答 2

0

异常消息中清楚地说明了问题。t_Click 方法是共享的(这意味着类的所有实例都通用),因此它不能引发特定于类实例的事件。您应该只从未共享的方法中引发事件。

于 2009-12-08T15:53:26.353 回答
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
于 2011-05-23T15:48:35.053 回答