rskibbe.UI.Dialogs.Wpf – An extension package for MVVM dialog implementation in WPF

What is rskibbe.UI.Dialogs.Wpf?

infoThe documentation is still a work in progress. Please understand that I do this for free and in my free time.

The goal – An MVVM friendly implementation for WPF

The goal of rskibbe.UI.Dialogs.Wpf.CommunityToolkit is to provide an implementation basis for WPF MVVM based dialogs. Keep in mind, that this is an extension package of rskibbe.UI.Dialogs – so you will need this package as well – it’s providing the infrastructure for dialogs, creating a common ground.

Installation

Use the NuGet Package Manager of Visual Studio to install the base package (and corresponding sub-packages if wanted/needed). The easiest way to access the Manager, is by clicking the „Extra“ menu tool strip menu item, or by right-clicking your project inside the project explorer. Then click something like „Manage NuGet Packages“. There you should be easily able to search for „rskibbe.UI.Dialogs“ and see all possible packages.

Or you can use the following NuGet Package Manager Console command for the newest version:

Install-Package rskibbe.UI.Dialogs
Install-Package rskibbe.UI.Dialogs.Wpf

Usage in project

To use this package in your project, you just need to install both mentioned dependencies from above. In the next step, you should register the WpfDialogService during your bootstrapping routine in the DI container. In my example, I’m using the „Autofac“ and „Autofac.Extensions.DependencyInjection“ nuget packages for dependency injection.

DI registration (Usually at app startup / bootstrap process)

        // somewhere before
        var builder = new ContainerBuilder();

        // register different dependencies, for example our DialogService
        builder.Register(c =>
        {
            var lifetimeScope = c.Resolve<ILifetimeScope>();
            var autofacServiceProvider = new AutofacServiceProvider(lifetimeScope);
            var dialogService = DialogServiceBuilder
                .New
                .UseServiceProviderForViewModels(autofacServiceProvider)
                .Build();
            return dialogService;
        })
        .As<IDialogService>()
        .SingleInstance();
        ' somewhere before
        Dim builder = New ContainerBuilder()

        ' register different dependencies, for example our DialogService
        builder.Register(Function(c)
               Dim lifetimeScope = c.Resolve(Of ILifetimeScope)()
               Dim autofacServiceProvider = New AutofacServiceProvider(lifetimeScope)
               Dim dialogService = DialogServiceBuilder _
                   .New _
                   .UseServiceProviderForViewModels(autofacServiceProvider) _
                   .Build()
               Return dialogService
           End Function) _
        .As<IDialogService>() _
        .SingleInstance()

Creating a DialogViewModel implementation

To use the WpfDialogService’s „ShowDialogAsync“ method, it needs an implementation of „IDialogViewModel“. The provided „DialogViewModelBase“ from the „rskibbe.UI.Dialogs.Wpf“-package will help as base class. If your are used to build your WPF MVVM apps using the typical „CommunityToolkit“ package, there’s also support for this, installing „rskibbe.UI.Dialogs.Wpf.CommunityToolkit“ – then you could use the source generators.

In this example, we will create a dialog using the „DialogViewModelBase“ base class. After creating the example ViewModel from below, fill it – as usual – with life (like commands, properties, etc.) and create a corresponding View for it like „MyDialogView“.

// don't forget to import the base class
using rskibbe.UI.Dialogs.Wpf;

public class MyDialogViewModel : DialogViewModelBase
{

   // your DialogViewModel logic
   // create properties to bind from view
   // create commands to bind from view

}
' don't forget to import the base class
Imports rskibbe.UI.Dialogs.Wpf

Public Class MyDialogViewModel
   Inherits DialogViewModelBase

   ' your DialogViewModel logic
   ' create properties to bind from view
   ' create commands to bind from view

End Class

Using the injected DialogService

After like typical constructor dependency injection, you can use the dialog service like the following. You can either use the generic method overload, or you could first resolve a ViewModel for yourself from the dependency injection container.

// don't forget the imports
using rskibbe.UI.Dialogs;

public class YourViewModel
{

  IDialogService _dialogService;

  public YourViewModel(IDialogService dialogService)
  {
      _dialogService = dialogService;
  }

  // should be an ICommand, of course...
  private void Bla()
  {
      bool? dialogResult = _dialogService.ShowDialogAsync<MyDialogViewModel>();
      // do something with the result
  }

}
' don't forget the imports
Imports rskibbe.UI.Dialogs

Public Class YourViewModel

  Private _dialogService As IDialogService

  Sub New(dialogService As IDialogService)
      _dialogService = dialogService;
  End Sub

  ' should be an ICommand, of course...
  Private Sub Bla()
      Dim dialogResult As Boolean? = _dialogService.ShowDialogAsync<MyDialogViewModel>();
      ' do something with the result
  End Sub

End Class

Key takeaways

Here I will talk about the key takeaways of this package, explaining classes, important stuff, the thoughts behind and so on.

IDialogViewModel

The MVVM friendly base-interface for implementing / extending IDialog from the base package.

DialogViewModelBase

A custom ViewModelBase implementation for IDialog / IDialogViewModel (and INotifyPropertyChanged behind the scenes).

WpfDialogServiceBuilder

A helper class to build „WpfDialogServiceBase“ for WPF MVVM apps using „Window“ as dialogs (or based on).

WpfDialogServiceBase

A base class for constructing your own IDialogService implementation.

WpfDialogService

A standard implementation of an IDialogService for typical WPF MVVM apps. (You can use another package called „rskibbe.UI.Dialogs.Wpf.CommunityToolkit“ if you need an implementation supporting the commonly used CommunityToolkit and – for example – its source generators).

Future plans / Todos

Here are some future plans and potential tasks I hope to tackle in the near future. However, please keep in mind that this project is a passion project done in my free time, so I can’t guarantee if or when these updates will be completed.

  • More documentation
  • More sub packages

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert