Skip to content

Snackbar not working when hosting WPF within a Winform. #946

Description

@ChrisLawford

I am hosting the WPF user control containing the Snackbar using an ElementHost within a Winform. I have created the following example to provide a way to easily see the issue and the following fix that I used to solve the issue but this is very much a quick fix and not a good way of doing things.

WinFormsHostingTestApplication

To replicate the problem:

  • Download the repo
  • Add Reference Material Design (We will be changing code)
  • Run the program
  • Click "Add WPF Control"
  • Click "This is a WPF control"
    Desired outcome: The snackbar shows at this point with a message.

The Culprit:

Running the project with material design referenced you shall see that the Snackbar doesnt receive any messages when you click the button.

private DispatcherOperation<Snackbar> FindSnackbar(Dispatcher dispatcher)
{
    return dispatcher.InvokeAsync(() =>
    {
        return _pairedSnackbars.FirstOrDefault(sb =>
        {
            if (!sb.IsLoaded || sb.Visibility != Visibility.Visible) return false;
            var window = Window.GetWindow(sb);
            return window != null && window.WindowState != WindowState.Minimized;
        });
    });
}

My quick fix is to change this to:

private DispatcherOperation<Snackbar> FindSnackbar(Dispatcher dispatcher)
{
    return dispatcher.InvokeAsync(() =>
    {
        return _pairedSnackbars.FirstOrDefault(sb =>
        {
            if (!sb.IsLoaded || sb.Visibility != Visibility.Visible) return false;
            //var window = Window.GetWindow(sb);
            //return window != null && window.WindowState != WindowState.Minimized;
            return true;
        });
    });
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions