有时候系统自带的消息弹出框不能满足我们的要求,这就需要自己定义弹出框,下面是自己定义的弹出框,以备查用
首先要定义一个窗体弹出框:
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();
}
}
}