65.9K
CodeProject 正在变化。 阅读更多。
Home

WPF 通用消息框

2013年7月11日

CPOL

2分钟阅读

viewsIcon

49889

downloadIcon

1407

避免在 WPF 中锁定应用程序的繁琐消息框或输入框。

引言

显示消息框、输入框或快速弹出窗体可以通过许多不同的方式实现。
然而,仅仅为了这些目的而打开或管理另一个窗口或特殊对话框,未免有些麻烦,毕竟消息框的功能应该非常简单,不是吗?

此外,当你希望该消息框或窗口具有更复杂的功能,并且希望它与主窗口通信时,事情就会变得有些混乱...

背景

那么,这个技巧有什么新意?

正如我们上面所说,有很多方法可以实现消息框的功能,

例如

  • 使用对话框,该对话框在本 CP 文章中有一个很好的示例:WPF-Dialog-MessageBox-Manager
  • 使用简单的 MessageBox [System.Windows.MessageBox],如以下代码示例所示
MessageBoxResult result = 
  MessageBox.Show("Do you want to close this window?", 
  "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (result == MessageBoxResult.Yes)
{
    Application.Current.Shutdown();
}    

那么,问题是什么?

嗯,这些方法以及其他几种方法都会锁定应用程序,并且样式和管理起来非常繁琐。我们在此技巧中提供的是一种轻量级、灵活的方法,它避免了使用类似 Win32 的方法所带来的可怕锁定行为。

使用代码

那么它是如何实现的?

很简单!

基本思路是在你的 XAML 结尾添加一个 Grid,最初将 Visibility 设置为 Collapsed。然后,当 MessageBox(或 InputBox\Form 等)应该出现时,只需切换 Visibility 属性为 Visible。确认(或取消)后,执行所需任务并将 Visibility 重置为 Collapsed

该 Grid 将具有半透明的黑色背景,并且将包含窗体\消息框\输入框等在中间,就像任何控件一样。

正如你所看到的,代码和 XAML 非常简单。

XAML

<Window x:Class="DialogReplacement.dialogExample"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="dialogExample" Height="500" Width="500">
    <Grid x:Name="gridMain">
        
        <StackPanel>
        <Button Margin="20" Width="200" Background="Black" 
            Foreground="White" Content="Click to toggle a Confirm Dialog" 
            Click="OpenMbox_Clicked" />
 
            <!-- 
            **********************
            Your Page content here
            **********************
            -->
            
        </StackPanel>
 
        <Grid x:Name="DialogReplacement" Visibility="Collapsed">
            <Grid Background="Black" Opacity="0.5"/>
 
            <Border
            MinWidth="250"
            Background="DarkGoldenrod" 
            BorderBrush="Black" 
            BorderThickness="1" 
            CornerRadius="0,65,0,65" 
            HorizontalAlignment="Center" 
            VerticalAlignment="Center">
 
                <StackPanel>
                    
                    <Button x:Name="btnClose" 
                            Margin="8" 
                            HorizontalAlignment="Left" 
                            Height="20" Width="20" 
                            Content="X" FontSize="12" 
                            FontFamily="Georgia" FontWeight="Bold" 
                            Foreground="WhiteSmoke" Background="Red" 
                            Click="mbox_cancel" />
                    
                    <StackPanel HorizontalAlignment="Center" Margin="0,-22,0,0">
                        
                        <Label FontFamily="Cambria" Content="Confirm Dialog" 
                               FontWeight="Bold" FontSize="20" />
                        <Label FontSize="14" FontWeight="Bold" Foreground="White" 
                               Content="Are you sure?"></Label>
                            
                    </StackPanel>
                    
                    <Button HorizontalAlignment="Right" x:Name="YesButton" 
                        Width="40" Margin="8" 
                        Padding="3,0,3,0" 
                        Content="Yes" Background="Olive" 
                        Foreground="White" 
                        Click="mbox_ok"/>
                       
                </StackPanel>
            </Border>
        </Grid>
    </Grid>
</Window>

代码后置

namespace DialogReplacement
{    
    public partial class dialogExample : Window
    {
        public dialogExample()
        {
            InitializeComponent();
        }
 
        private void OpenMbox_Clicked(object sender, RoutedEventArgs e)
        {
            DialogReplacement.Visibility = System.Windows.Visibility.Visible;
        }
 
        private void mbox_ok(object sender, RoutedEventArgs e)
        {
            DialogReplacement.Visibility = System.Windows.Visibility.Collapsed;
        }
 
        private void mbox_cancel(object sender, RoutedEventArgs e)
        {
            DialogReplacement.Visibility = System.Windows.Visibility.Collapsed;
        }
    }
} 

示例

这是一个我差点删除了 Chris 帐户的例子...

哦,别担心,我点击了取消 ;)

历史

  • 版本 1.0.0.0。
© . All rights reserved.