Avi
Avi

Reputation: 16176

Null "Sender" in WPF XAML Event

I read here that Sender should never be null in an event handler (Konrad Rudolph's answer to "Best way to handle a NULL").

My event handler, however, gets a null sender from WPF XAML.

This is the XAML:

<Storyboard x:Name="Storyboard"  CurrentTimeInvalidated="StoryboardChanged">

And in the main window, this is the event handerl:

private void StoryboardChanged(object sender, EventArgs e)
{
        try
        {
            #region VerifyInputs

            Validator.Verify(sender); <------------------- aborts on null

So, my question is: How should I fix my XAML so that WPF sends a non-null "sender" value?

Edit: Appologies for clouding the issue with my thoughtless copy/paste of Validator.Verify. This method just checks to see whether the argument is null:

    public static void Verify(Object theObj)
    {
        if (theObj == null)
        {
            string errMsg = "theObj is null";
            Debug.Assert(theObj != null, errMsg);
            throw new ApplicationException(errMsg);
        }
    }

edit:

Abort, Abort, Abort

Sorry. Sender was not null, e was

:(

Upvotes: 0

Views: 877

Answers (1)

Steve Py
Steve Py

Reputation: 34793

I think the issue may be what your Validator class is expecting the sender to be. For the CurrentTimeInvalidated event handler, the "sender" is not a Storyboard, it will be a System.Media.Animation.Clock. If your validator verify method is expecting something else, it will be treated as #null.

(I.e. if the signature is something like Verify(object sender) and your implementation does something like "sender as Storyboard", it will end up with a null value.)

Upvotes: 1

Related Questions