Skip to content

DialogHost IsLoaded but not exactly #3804

Open

Description

@jbooth88

Bug explanation

I've uploaded a sample project illustrating the issue.

When trying to get a WPF window to open a view using DialogHost on initial startup/show, I noticed that the DialogHost.Show method throws an exception even though the DialogHost instance IsLoaded property was true. I am thinking the DialogHost is loaded but not yet registered in the LoadedInstances HashSet by the time the Window.IsLoaded event triggers.

https://github.com/jbooth88/DialogHostStartupExample

Looks like IsLoaded property is coming from FrameworkElement, so my guess here is that it's not the correct value to identify whether or not the DialogHost is fully initialized. I am not sure of the best way to solve this problem.

Version

5.2.1

Activity

  1. corvinsz commented on Mar 11, 2025

    @corvinsz
    Member

    FYI: As far as I can tell the problem is that the Show method is called before the Loaded event of the DialogHost is raised. Therefore the dialoghost instance has not yet been added to the LoadedInstances hashset.

    Executing the logic in the Onloaded method when the Initialized event is raised fixes this problem. I am however not confident that this is a suitable fix, because the DialogHost is quite a large class and it could possibly break something else.

    public DialogHost()
    {
    -   Loaded += OnLoaded;
    +   Initialized += OnLoaded;
        Unloaded += OnUnloaded;
    
        CommandBindings.Add(new CommandBinding(CloseDialogCommand, CloseDialogHandler, CloseDialogCanExecute));
        CommandBindings.Add(new CommandBinding(OpenDialogCommand, OpenDialogHandler));
    }

    Edit: @jbooth88 if applicable and you need a quick & dirty fix you can just listen for the Loaded event on the DialogHost like so:

    public MainWindow()
    {
        InitializeComponent();
        myDialogHost.Loaded += MyDialogHost_Loaded;
    }
    private async void MyDialogHost_Loaded(object sender, RoutedEventArgs e) => await DialogHost.Show("test content", "RootDialog");
  2. jbooth88 commented on Mar 11, 2025

    @jbooth88
    ContributorAuthor

    That is covered in "Scenario 3" in the sample project I provided. It works only if the DialogHost was not already loaded/initialized prior to the window constructor call (which I admit won't be a problem for most users).

  3. AmeerMansour commented on Mar 29, 2025

    @AmeerMansour

    @corvinsz
    can you please help me and check my issue as i am not expert in material design toolkit structure ?
    #3824

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugevaluation requiredItems is pending review or evaluation by the team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions