MaterialDesignInXAML/MaterialDesignInXamlToolkit · error · ArgumentNullException

session

Error message

session

What it means

ArgumentNullException(nameof(session)) in DialogClosedEventArgs constructor (DialogClosedEventArgs.cs:7). The args object requires a live DialogSession because Parameter and consumers depend on it. Thrown only if internal/library code constructs DialogClosedEventArgs with a null session; not normally reachable by user code.

Source

Thrown at src/MaterialDesignThemes.Wpf/DialogClosedEventArgs.cs:7

namespace MaterialDesignThemes.Wpf;

public class DialogClosedEventArgs : RoutedEventArgs
{
    public DialogClosedEventArgs(DialogSession session, RoutedEvent routedEvent)
        : base(routedEvent)
        => Session = session ?? throw new ArgumentNullException(nameof(session));

    /// <summary>
    /// Gets the parameter originally provided to <see cref="DialogHost.CloseDialogCommand"/>/
    /// </summary>
    public object? Parameter => Session.CloseParameter;

    /// <summary>
    /// Allows interaction with the current dialog session.
    /// </summary>
    public DialogSession Session { get; }
}

View on GitHub (pinned to 98edec3a0b)

Solutions

  1. Always pass a valid DialogSession instance when constructing DialogClosedEventArgs; reuse DialogHost.CurrentSession if synthesizing one.
  2. If writing tests/mocks, instantiate DialogSession through an actual DialogHost rather than null.
  3. Prefer subscribing to the DialogClosed event instead of constructing the args yourself.
Defensive patterns

Strategy: validation

Validate before calling

DialogSession? session = dialogHost.CurrentSession;
if (session is null)
    throw new InvalidOperationException("Cannot build DialogClosedEventArgs without a session.");

var args = new DialogClosedEventArgs(session, DialogHost.DialogClosedEvent);

Type guard

static bool TryBuildClosedArgs(DialogHost host, out DialogClosedEventArgs args)
{
    args = null!;
    if (host.CurrentSession is null) return false;
    args = new DialogClosedEventArgs(host.CurrentSession, DialogHost.DialogClosedEvent);
    return true;
}

Prevention

When it happens

Trigger: Constructing 'new DialogClosedEventArgs(null, routedEvent)' directly, or a fork/custom build invoking the constructor without a session. In the shipped library, the host always passes CurrentSession.

Common situations: Custom subclasses/mocks of the event args; unit-test fakes that pass null; reflection-based instantiation.

Related errors


AI-assisted analysis of MaterialDesignInXAML/MaterialDesignInXamlToolkit@98edec3a0b (2026-08-13). Data as JSON: /api/errors/b592c72ce731b034. Report an issue: GitHub.