MessageDialogManagerLib 1.0.3

Library to easily use dialogs from a View Model in Mahapps.Metro applications.

Install-Package MessageDialogManagerLib -Version 1.0.3
dotnet add package MessageDialogManagerLib --version 1.0.3
<PackageReference Include="MessageDialogManagerLib" Version="1.0.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MessageDialogManagerLib --version 1.0.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: MessageDialogManagerLib, 1.0.3"
For F# scripts that support #r syntax, copy this into the source code to reference the package.

MessageDialogManagerLib

NuGet Downloads .NET Core Languages License Author

MessageDialogManagerLib is a library to easily use dialogs from ViewModels in WPF Mahapps.Metro applications. This library uses Mahapps.Metro to show info, progress, ok/cancel or custom dialog. To show an open file dialog, this library uses Microsoft.Win32.OpenFileDialog, to show a save file dialog uses Microsoft.Win32.SaveFileDialog and to show a folder browser, this library uses FolderBrowserEx.

Supporting .NET Framework (4.5+) and .NET Core (3.0 and 3.1)

Introduction

In both WPF .NET Framework and .NET Core applications we can use dialogs. The problem comes when you want to show a dialog from a ViewModel, especially when you want to test your ViewModel and dialogs block your tests.

The aim of this project is to offer a library with an interface to easily inject in your ViewModel. Your ViewModel will be independent from dialogs implementations and will be testable without blocks.

Getting Started

To use this library, there are a few options:

To use this library, the WPF application has to use Mahapps.Metro.

The MessageDialogManagerLib uses the IMessageDialogManager interface.

public interface IMessageDialogManager
{
    /// <summary>
    /// Shows a folder browser dialog
    /// </summary>
    /// <param name="title">Sets the title of the dialog</param>
    /// <param name="initialPath">Sets the initial path of the dialog</param>
    /// <returns>Returns if a folder has been selected</returns>
    bool ShowFolderBrowser(string title, string initialPath, bool allowMultiSelect = false);

    /// <summary>
    /// Gets the selected folder
    /// </summary>
    string FolderPath { get; set; }

    /// <summary>
    /// Shows a file browser dialog
    /// </summary>
    /// <param name="title">Sets the title of the dialog</param>
    /// <param name="initialPath">Sets the initial path of the dialog</param>
    /// <param name="filter">Sets a filter to show only the files that meet the filter</param>
    /// <returns>Returns if a file has been selected</returns>
    bool ShowFileBrowser(string title, string initialPath, string filter, bool allowMultiSelect = false);

    /// <summary>
    /// Gets the selected file
    /// </summary>
    string FilePath { get; set; }

    /// <summary>
    /// Shows a save file dialog
    /// </summary>
    /// <param name="title">Sets the title of the dialog</param>
    /// <param name="initialPath">Sets the initial path of the dialog</param>
    /// <param name="fileName">Sets the file's name</param>
    /// <param name="defaultExt">Sets the default file's extension</param>
    /// <param name="filter">Sets the filter</param>
    /// <returns>Returns if a file has been saved</returns>
    bool ShowSaveFileDialog(string title, string initialPath, string fileName, 
        string defaultExt, string filter);

    /// <summary>
    /// Gets the file to save
    /// </summary>
    string FilePathToSave { get; set; }

    /// <summary>
    /// Shows a ok and cancel dialog
    /// </summary>
    /// <param name="text">Sets the text of the dialog</param>
    /// <param name="title">Sets the title of the dialog</param>
    /// MessageDialogResult.OK if the user clicks OK in the dialog box;
    /// otherwise, MessageDialogResult.Cancel.
    Task<MessageDialogResult> ShowOkCancelDialogAsync(string text, string title);

    /// <summary>
    /// Shows a info dialog
    /// </summary>
    /// <param name="title">Sets the title of the dialog</param>
    /// <param name="message">Sets the message of the dialog</param>
    /// <returns></returns>
    Task ShowInfoDialogAsync(string title, string message);

    /// <summary>
    /// Shows a progress dialog
    /// </summary>
    /// <param name="title">Sets the title of the dialog</param>
    /// <param name="message">Sets the message of the dialog</param>
    /// <returns></returns>
    Task ShowProgress(string title, string message);

    /// <summary>
    /// Updates the progress
    /// </summary>
    /// <param name="progress">Sets de progress</param>
    void UpdateProgress(double progress);

    /// <summary>
    /// Updates the message progress
    /// </summary>
    /// <param name="message">Sets the message progress</param>
    void UpdateMessageProgress(string message);

    /// <summary>
    /// Closes the progress dialog
    /// </summary>
    /// <returns></returns>
    Task CloseProgress();

    /// <summary>
    /// Shows a custom dialog
    /// </summary>
    /// <param name="viewModel">Sets the viewmodel attached to the custom dialog</param>
    /// <returns></returns>
    Task ShowDialogAsync(IDialogViewModel viewModel);
}

To use MessageDialogManagerLib in an application, you can follow this example code. There are others examples in the directory Samples of the solution.

using CommandLibrary;
using MessageDialogManagerLib;
using System.Threading.Tasks;
using System.Windows.Input;

namespace NetFrameworkSample.ViewModel
{
    public class MainWindowViewModel
    {
        private readonly IMessageDialogManager _messageDialogManager;

        public MainWindowViewModel(IMessageDialogManager messageDialogManager)
        {
            _messageDialogManager = messageDialogManager;
            ShowOkCancelDialogCommand = new Command(
                ShowOkCancelDialogCommandExecute, 
                ShowOkCancelDialogCommandCanExecute);
        }

        public ICommand ShowOkCancelDialogCommand { get; private set; }

        private async void ShowOkCancelDialogCommandExecute()
        {
            MessageDialogResult result = 
                await _messageDialogManager.ShowOkCancelDialogAsync(
                    "Ok Cancel Dialog", 
                    "This is a Ok Cancel Dialog");
            if (result == MessageDialogResult.OK)
                await _messageDialogManager.ShowInfoDialogAsync("Result", "You select Ok");
            else
                await _messageDialogManager.ShowInfoDialogAsync("Result", "You select Cancel");
        }

        private bool ShowOkCancelDialogCommandCanExecute()
        {
            return true;
        }
    }
}

License

Copyright © 2020 Evaristo Cuesta

MessageDialogManagerLib is provided as-is under the MIT license. For more information see LICENSE.

Credits

This project uses Mahapps.Metro and FolderBrowserEx.

MessageDialogManagerLib

NuGet Downloads .NET Core Languages License Author

MessageDialogManagerLib is a library to easily use dialogs from ViewModels in WPF Mahapps.Metro applications. This library uses Mahapps.Metro to show info, progress, ok/cancel or custom dialog. To show an open file dialog, this library uses Microsoft.Win32.OpenFileDialog, to show a save file dialog uses Microsoft.Win32.SaveFileDialog and to show a folder browser, this library uses FolderBrowserEx.

Supporting .NET Framework (4.5+) and .NET Core (3.0 and 3.1)

Introduction

In both WPF .NET Framework and .NET Core applications we can use dialogs. The problem comes when you want to show a dialog from a ViewModel, especially when you want to test your ViewModel and dialogs block your tests.

The aim of this project is to offer a library with an interface to easily inject in your ViewModel. Your ViewModel will be independent from dialogs implementations and will be testable without blocks.

Getting Started

To use this library, there are a few options:

To use this library, the WPF application has to use Mahapps.Metro.

The MessageDialogManagerLib uses the IMessageDialogManager interface.

public interface IMessageDialogManager
{
    /// <summary>
    /// Shows a folder browser dialog
    /// </summary>
    /// <param name="title">Sets the title of the dialog</param>
    /// <param name="initialPath">Sets the initial path of the dialog</param>
    /// <returns>Returns if a folder has been selected</returns>
    bool ShowFolderBrowser(string title, string initialPath, bool allowMultiSelect = false);

    /// <summary>
    /// Gets the selected folder
    /// </summary>
    string FolderPath { get; set; }

    /// <summary>
    /// Shows a file browser dialog
    /// </summary>
    /// <param name="title">Sets the title of the dialog</param>
    /// <param name="initialPath">Sets the initial path of the dialog</param>
    /// <param name="filter">Sets a filter to show only the files that meet the filter</param>
    /// <returns>Returns if a file has been selected</returns>
    bool ShowFileBrowser(string title, string initialPath, string filter, bool allowMultiSelect = false);

    /// <summary>
    /// Gets the selected file
    /// </summary>
    string FilePath { get; set; }

    /// <summary>
    /// Shows a save file dialog
    /// </summary>
    /// <param name="title">Sets the title of the dialog</param>
    /// <param name="initialPath">Sets the initial path of the dialog</param>
    /// <param name="fileName">Sets the file's name</param>
    /// <param name="defaultExt">Sets the default file's extension</param>
    /// <param name="filter">Sets the filter</param>
    /// <returns>Returns if a file has been saved</returns>
    bool ShowSaveFileDialog(string title, string initialPath, string fileName, 
        string defaultExt, string filter);

    /// <summary>
    /// Gets the file to save
    /// </summary>
    string FilePathToSave { get; set; }

    /// <summary>
    /// Shows a ok and cancel dialog
    /// </summary>
    /// <param name="text">Sets the text of the dialog</param>
    /// <param name="title">Sets the title of the dialog</param>
    /// MessageDialogResult.OK if the user clicks OK in the dialog box;
    /// otherwise, MessageDialogResult.Cancel.
    Task<MessageDialogResult> ShowOkCancelDialogAsync(string text, string title);

    /// <summary>
    /// Shows a info dialog
    /// </summary>
    /// <param name="title">Sets the title of the dialog</param>
    /// <param name="message">Sets the message of the dialog</param>
    /// <returns></returns>
    Task ShowInfoDialogAsync(string title, string message);

    /// <summary>
    /// Shows a progress dialog
    /// </summary>
    /// <param name="title">Sets the title of the dialog</param>
    /// <param name="message">Sets the message of the dialog</param>
    /// <returns></returns>
    Task ShowProgress(string title, string message);

    /// <summary>
    /// Updates the progress
    /// </summary>
    /// <param name="progress">Sets de progress</param>
    void UpdateProgress(double progress);

    /// <summary>
    /// Updates the message progress
    /// </summary>
    /// <param name="message">Sets the message progress</param>
    void UpdateMessageProgress(string message);

    /// <summary>
    /// Closes the progress dialog
    /// </summary>
    /// <returns></returns>
    Task CloseProgress();

    /// <summary>
    /// Shows a custom dialog
    /// </summary>
    /// <param name="viewModel">Sets the viewmodel attached to the custom dialog</param>
    /// <returns></returns>
    Task ShowDialogAsync(IDialogViewModel viewModel);
}

To use MessageDialogManagerLib in an application, you can follow this example code. There are others examples in the directory Samples of the solution.

using CommandLibrary;
using MessageDialogManagerLib;
using System.Threading.Tasks;
using System.Windows.Input;

namespace NetFrameworkSample.ViewModel
{
    public class MainWindowViewModel
    {
        private readonly IMessageDialogManager _messageDialogManager;

        public MainWindowViewModel(IMessageDialogManager messageDialogManager)
        {
            _messageDialogManager = messageDialogManager;
            ShowOkCancelDialogCommand = new Command(
                ShowOkCancelDialogCommandExecute, 
                ShowOkCancelDialogCommandCanExecute);
        }

        public ICommand ShowOkCancelDialogCommand { get; private set; }

        private async void ShowOkCancelDialogCommandExecute()
        {
            MessageDialogResult result = 
                await _messageDialogManager.ShowOkCancelDialogAsync(
                    "Ok Cancel Dialog", 
                    "This is a Ok Cancel Dialog");
            if (result == MessageDialogResult.OK)
                await _messageDialogManager.ShowInfoDialogAsync("Result", "You select Ok");
            else
                await _messageDialogManager.ShowInfoDialogAsync("Result", "You select Cancel");
        }

        private bool ShowOkCancelDialogCommandCanExecute()
        {
            return true;
        }
    }
}

License

Copyright © 2020 Evaristo Cuesta

MessageDialogManagerLib is provided as-is under the MIT license. For more information see LICENSE.

Credits

This project uses Mahapps.Metro and FolderBrowserEx.

Release Notes

https://github.com/evaristocuesta/MessageDialogManagerLib/releases

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.0.3 93 12/25/2020
1.0.2 44 11/30/2020
1.0.1 53 11/24/2020
1.0.0 157 10/31/2020