1

宣言する方法の完全な例が必要です..そしてそれをカスタムルーティングイベントとして使用した後。実際、構文は知っていますが、それを機能させる方法と後で使用する方法がわかりません。簡単で完全な例を教えてください(vbコードの方が適しています)。たとえば、ボタンをクリックしてラベルにテキストを表示する場合などです。

4

1 に答える 1

0

custom を使用した簡単な方法を次に示します。プロパティが変更さRoutedEventれるたびにアニメーションをトリガーするには (TextChanged イベント):TextTextBlock

この例では、から派生するクラスを作成しましたTextBlock。このデモンストレーションでは、これを と呼びます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

次に、TextCallback メソッドを指定できるように、プロパティ宣言を SHADOW します。

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

EDIT:上記の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>

上記のトリガーでは、単純に のサイズを拡大してから縮小していTextBlockます。これはすべて 200 ミリ秒以内に行われます。これにより、テキストが変更されたという微妙なジェスチャがユーザーに与えられ、新しい値に注意が向けられます。

また、まだ参照していない場合は、xaml ページの上部でアセンブリを参照してください。

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

簡単にするために、VisualStudio に接続させてください。入力するとxmlns:local=、Intellisense によって選択する名前空間のリストがポップアップ表示されます。プロジェクトのベース名前空間を見つけて挿入します。

ここに画像の説明を入力

これは の単純な使用方法ですが、RoutedEvent私が頻繁に使用する実際の使用例です。お役に立てば幸いです。

于 2012-11-09T14:02:37.967 に答える