我需要一個完整的示例如何聲明..並在使用它之後定製路由事件。 其實我知道的語法,但我不知道如何使它的工作以及如何使用它 以後。你能給我簡單的完整例子嗎(VB代碼對我來說更好)。 例如,當點擊一個按鈕以在標籤上顯示文本時。自定義路由事件示例
1
A
回答
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應彈出可供選擇的命名空間列表。找到你的項目的基礎命名空間和插入:
這是一個簡單的採用了RoutedEvent
的,但我經常使用一個現實生活中的使用情況。我希望它有幫助。
相關問題
- 1. WPF自定義路由事件問題
- 2. 自定義路由事件失敗!
- 3. WPF路由事件,訂閱自定義事件
- 4. 自定義路由
- 5. WP7中的路由事件自定義控件
- 6. 綁定來自主視圖實例的路由器事件
- 7. jQuery的JTable中自定義點擊事件操作路由
- 8. Interactivity.EventTrigger不訂閱自定義路由事件
- 9. 處理隧道自定義路由事件
- 10. EmberJs:自定義事件中的路由器沒有收到
- 11. WPF XAML IntelliSense不自定義路由事件處理程序
- 12. WPF自定義路由事件 - 如何取消訂閱?
- 13. 如何創建自定義路由事件? WPF c#
- 14. WPF:自定義路由事件與額外的信息
- 15. WPF - 聲明一個自定義路由事件並收聽它
- 16. 如何從DataTemplate提取自定義的路由事件?
- 17. Rails-4自定義路由
- 18. 自定義路由在MVC3
- 19. Swashbuckle的自定義路由
- 20. MVC C#自定義路由
- 21. MVC 4:自定義路由
- 22. ASP.NET MVC4自定義路由
- 23. ASP.Net MVC4自定義路由
- 24. Symfony自定義路由
- 25. asp.net MVC自定義路由
- 26. 自定義路由Rails中
- 27. 瓶自定義路由
- 28. 自定義路由和i18n
- 29. Rails 4 - 自定義路由
- 30. ZF2自定義路由
'Button.Click'已經是'RoutedEvent'了。如果你想給一個'Label'元素指定一個文本值,只需要對該按鈕的'Click'事件作出響應......? – XamlZealot
其實我不想給一個Label分配一個文本值。我只需要一個完整的例子來說明如何聲明和使用它之後的自定義路由事件。 – apollon