An open API service indexing awesome lists of open source software.

https://github.com/evaristocuesta/messagedialogmanagerlib

Library to easily use dialogs from a View Model in a Mahapps Metro App.
https://github.com/evaristocuesta/messagedialogmanagerlib

csharp dialogs mahapps-metro mvvm netframework viewmodel wpf

Last synced: 5 months ago
JSON representation

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

Awesome Lists containing this project

README

        

# MessageDialogManagerLib
[![NuGet](https://img.shields.io/nuget/v/MessageDialogManagerLib)](https://nuget.org/packages/MessageDialogManagerLib/) [![Downloads](https://img.shields.io/nuget/dt/MessageDialogManagerLib)](https://nuget.org/packages/MessageDialogManagerLib/) [![.NET Core](https://github.com/evaristocuesta/MessageDialogManagerLib/workflows/.NET%20Core/badge.svg)](https://github.com/evaristocuesta/MessageDialogManagerLib/actions) [![Languages](https://img.shields.io/github/languages/top/evaristocuesta/MessageDialogManagerLib)](https://github.com/evaristocuesta/MessageDialogManagerLib/) [![License](https://img.shields.io/github/license/evaristocuesta/MessageDialogManagerLib)](https://raw.githubusercontent.com/evaristocuesta/MessageDialogManagerLib/master/LICENSE) [![Author](https://img.shields.io/badge/author-Evaristo%20Cuesta-blue)](https://www.evaristocuesta.com/)

**MessageDialogManagerLib** is a library to easily use dialogs from ViewModels in WPF Mahapps.Metro applications. This library uses [Mahapps.Metro](https://mahapps.com/) 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](https://github.com/evaristocuesta/FolderBrowserEx).

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

## Table of contents

- [Introduction](#introduction)
- [Getting Started](#getting-started)
- [License](#license)
- [Credits](#credits)

## 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:

- Download the [Github repository](https://github.com/evaristocuesta/MessageDialogManagerLib)
- Use the [MessageDialogManagerLib Nuget Package](https://nuget.org/packages/MessageDialogManagerLib/)

To use this library, the WPF application has to use [Mahapps.Metro](https://mahapps.com/).

The **MessageDialogManagerLib** uses the IMessageDialogManager interface.
```csharp
public interface IMessageDialogManager
{
///
/// Shows a folder browser dialog
///
/// Sets the title of the dialog
/// Sets the initial path of the dialog
/// Returns if a folder has been selected
bool ShowFolderBrowser(string title, string initialPath, bool allowMultiSelect = false);

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

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

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

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

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

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

///
/// Shows a info dialog
///
/// Sets the title of the dialog
/// Sets the message of the dialog
///
Task ShowInfoDialogAsync(string title, string message);

///
/// Shows a progress dialog
///
/// Sets the title of the dialog
/// Sets the message of the dialog
///
Task ShowProgress(string title, string message);

///
/// Updates the progress
///
/// Sets de progress
void UpdateProgress(double progress);

///
/// Updates the message progress
///
/// Sets the message progress
void UpdateMessageProgress(string message);

///
/// Closes the progress dialog
///
///
Task CloseProgress();

///
/// Shows a custom dialog
///
/// Sets the viewmodel attached to the custom dialog
///
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.

```csharp
MessageDialogManagerMahapps messageDialogManager = new MessageDialogManagerMahapps(App.Current);
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");
```

If you want to use the **MessageDialogManagerLib** library from a View Model, follow this example code. You can find the complete example in the directory Samples of the solution.

```csharp
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](https://github.com/evaristocuesta/MessageDialogManagerLib/blob/master/LICENSE).

## Credits

This project uses [Mahapps.Metro](https://mahapps.com/) and [FolderBrowserEx](https://github.com/evaristocuesta/FolderBrowserEx).