WPF中自定义消息弹窗

发布于:2025-05-31 ⋅ 阅读:(23) ⋅ 点赞:(0)

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

一、XAML 布局设计

文件:MessageInfo.xaml

<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

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(新增静态类)

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;       // 固定返回值(可根据需求扩展)
    }
}
四、调用示例
// 常规调用(显示错误消息)
try
{
    // 业务逻辑代码
}
catch (Exception ex)
{
    MessageBoxHelper.Show($"端口号格式无效: {ex.Message}", "错误提示");
}

// 简化调用(使用默认标题)
MessageBoxHelper.Show("操作成功!");
五、关键功能说明
功能 实现方式
自动换行 TextBox 中添加 TextWrapping="Wrap",文本超出宽度时自动换行。
字体颜色 通过 Foreground 属性设置,如 Foreground="White" 或十六进制色值 #FFFFFF
窗口居中 Window 中设置 WindowStartupLocation="CenterScreen"
模态窗口 使用 ShowDialog() 显示窗口,确保用户必须关闭窗口后才能操作父窗口。
样式复用 通过 Style="{DynamicResource ccbtn}" 引用资源字典中的按钮样式(需提前定义)。

网站公告

今日签到

点亮在社区的每一天
去签到