【问题标题】:Raising event in custom control在自定义控件中引发事件
【发布时间】:2009-12-08 09:49:42
【问题描述】:

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

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

我的代码是:

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) 出现错误

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

感谢您的回答, 阿尔珀

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 好的,但是当我从 t_Click 中删除共享时,我无法使用以下代码将处理程序添加到超链接:x.AddHandler(Hyperlink.ClickEvent, New RoutedEventHandler(AddressOf t_Click))
    • 那是因为您的 InlineChanged Sub 也已共享,因此它无法访问不再共享的 t_Click。因此,您还应该从 InlineChanged 中删除 Shared 关键字。
    • 好的,但另一个问题:D 当我从 InlineChanged 中删除共享关键字时,在这一行出现错误 Public ReadOnly InlineCollectionProperty As DependencyProperty = DependencyProperty.Register("InlineCollection", GetType(String), GetType(CustomTextBlock) , New PropertyMetadata(New PropertyChangedCallback(AddressOf CustomTextBlock.InlineChanged))) 在这部分:AddressOf CustomTextBlock.InlineChanged 错误是:对非共享成员的引用需要对象引用。再次感谢:(
    • 如果不确切知道您要实现什么,就很难回答,但底线是您的事件和与此事件交互的函数都是共享的,或者两者都不共享。如果您不知道 Shared 意味着什么,请阅读有关该主题的信息 ;-)
    【解决方案2】:

    做这样的事情-

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-05
      • 1970-01-01
      • 2012-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多