WPF中自定义消息弹窗

WPF 自定义消息弹窗开发笔记

一、XAML 布局设计

文件:MessageInfo.xaml

xml 复制代码
<Window x:Class="AutoFeed.UserControls.MessageInfo"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:AutoFeed.UserControls"
        mc:Ignorable="d"
        Icon="/Source/bitbug_favicon.ico"          <!-- 窗口图标 -->
        Title="Message" Height="200" Width="350"    <!-- 标题、尺寸 -->
        WindowStartupLocation="CenterScreen">       <!-- 窗口居中 -->
    <Grid Background="#FF333333">                  <!-- 背景色(深灰色) -->
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>             <!-- 内容区域自动扩展 -->
            <RowDefinition Height="60"/>            <!-- 按钮区域固定高度 -->
        </Grid.RowDefinitions>

        <!-- 消息显示区域 -->
        <Grid Grid.Row="0">
            <TextBox 
                Margin="20"                         <!-- 内边距 -->
                Name="msg"                          <!-- 控件名称 -->
                TextWrapping="Wrap"                 <!-- 文本自动换行 -->
                FontSize="16"                       <!-- 字体大小 -->
                Foreground="White"                  <!-- 字体颜色 -->
                Background="Transparent"            <!-- 透明背景 -->
                BorderThickness="0"/>               <!-- 无边框 -->
        </Grid>

        <!-- 按钮区域 -->
        <StackPanel 
            Grid.Row="1" 
            HorizontalAlignment="Center"            <!-- 水平居中 -->
            VerticalAlignment="Center">             <!-- 垂直居中 -->
            <Button 
                Click="ok_click"                    <!-- 点击事件 -->
                Content="确定"                      <!-- 按钮文本 -->
                Style="{DynamicResource ccbtn}"/>    <!-- 引用样式资源 -->
        </StackPanel>
    </Grid>
</Window>
二、后台代码逻辑

文件:MessageInfo.xaml.cs

csharp 复制代码
using System.Windows;

namespace AutoFeed.UserControls
{
    /// <summary>
    /// 消息弹窗交互逻辑
    /// </summary>
    public partial class MessageInfo : Window
    {
        public MessageInfo()
        {
            InitializeComponent();
        }

        // 关闭窗口事件
        private void ok_click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        // 公共属性:设置消息内容
        public string Message
        {
            get => msg.Text?.ToString() ?? "";
            set => msg.Text = value;
        }
    }
}
三、静态帮助类封装(合并到后台代码)

文件:MessageInfo.xaml.cs(新增静态类)

csharp 复制代码
public static class MessageBoxHelper
{
    /// <summary>
    /// 显示消息弹窗(类似 System.Windows.MessageBox)
    /// </summary>
    /// <param name="message">消息内容</param>
    /// <param name="title">窗口标题(默认:"Message")</param>
    /// <returns>返回值(简化为 OK)</returns>
    public static MessageBoxResult Show(string message, string title = "Message")
    {
        var msgWindow = new MessageInfo
        {
            Title = title,                  // 设置窗口标题
            WindowStartupLocation = WindowStartupLocation.CenterScreen // 窗口居中
        };
        msgWindow.Message = message;      // 设置消息内容
        msgWindow.ShowDialog();           // 显示模态窗口
        return MessageBoxResult.OK;       // 固定返回值(可根据需求扩展)
    }
}
四、调用示例
csharp 复制代码
// 常规调用(显示错误消息)
try
{
    // 业务逻辑代码
}
catch (Exception ex)
{
    MessageBoxHelper.Show($"端口号格式无效: {ex.Message}", "错误提示");
}

// 简化调用(使用默认标题)
MessageBoxHelper.Show("操作成功!");
五、关键功能说明
功能 实现方式
自动换行 TextBox 中添加 TextWrapping="Wrap",文本超出宽度时自动换行。
字体颜色 通过 Foreground 属性设置,如 Foreground="White" 或十六进制色值 #FFFFFF
窗口居中 Window 中设置 WindowStartupLocation="CenterScreen"
模态窗口 使用 ShowDialog() 显示窗口,确保用户必须关闭窗口后才能操作父窗口。
样式复用 通过 Style="{DynamicResource ccbtn}" 引用资源字典中的按钮样式(需提前定义)。
相关推荐
yantuguiguziPGJ19 小时前
WPF 联合 Web 开发调试流程梳理(基于 Microsoft.Web.WebView2)
前端·microsoft·wpf
Aevget19 小时前
DevExpress WPF中文教程:Data Grid - 如何使用虚拟源?(二)
.net·wpf·界面控件·devexpress·ui开发·数据网格
大美B端工场-B端系统美颜师1 天前
工控软件开发选择难?Electron、Qt、WPF 对比
qt·electron·wpf
c#上位机1 天前
MefBootstrapper在Prism引导程序中的使用
c#·wpf·prism
没有bug.的程序员2 天前
服务治理与 API 网关:微服务流量管理的艺术
java·分布式·微服务·架构·wpf
Brianna Home2 天前
【案例实战】鸿蒙分布式调度:跨设备协同实战
华为·wpf·harmonyos
c#上位机3 天前
wpf中Grid的MouseDown 事件无法触发的原因
c#·wpf
△曉風殘月〆3 天前
如何在WPF中实现ComboBox多选
wpf
csdn_aspnet3 天前
如何使用现有工具进行 .NET 8 迁移 Wpf
wpf·.net 8
123梦野4 天前
WPF——动画
wpf