WPF C#中的Material Design MessageBox

6

我正在使用 Material Design In XAML Toolkit C#,以及 WPF C# 中的 Material Design MessageBox。

image 1

image 2

我需要一个类似或更好的 MessageBox 设计。

我尝试使用 DialogHost,但出现以下错误:

No loaded DialogHost instances.

private async void MenuPopupButton_OnClick(object sender, RoutedEventArgs e) {
    var sampleMessageDialog = new SampleMessageDialog {
        Message = {Text = "Goodbye"}
    };

    await DialogHost.Show(sampleMessageDialog, "RootDialog");            
}

没有加载的DialogHost实例。


可能有帮助:https://github.com/AmRo045/AmRoMessageBox - AmRo
确保标识符正确,并在单击时位于 dialogHost 内部。 - Albert Alonso
2个回答

15

我在我的应用程序中开发了一个自定义消息框(使用Material Design控件)。以下是WPF XAML和C#的代码。

它包含以下三种类型的消息框:

  1. Yes,No
  2. Ok
  3. Ok,Cancel

enter image description here

从下面的链接下载代码。

https://github.com/sandeepjadhav75502/CustomMessageBoxWPF

WPF XAML代码:

标题

<Window x:Class="EVotingDashBoard.MessageBoxCustom"
        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"
        mc:Ignorable="d"
        Title="MessageBoxWindow" Height="220" Width="500" 
        WindowStartupLocation="CenterScreen" WindowStyle="None" Background="White" 
        ResizeMode="CanResize" Topmost="True" ShowInTaskbar="False"
        xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
        TextElement.Foreground="{DynamicResource MaterialDesignBody}"
        TextElement.FontWeight="Regular"
        TextElement.FontSize="10"
        TextOptions.TextFormattingMode="Ideal"
        TextOptions.TextRenderingMode="Auto"        
        FontFamily="{DynamicResource MaterialDesignFont}">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="197*"/>
            <ColumnDefinition Width="295*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <materialDesign:Card x:Name="cardHeader" Grid.Row="0" Padding="10" Margin="0" materialDesign:ShadowAssist.ShadowDepth="Depth5"   Background="{DynamicResource PrimaryHueDarkBrush}" Foreground="{DynamicResource PrimaryHueDarkForegroundBrush}" Visibility="Visible" Grid.ColumnSpan="2">
            <StackPanel>
                <TextBlock x:Name="txtTitle" HorizontalAlignment="Center" VerticalAlignment="Stretch" Style="{DynamicResource MaterialDesignTitleTextBlock}" FontSize="20" >Message Title</TextBlock>
            </StackPanel>
        </materialDesign:Card>
        <StackPanel  HorizontalAlignment="Right" Margin="0,5,5,0"  VerticalAlignment="Top" Grid.Column="1">
            <Button x:Name="btnClose" Click="btnClose_Click" Width="35" Height="35"  Background="White" Foreground="{DynamicResource PrimaryHueDarkBrush}" Style="{StaticResource MaterialDesignFloatingActionDarkButton}">
                <materialDesign:PackIcon Kind="Close" />
            </Button>
        </StackPanel>
        <Grid Grid.Row="1" Grid.ColumnSpan="2">
            <Grid Margin="20">
                <Grid.RowDefinitions>
                    <RowDefinition Height="auto"/>
                    <RowDefinition Height="auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>

                <Grid Grid.Row="0">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>

                    <materialDesign:TransitioningContent>
                        <materialDesign:TransitioningContent.OpeningEffects >
                            <materialDesign:TransitionEffect Kind="FadeIn" />
                            <materialDesign:TransitionEffect Kind="SlideInFromBottom" />
                        </materialDesign:TransitioningContent.OpeningEffects>
                        <TextBox x:Name="txtMessage" HorizontalAlignment="Center" IsReadOnly="True" Grid.Row="0" Margin="5" materialDesign:HintAssist.Hint="" FontSize="18" Style="{StaticResource MaterialDesignFloatingHintTextBox}" />
                    </materialDesign:TransitioningContent>

                </Grid>
                <Grid Grid.Row="1" Margin="0,20,0,5">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="auto"/>
                        <ColumnDefinition Width="auto"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>

                    <Button x:Name="btnOk" Click="btnOk_Click" Grid.Column="1" Style="{StaticResource MaterialDesignRaisedButton}" Margin="5" Width="100" Content="OK" ToolTip="Ok"/>
                    <Button x:Name="btnCancel" Click="btnCancel_Click" Grid.Column="2" Style="{StaticResource MaterialDesignRaisedButton}" Margin="5" Width="100" Content="Cancel" ToolTip="Cancel"/>
                    <Button x:Name="btnYes" Click="btnYes_Click" Grid.Column="1" Style="{StaticResource MaterialDesignRaisedButton}" Margin="5" Width="100" Content="Yes" ToolTip="Yes"/>
                    <Button x:Name="btnNo" Click="btnNo_Click" Grid.Column="2" Style="{StaticResource MaterialDesignRaisedButton}" Margin="5" Width="100" Content="No" ToolTip="No"/>
                </Grid>

            </Grid>
        </Grid>
    </Grid>
</Window>

C#代码后台

 /// <summary>
    /// Interaction logic for MessageBoxCustom.xaml
    /// </summary>
    public partial class MessageBoxCustom : Window
    {
        public MessageBoxCustom(string Message, MessageType Type, MessageButtons Buttons)
        {
            InitializeComponent();
            txtMessage.Text = Message;
            switch (Type)
            {

                case MessageType.Info:
                    txtTitle.Text = "Info";
                    break;
                case MessageType.Confirmation:
                    txtTitle.Text = "Confirmation";
                    break;
                case MessageType.Success:
                    {
                        txtTitle.Text = "Success";
                    }
                    break;
                case MessageType.Warning:
                    txtTitle.Text = "Warning";
                    break;
                case MessageType.Error:
                    {   
                        txtTitle.Text = "Error";
                    }
                    break;
            }
            switch (Buttons)
            {
                case MessageButtons.OkCancel:
                    btnYes.Visibility = Visibility.Collapsed; btnNo.Visibility = Visibility.Collapsed;
                    break;
                case MessageButtons.YesNo:
                    btnOk.Visibility = Visibility.Collapsed; btnCancel.Visibility = Visibility.Collapsed;
                    break;
                case MessageButtons.Ok:
                    btnOk.Visibility = Visibility.Visible;
                    btnCancel.Visibility = Visibility.Collapsed;
                    btnYes.Visibility = Visibility.Collapsed; btnNo.Visibility = Visibility.Collapsed;
                    break;
            }
        }

        private void btnYes_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
            this.Close();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
            this.Close();
        }

        private void btnNo_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }
    }
    public enum MessageType
    {
        Info,
        Confirmation,
        Success,
        Warning,
        Error,
    }
    public enum MessageButtons
    {
        OkCancel,
        YesNo,
        Ok,
    }

我们可以这样调用它


 private void Button_Close(object sender, RoutedEventArgs e)
 {
        bool? Result = new MessageBoxCustom("Are you sure, You want to close 
        application ?", MessageType.Confirmation, MessageButtons.YesNo).ShowDialog();

        if (Result.Value)
        {
            Application.Current.Shutdown();
        }
    }

3
请检查上述代码,其中添加了 **ShowDialog()**。 - Sandeep Jadhav
我现在明白了,它默认适用于Window类型,但是我尝试将其实现为UserControl,所以出现了混淆。 - Amna
1
帮了我很多! - Niggo

2
考虑使用这个包,它具有以下额外的功能:
  • 自定义边框窗口、消息前景和背景、标题前景和背景、边框等样式
  • 复制消息框详细信息到剪贴板的按钮
  • 可滚动的消息框内容
  • 消息内容是.NET UIElement,可以承载任何内容

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接