天天看點

自定義的消息彈出框

有時候系統自帶的消息彈出框不能滿足我們的要求,這就需要自己定義彈出框,下面是自己定義的彈出框,以備查用

首先要定義一個窗體彈出框:

xaml如下:

<Window x:Class="CoffeeMachine.Controls.Message"

             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

             mc:Ignorable="d" WindowState="Maximized" WindowStyle="None" AllowsTransparency="True"

             d:DesignHeight="1920" d:DesignWidth="1080" Background="Transparent">

    <Viewbox>

        <Grid Height="1920" Width="1080">

            <Grid>

                <Border Background="Black" Opacity="0.5"></Border>

                <Border CornerRadius="20" Width="600" MinHeight="200" HorizontalAlignment="Center" VerticalAlignment="Center" Background="#D9FFFFFF" >

                    <Grid Width="600" MinHeight="200">

                        <TextBlock Name="tbTitle" FontSize="35" Foreground="#FFFF8700" Text="提示資訊" HorizontalAlignment="Center" VerticalAlignment="Top" Height="50" Margin="0,20"/>

                        <StackPanel Margin="0,100,0,0">

                            <TextBlock x:Name="tbContent" HorizontalAlignment="Center" TextWrapping="Wrap" MaxWidth="500" FontSize="35"/>

                            <Button x:Name="btnOk" Margin="100,30" Content="關閉" FontSize="35" Foreground="White" Style="{StaticResource ButtonStyle}" Height="80" Click="Button_Click"/>

                            <StackPanel Name="spOkCancel" Margin="50,30" Orientation="Horizontal" Visibility="Collapsed">

                                <Button Content="确定" FontSize="35" Foreground="White" Style="{StaticResource ButtonStyle}" Height="80" Width="225" Click="Button_Click_1"/>

                                <Button Content="取消" FontSize="35" Foreground="White" Style="{StaticResource ButtonStyle}" Height="80"  Width="225" Margin="50,0,0,0" Click="Button_Click_2"/>

                            </StackPanel>

                        </StackPanel>

                    </Grid>

                </Border>

            </Grid>

        </Grid>

    </Viewbox>

</Window>

.cs如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Navigation;

using System.Windows.Shapes;

namespace CoffeeMachine.Controls

{

    /// <summary>

    /// Message.xaml 的互動邏輯

    /// </summary>

    public partial class Message : Window

    {

        public Message(string message,string title,MessageBoxButton messageBoxButton)

        {

            InitializeComponent();

            MessageType(messageBoxButton);

            tbTitle.Text = title;

            tbContent.Text = message;

        }

        private void MessageType(MessageBoxButton messageBoxButton)

        {

            if (messageBoxButton == MessageBoxButton.OKCancel)

            {

                btnOk.Visibility = Visibility.Visible;

                spOkCancel.Visibility = Visibility.Visible;

            }

        }

        private void Button_Click(object sender, RoutedEventArgs e)

        {

            this.Close();

        }

        private void Button_Click_1(object sender, RoutedEventArgs e)

        {

            this.DialogResult = true;

            this.Close();

        }

        private void Button_Click_2(object sender, RoutedEventArgs e)

        {

            this.DialogResult = false;

            this.Close();

        }

    }

}

然後使用消息框的時候,定義一個靜态類來引用該消息框。

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows;

namespace CoffeeMachine.ClassHelp

{

    public static class MessageHelper

    {

        public static void ShowMessage(string message,string title="",MessageBoxButton ms=MessageBoxButton.OK)

        {

            Controls.Message messageBox = new Controls.Message(message,title,ms);

            messageBox.ShowDialog();

        }

    }

}