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}" 引用资源字典中的按钮样式(需提前定义)。
相关推荐
mengge.cloud14 小时前
存储技术基础小白教程
linux·运维·服务器·wpf·存储
DreamLife☼1 天前
复杂Agent系统架构设计
系统架构·wpf·agent·组件·设计·构架·说明
SamChan901 天前
PDF翻译服务端到端基准测试:4款主流方案的吞吐量、延迟、内存占用对比
服务器·网络·python·ai·pdf·wpf
bugcome_com1 天前
Avalonia 控件模板实战:从 WPF 迁移自定义 Button 样式的完整指南
wpf
七夜zippoe1 天前
DolphinDB 故障排查实战:系统、数据库与集群常见问题诊断
数据库·wpf·集群·常见问题·故障排查·dolphindb
dalong102 天前
WPF:箭头绘制程序
wpf·自定义绘制
张工在路上3 天前
WPF 布局基础概述
大数据·hadoop·wpf
迪飞特科技4 天前
分布式事务下 Spring 声明式事务失效的 3 种修复方案
分布式·spring·wpf
2601_962174177 天前
Redis 简介
数据库·redis·wpf
SamChan908 天前
Python+PyMuPDF提取PDF表格并翻译:表格结构检测与双语重建实战
windows·python·ai·pdf·wpf