You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WPF C#中Material Design MessageBox实现问题:DialogHost报错无加载实例

解决Material Design In XAML Toolkit中DialogHost报错"No loaded DialogHost instances"的问题

嘿,这个报错我之前也踩过坑!本质原因很直白:你在代码里调用DialogHost.Show(sampleMessageDialog, "RootDialog")时,指定了要找名为"RootDialog"的DialogHost,但你的WPF界面里根本没加载这个控件,工具包找不到它自然就抛出错误了。

下面给你一步步捋清楚解决方法:

1. 先在XAML中添加DialogHost控件

你需要把DialogHost作为界面的根容器(或者至少是能覆盖需要弹出对话框区域的容器),并且设置它的Identifier属性和代码里用的"RootDialog"完全一致。比如把原来的窗口内容嵌套在DialogHost里:

<Window x:Class="YourNamespace.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
        Title="MainWindow" Height="450" Width="800">
    <!-- 这里添加DialogHost,Identifier和代码里的匹配 -->
    <materialDesign:DialogHost Identifier="RootDialog">
        <!-- 你的原有界面内容,比如触发对话框的按钮 -->
        <Grid>
            <materialDesign:PopupButton x:Name="MenuPopupButton" 
                                        Click="MenuPopupButton_OnClick" 
                                        Content="打开Material风格对话框"/>
        </Grid>
    </materialDesign:DialogHost>
</Window>

2. 确保你的SampleMessageDialog正确实现

SampleMessageDialog需要是一个继承自UserControl的控件,才能被DialogHost正确加载展示。比如它的XAML可以这么写:

<UserControl x:Class="YourNamespace.SampleMessageDialog"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
             d:DesignHeight="200" d:DesignWidth="300">
    <materialDesign:Card Padding="24" Width="300">
        <StackPanel>
            <TextBlock x:Name="Message" FontSize="18" Margin="0,0,0,16"/>
            <Button Content="确定" 
                    HorizontalAlignment="Right"
                    Click="CloseButton_Click"
                    Style="{StaticResource MaterialDesignRaisedButton}"/>
        </StackPanel>
    </materialDesign:Card>
</UserControl>

然后在它的后台代码里添加关闭对话框的逻辑,让用户点击确定就能正常关闭:

private void CloseButton_Click(object sender, RoutedEventArgs e)
{
    // 调用DialogHost的内置关闭命令
    MaterialDesignThemes.Wpf.DialogHost.CloseDialogCommand.Execute(null, this);
}

3. 额外的检查项

  • 确认你已经正确安装了MaterialDesignThemes和MaterialDesignColors这两个NuGet包,版本最好保持一致;
  • 在App.xaml里要合并Material Design的资源,不然控件会没有样式:
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <materialDesign:BundledTheme BaseTheme="Light" PrimaryColor="DeepPurple" SecondaryColor="Teal"/>
            <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>
  • 不要把DialogHost放在Visibility="Collapsed"或者还没加载到可视化树的控件里,不然工具包还是找不到它。

这样改完之后,你再点击按钮,应该就能正常弹出Material Design风格的对话框啦!

内容的提问来源于stack exchange,提问作者Abdelrahman Gamal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 07:55:55