MaterialDesignInXAML / MaterialDesignInXamlToolkit

Google's Material Design in XAML & WPF, for C# & VB.Net.
http://materialdesigninxaml.net
MIT License
15.02k stars 3.41k forks source link

No loaded DialogHost instances when dialoghost is in root #2321

Closed jf-dynamics closed 3 years ago

jf-dynamics commented 3 years ago

I am trying to add a reusable dialog host to my test app. But unfortunately, the app always throws a 'No loaded DialogHost instances.' exception.

The code is very simple.

MainWindow.xaml

    <views:MvxWindow
        ...
        >
        <materialDesign:DialogHost Identifier="RootDialog" />
    </views:MvxWindow>

MainWindow.xaml.cs

    public partial class MainWindow : MvxWindow
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }

DashboardView.xaml

    <views:MvxWpfView
         ...
         >
         ...
         <Button Content="ShowDialog"
                 Click="OnClick_ShowDialogAsync" /> 
         ...
    </views:MvxWpfView>

DashboardView.xaml.cs

    public partial class DashboardView : MvxWpfView
    {
                public HomeView()
                {
                    InitializeComponent();
                }

                private async void OnClick_ShowDialogAsync(object sender, RoutedEventArgs e)
                {
                    var view = new SampleDialog
                    {
                        DataContext = new SampleDialogViewModel()
                    };

                     await DialogHost.Show(view, "RootDialog", ExtendedOpenedEventHandler, ExtendedClosingEventHandler);
                }

                ...
    }

When I put this line:

<materialDesign:DialogHost Identifier="RootDialog" />

inside the DashboardView.xaml it works fine.

jf-dynamics commented 3 years ago

please see sample app on this repo https://github.com/jf-dynamics/SampleWPF to reproduce the above issue.

Keboo commented 3 years ago

Hi @jf-dynamics the issue is with how MvvmCross works. When it runs it replaces any window content that you may have specified. If you check your Window's Content property at the end of the constructor, you will see that it is an instance of DashboardView and not the Grid like what you specified in MainWindow.xaml.