Event trigger function in the resource dictionary

I have a resdict.xaml file which is a resource dictionary

This file is linked in my window xaml file window.xaml.cs:

<Window.Resources>
   <ResourceDictionary Source="resdict.xaml" />
<Window.Resources>

Now, in window.xaml.cs, I also have this code for the storyboard:

<Storyboard Completed="HandlingEventHandler">
  ....some code...
</Storyboard>

When the storyboard is used and completes its launch, it calls HandlingEventHandler (). This is what I want.

Now we will move the storyboard code to the resdict.xaml resource dictionary file.

I can still use the storyboard perfectly, it does its thing, it plays the animation and all that, but HandlingEventHandler no longer starts. Why is this?

Is there a way to fix this without creating a partial class for the dictionary file?

For example, if I create a staticString HandlingEventHandler, I can do something like:

<Storyboard Completed="{Static: Myclass.HandlingEventHandler}">

?

+4
1

, .

, :

public class StoryBoardBehaviour : DependencyObject
{
    public static readonly DependencyProperty AttachCompletedHandlerProperty =
        DependencyProperty.Register("AttachCompletedHandler", typeof(bool), typeof(StoryBoardBehaviour), new PropertyMetadata(false, AttachCompletedHandlerChanged));

    public static void SetAttachCompletedHandler(DependencyObject obj, bool value)
    {
        obj.SetValue(AttachCompletedHandlerProperty, value);
    }

    public static bool GetAttachCompletedHandler(DependencyObject obj)
    {
        return (bool)obj.GetValue(AttachCompletedHandlerProperty);
    }

    private static void AttachCompletedHandlerChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var storyboard = (Storyboard)d;
        var oldValue = (bool)e.OldValue;
        var newValue = (bool)e.NewValue;

        if (newValue && !oldValue)
        {
            storyboard.Completed += StoryboardOnCompleted;
        }

        if (!newValue && oldValue)
        {
            storyboard.Completed -= StoryboardOnCompleted;
        }
    }

    private static void StoryboardOnCompleted(object sender, object o)
    {
        // Completed handler logic
    }
}

:

<Storyboard behaviours:StoryBoardBehaviour.AttachCompletedHandler="True">

0

Source: https://habr.com/ru/post/1539316/


All Articles