2012-11-08 63 views
1

我需要一個完整的示例如何聲明..並在使用它之後定製路由事件。 其實我知道的語法,但我不知道如何使它的工作以及如何使用它 以後。你能給我簡單的完整例子嗎(VB代碼對我來說更好)。 例如,當點擊一個按鈕以在標籤上顯示文本時。自定義路由事件示例

+0

'Button.Click'已經是'RoutedEvent'了。如果你想給一個'Label'元素指定一個文本值,只需要對該按鈕的'Click'事件作出響應......? – XamlZealot

+0

其實我不想給一個Label分配一個文本值。我只需要一個完整的例子來說明如何聲明和使用它之後的自定義路由事件。 – apollon

回答

0

這裏是我使用的自定義RoutedEvent一個簡單的方法:要觸發時的TextBlock變化(TextChanged事件)的Text屬性動畫:

在這種情況下我已經創建了從0​​派生的類。爲了演示的目的,我們將其稱爲MyCustomTextBlock

首先我們定義了RoutedEvent

Public Shared ReadOnly TextChangedEvent As RoutedEvent = EventManager.RegisterRoutedEvent("TextChanged", RoutingStrategy.Bubble, GetType(RoutedEventHandler), GetType(MyCustomTextBlock)) 

我們定義RoutedEeventHandler:

Public Custom Event TextChanged As RoutedEventHandler 

    AddHandler(ByVal value As RoutedEventHandler) 
     Me.AddHandler(TextChangedEvent, value) 
    End AddHandler 

    RemoveHandler(ByVal value As RoutedEventHandler) 
     Me.RemoveHandler(TextChangedEvent, value) 
    End RemoveHandler 

    RaiseEvent(ByVal sender As Object, ByVal e As RoutedEventArgs) 
     Me.RaiseEvent(e) 
    End RaiseEvent 

End Event 

接下來,陰影Text財產申報,以便您可以指定一個回調方法:

Public Shared Shadows TextProperty As DependencyProperty = DependencyProperty.Register("Text", GetType(String), MethodBase.GetCurrentMethod().DeclaringType, New FrameworkPropertyMetadata(String.Empty, New PropertyChangedCallback(AddressOf TextPropertyChanged))) 

編輯:在上面的DependencyProperty註冊,我使用Reflection獲得調用類型,因爲我使用代碼片段來注入DependencyProperty註冊調用,它使我的代碼段更加動態。你可以用下面的代碼替換上面的調用,如果你不喜歡導入Reflection Namespace

Public Shared Shadows TextProperty As DependencyProperty = DependencyProperty.Register("Text", GetType(String), GetType(MyCustomTextBlock), New FrameworkPropertyMetadata(String.Empty, New PropertyChangedCallback(AddressOf TextPropertyChanged))) 

定義將執行每當Text價值變動回調方法,提高RoutedEvent

Private Shared Sub TextPropertyChanged(ByVal Sender As Object, ByVal e As DependencyPropertyChangedEventArgs) 
    DirectCast(Sender, MyCustomTextBlock).RaiseEvent(New RoutedEventArgs(MyCustomTextBlock.TextChangedEvent)) 
End Sub 

那是所有的代碼,現在讓我們在XAML中使用它:

<local:MyCustomTextBlock> 
    <local:MyCustomTextBlock.Style> 
     <Style TargetType="local:MyCustomTextBlock"> 
      <Style.Triggers> 
       <EventTrigger RoutedEvent="local:MyCustomTextBlock.TextChanged"> 
        <BeginStoryboard> 
         <Storyboard> 
          <DoubleAnimation To="1.5" Duration="0:0:0.1" Storyboard.TargetProperty="(FrameworkElement.RenderTransform).(TransformGroup.Children)[1].(ScaleTransform.ScaleX)" AutoReverse="True"/> 
          <DoubleAnimation To="1.5" Duration="0:0:0.1" Storyboard.TargetProperty="(FrameworkElement.RenderTransform).(TransformGroup.Children)[1].(ScaleTransform.ScaleY)" AutoReverse="True"/> 
         </Storyboard> 
        </BeginStoryboard> 
       </EventTrigger> 
      </Style.Triggers> 
     </Style> 
    </local:MyCustomTextBlock.Style> 
</local:MyCustomTextBlock> 

在上面的觸發器中,我只是sca將尺寸縮小爲TextBlock,然後再向下,所有這些都在200毫秒內,這給用戶一種微妙的手勢,文字已經改變,將他們的注意力引向新的價值。

另外,如果您還沒有準備好,在你的XAML頁面的頂部引用您的裝配:

<Window x:Class="MyWindow" 
    xmlns:local="clr-namespace:MyRoutedEventProject"/> 

對於純樸的緣故,讓VisualStudio的鉤它給你的。如果您輸入xmlns:local=,則Intellisense應彈出可供選擇的命名空間列表。找到你的項目的基礎命名空間和插入:

enter image description here

這是一個簡單的採用了RoutedEvent的,但我經常使用一個現實生活中的使用情況。我希望它有幫助。