In WPF .NET 6, 7, and 8, a message box, often referred to as a dialog or alert box, serves as a user interface element designed to present information, prompt user input, or deliver notifications within software applications. This essential component plays distinct roles and adheres to specific requirements, all aimed at enhancing user interaction. Here's a comprehensive overview of the functions and requirements associated with a message box:

Functions


Informational Messages

User Input Prompt

Error Handling

Warning Messages

Confirmation Dialogs

Progress Indication

Requirements


Clarity and Readability

Consistent Design

Configurability

Accessibility

User Interaction Handling

Responsiveness

Localization

Implementing a custom message box in WPF using the Custom.Dotnet7.MessageBox NuGet package involves the following steps.

1. Installation of the NuGet Package

2. Integration into the WPF Project

3. Initial setup for the custom message box

Add the following details in App.xaml.cs

using Custom.Dotnet7.MessageBox.Implementation;
using Custom.Dotnet7.MessageBox.Interfaces;
using Custom.Dotnet7.MessageBox.Utility;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace Dotnet7CustomDialogSample
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        public static IServiceProvider ServiceProvider { get; private set; } = null;

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            // Initial setup for all types of modal MessageBox
            CustomMessageBoxHandler.GetInstance.ConfigureModalPopupSettings(
                ModalPopupMouseHoverButtonBackgroundColor: new SolidColorBrush(Colors.Blue),
                ModalPopupMouseHoverButtonForegroundColor: new SolidColorBrush(Colors.White),
                ModalPopupBodyBackgroundColor: new SolidColorBrush(Colors.Black),
                ModalPopupMessageTextColor: new SolidColorBrush(Colors.White),
                ModalPopupMessageTextFontSize: 13.0,
                ModalPopupMessageTextFontFamily: new FontFamily("Arial Black"),
                ModalPopupButtonBottomAlignment: HorizontalAlignment.Right,
                ModalPopupButtonWidth: 80.0,
                ModalPopupButtonTextColor: new SolidColorBrush(Colors.White),
                ModalPopupMessageButtonFontSize: 12.0,
                ModalPopupButtonDisplayBackgroundColor: new SolidColorBrush(Colors.OrangeRed),
                ModalPopupHeaderBackgroundColor: new SolidColorBrush(Colors.OrangeRed),
                ModalPopupHeaderTitleTextColor: new SolidColorBrush(Colors.White),
                ModalPopupHeaderFontFamily: new FontFamily("Arial Black"),
                ModalPopupHeaderFontSize: 13.0,
                ModalPopupHeaderIcon: CreateImageSourceFromPackUri());

            SetInstance();
        }

        private void SetInstance()
        {
            // Create a service collection
            var services = new ServiceCollection();

            // Add your services to the container
            services.AddSingleton<IModalPopupHandlerService, ModalPopupHandlerService>();
            services.AddTransient<MainWindow>();
            services.AddTransient<MainWindowViewModel>();

            // Build the service provider
            ServiceProvider = services.BuildServiceProvider();
        }

        public ImageSource CreateImageSourceFromPackUri()
        {
            try
            {
                // When the image is kept in the running assembly
                Uri uri = new Uri(@"pack://application:,,,/Dotnet7CustomDialogSample;component/Images/CameraControl.png", UriKind.RelativeOrAbsolute);
                return new BitmapImage(uri);
            }
            catch (Exception ex)
            {
                // Handle any exceptions, such as UriFormatException or FileNotFoundException
                // You can log the exception or return a default image source if needed
                return new BitmapImage();
            }
        }
    }
}

4. Integration in Code Behind

In the code-behind file (usually the .cs file associated with your XAML), handle the logic for invoking and responding to the custom message box.

Implement event handlers or methods to execute actions based on user interactions with the message box.

using Custom.Dotnet7.MessageBox.Utility;
using Microsoft.Extensions.DependencyInjection;
using System.Windows;

namespace Dotnet7CustomDialogSample
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = App.ServiceProvider.GetRequiredService<MainWindowViewModel>();
            SharedOccurrenceEventsHandler.ModalConfirmDialogCallBackHandler += CommonEvents_ModalConfirmDialogCallBackHandler;
        }

        private void CommonEvents_ModalConfirmDialogCallBackHandler(object? sender, ProcessEventArgs e)
        {
            if (e.DialogResult)
            {
                // Perform your task
            }
            else
            {
                // Do action
            }
        }

        private void FileDialog_Click(object sender, RoutedEventArgs e)
        {
            string inputResult = CustomMessageBoxHandler.GetInstance.DisplayComprehensiveFileMessageBox(headerTitle: "File Handler", browseButtonText: "Browse Folder", modalPopupButtonText: "Ok");
            var str = inputResult;
        }

        private void InputDialog_Click(object sender, RoutedEventArgs e)
        {
            string inputResult = CustomMessageBoxHandler.GetInstance.DisplayComprehensiveInputMessageBox(headerTitle: "Input Prompt", inputLabelText: "Enter Text", modalPopupButtonText: "Ok");
            var str = inputResult;
        }

        private void CustomAlertModalPopup_Click(object sender, RoutedEventArgs e)
        {
            CustomMessageBoxHandler.GetInstance.DisplayCustomAlertMessageBox(headerTitle: "Information", modalPopupMessageText: "This is a custom message box", messageBoxImageIconType: CustomMessageBoxImage.Exclamation, modalPopupButtonText: "OK");
        }

        private void CustomConfirmModalPopup_Click(object sender, RoutedEventArgs e)
        {
            CustomMessageBoxHandler.GetInstance.DisplayCustomConfirmationMessageBox(headerTitle: "Information", modalPopupMessageText: "Do you want to close this popup? Do you want to close this popup? Do you want to close this popup?", messageBoxImageIconType: CustomMessageBoxImage.Warning, modalPopupFirstButtonText: "OK", modalPopupSecondButtonText: "Cancel");
        }

        private void ComprehensiveCustomAlertModalPopup_Click(object sender, RoutedEventArgs e)
        {
            CustomMessageBoxHandler.GetInstance.DisplayComprehensiveCustomAlertMessageBox(headerTitle: "Information", modalPopupMessageText: "This is a custom message box", messageBoxImageIconType: CustomMessageBoxImage.Error, modalPopupButtonText: "Okay");
        }

        private void ComprehensiveCustomConfirmModalPopup_Click(object sender, RoutedEventArgs e)
        {
            CustomMessageBoxHandler.GetInstance.DisplayComprehensiveCustomConfirmMessageBox(headerTitle: "Demo Modal Popup", modalPopupMessageText: "Do you want to close this application?", messageBoxImageIconType: CustomMessageBoxImage.Information, modalPopupFirstButtonText: "Yes", modalPopupSecondButtonText: "No");
        }
    }
}

5. Code integration in MVVM pattern

If you are using MVVM pattern and your application is based on dependency injection then follow the following instructions.

using Custom.Dotnet7.MessageBox.Interfaces;
using Custom.Dotnet7.MessageBox.Utility;
using Prism.Commands;
using Prism.Mvvm;

namespace Dotnet7CustomDialogSample
{
    public class MainWindowViewModel : BindableBase
    {
        // Custom Alert
        private DelegateCommand _ExecuteCustomAlertPopupCommand;

        public DelegateCommand ExecuteCustomAlertPopupCommand =>
            _ExecuteCustomAlertPopupCommand ?? (_ExecuteCustomAlertPopupCommand = new DelegateCommand(ExecuteCustomAlertPopupHandler));

        private void ExecuteCustomAlertPopupHandler()
        {
            modalPopupHandlerService.DisplayCustomAlertMessageBox(headerTitle: "Custom Message", modalPopupMessageText: "This is a custom message box", messageBoxImageIconType: CustomMessageBoxImage.Exclamation, modalPopupButtonText: "OK");
        }

        // Custom Confirm Modal Popup
        private DelegateCommand _ExecuteCustomConfirmPopupCommand;

        public DelegateCommand ExecuteCustomConfirmPopupCommand =>
            _ExecuteCustomConfirmPopupCommand ?? (_ExecuteCustomConfirmPopupCommand = new DelegateCommand(ExecuteCustomConfirmPopupHandler));

        private void ExecuteCustomConfirmPopupHandler()
        {
            modalPopupHandlerService.DisplayCustomConfirmationMessageBox(headerTitle: "Custom Message", modalPopupMessageText: "Do you want to close this popup? Do you want to close this popup? Do you want to close this popup?", messageBoxImageIconType: CustomMessageBoxImage.Warning, modalPopupFirstButtonText: "OK", modalPopupSecondButtonText: "Cancel");
        }

        // Comprehensive Alert
        private DelegateCommand _ExecuteComprehensiveAlertPopupCommand;

        public DelegateCommand ExecuteComprehensiveAlertPopupCommand =>
            _ExecuteComprehensiveAlertPopupCommand ?? (_ExecuteComprehensiveAlertPopupCommand = new DelegateCommand(ExecuteComprehensiveAlertPopupHandler));

        private void ExecuteComprehensiveAlertPopupHandler()
        {
            modalPopupHandlerService.DisplayComprehensiveCustomAlertMessageBox(headerTitle: "Custom Message", modalPopupMessageText: "This is a custom message box", messageBoxImageIconType: CustomMessageBoxImage.Error, modalPopupButtonText: "Okay");
        }

        // Comprehensive Confirm Modal Popup
        private DelegateCommand _ExecuteComprehensiveConfirmPopupCommand;

        public DelegateCommand ExecuteComprehensiveConfirmPopupCommand =>
            _ExecuteComprehensiveConfirmPopupCommand ?? (_ExecuteComprehensiveConfirmPopupCommand = new DelegateCommand(ExecuteComprehensiveConfirmPopupHandler));

        private void ExecuteComprehensiveConfirmPopupHandler()
        {
            modalPopupHandlerService.DisplayComprehensiveCustomConfirmMessageBox(headerTitle: "Custom Message", modalPopupMessageText: "This is a custom confirm message box", messageBoxImageIconType: CustomMessageBoxImage.Question, modalPopupFirstButtonText: "Okay", modalPopupSecondButtonText: "Close");
        }

        // Input Dialog
        private DelegateCommand _ExecuteCustomInputPopupCommand;

        public DelegateCommand ExecuteCustomInputPopupCommand =>
            _ExecuteCustomInputPopupCommand ?? (_ExecuteCustomInputPopupCommand = new DelegateCommand(ExecuteInputPopupHandler));

        private void ExecuteInputPopupHandler()
        {
            string inputResult = modalPopupHandlerService.DisplayComprehensiveInputMessageBox(headerTitle: "Custom Message", inputLabelText: "Enter your text", modalPopupButtonText: "Okay");
            var str = inputResult;
        }

        // File Dialog
        private DelegateCommand _ExecuteCustomFilePopupCommand;

        public DelegateCommand ExecuteCustomFilePopupCommand =>
            _ExecuteCustomFilePopupCommand ?? (_ExecuteCustomFilePopupCommand = new DelegateCommand(ExecuteFilePopupHandler));

        private void ExecuteFilePopupHandler()
        {
            string inputResult = modalPopupHandlerService.DisplayComprehensiveFileMessageBox(headerTitle: "File Dialog Popup", browseButtonText: "Browse your custom Folder", modalPopupButtonText: "Okay");
            var str = inputResult;
        }

        IModalPopupHandlerService? modalPopupHandlerService = null;

        public MainWindowViewModel(IModalPopupHandlerService modalPopupHandlerService)
        {
            this.modalPopupHandlerService = modalPopupHandlerService;
            SharedOccurrenceEventsHandler.ModalConfirmDialogCallBackHandler += CommonEvents_ModalConfirmDialogCallBackHandler;
        }

        private void CommonEvents_ModalConfirmDialogCallBackHandler(object? sender, ProcessEventArgs e)
        {
            if (e.DialogResult)
            {
                // Perform your task
            }
            else
            {
                // Do action
            }
        }
    }
}

6. Testing

The final result will be like the one below.

Result

Nuget Package: "Custom.Dotnet7.MessageBox"

Custom Message box

Repository Path: https://github.com/OmatrixTech/Dotnet7CustomDialogSample