• Home
  • Documentation
Show / Hide Table of Contents
  • Introduction
  • Commands
    • Commanding
    • Composite Commands
    • Async Commands
    • Error Handling
  • Dependency Injection
    • Getting Started
    • Registering Types
    • Microsoft Extensions (Supplement)
    • Platform Specific Services
    • Exception Handling
    • ContainerLocator
    • Appendix
  • Dialog Service
    • Dialog Service
    • IDialogAware ViewModels
    • IDialogWindow (WPF & Uno Platform)
  • Event Aggregator
  • Mvvm
    • BindableBase
    • ViewModelLocator
  • Modularity
    • Getting Started
    • Module Catalog
    • Module Initialization
  • Navigation
    • Getting Started
    • INavigationParameters
    • Page Navigation
    • Regions
      • Getting Started
      • Region Manager
      • Region Adapters
      • Region Behaviors
      • About Navigation in Prism
      • Basic Region Navigation
      • View/ViewModel Participation
      • Navigating to Existing Views
      • Passing Parameters
      • Confirming Navigation
      • Controlling View Lifetime
      • Navigation Journal
  • Platforms
    • Maui
      • Getting Started
      • Migrating from Prism.Forms
      • PrismAppBuilder
      • AppModel
        • IPageLifecycleAware
      • Behaviors
        • Introduction
        • BehaviorBase<T>
        • EventToCommandBehavior
        • PageBehaviorFactory
      • Dialogs
        • Getting Started
        • IPageDialogService
      • Navigation
        • Introduction
        • Page Navigation
        • NavigationBuilder
        • TabbedPages
        • Understanding the INavigationResult
        • NavigationExceptions
        • Global Navigation Observer
        • XAML Navigation
    • Uno Platform
      • Getting Started
      • Uno.Extensions
    • Wpf
      • Introduction
      • Getting Started
      • View Composition
      • Interactivity
        • Event To Command
    • Xamarin.Forms
      • Create Your First App
      • Behaviors
        • Working with Behaviors
        • EventToCommand Behavior
        • PageBehaviorFactory
      • Dialogs
        • Dialogs
        • Page Dialog Service
        • Dialog Service
        • Styling Dialogs
      • Navigation
        • Navigation Basics
        • Passing Parameters
        • Confirming Navigation
        • Deep Linking
        • Working w/ MasterDetailPages
        • Working w/ NavigationPages
        • Working w/ TabbedPages
        • XAML Navigation
      • Application Lifecycle
      • Page Lifecycle
      • Additional Platforms
        • GTK
  • Magician
    • Getting Started
  • Plugins
    • Essentials
      • Getting Started
      • ApplicationModel
        • App Context
        • Browser
        • Communication
          • Email
          • Phone Dialer
        • Data Transfer
          • Clipboard
          • Share
        • LatestVersion
        • Launcher
        • Version Tracking
      • Devices
        • Battery
        • Sensors
          • Biometrics
          • Geocoding
          • Geofencing
          • Geolocation
      • IO
        • File System
        • Stores
      • Media
        • Getting Started
        • Camera
        • Video
      • Networking
        • Connectivity
      • Notifications
        • Getting Started
        • ActionSheets
        • Alerts
        • Prompts
      • Permissions
        • Permissions Manager
      • Threading
        • Main Thread
    • Logging
      • Getting Started
      • Interop
        • Essentials
        • Microsoft Extensions
      • Providers
        • AppCenter
        • Console
        • Debug
        • Firebase
        • Graylog
        • Kochava
        • Raygun
        • Sentry
        • Unit Testing
        • Xunit
    • Observable Regions
    • Popups
  • Pipelines
    • Commercial Plus

Getting Started

There are a variety of reasons you may want to create a Dialog in your application. This could be to display a message to your user, or present them with a form to enter some information, etc. Within the Prism.Core we have defined a central abstraction layer for presenting Dialogs across all Prism supported platforms. Dialogs within Prism use the native mechanisms for presenting your custom Views. This enables you to create Dialogs that have the same look and feel as the rest of your application while continuing to use the MVVM pattern.

Changes

Prism 9.0 introduces some changes to the IDialogService with a goal of helping meet you with the callback code that meets your needs. At the heart of the changes is the introduction of the DialogCallback. The DialogCallback is designed to provide you more flexibility in responding to the IDialogResult. This allows you to provide an asynchronous or synchronous delegate. Finally rather than being explicitly prescriptive about what you might need to provide as an argument for your callback, it aims to better meet you.

On Close

// Basic Callback
new DialogCallback().OnClose(() => Console.WriteLine("The Dialog Closed"));

// Callback with the Dialog Result
new DialogCallback().OnClose(result => Console.WriteLine($"The Dialog Button Result is: {result.Result}"));

In addition to the synchronous callbacks shown above each of these has an equivalent for handling asynchronous callbacks:

// Basic Callback
new DialogCallback().OnCloseAsync(() => Task.CompletedTask);

// Callback with the Dialog Result
new DialogCallback().OnCloseAsync(result => Task.CompletedTask);

Error Handling

Additionally it will let you provide an error handler which will only be invoked in the case that an Exception is encountered.

// Basic Error Callback
new DialogCallback().OnError(() => Console.WriteLine("Whoops... something bad happened!"));

// Catch All Exception Handler
new DialogCallback().OnError(exception => Console.WriteLine(exception));

// Specific Catch Handler
new DialogCallback().OnError<NullReferenceException>(nre =>
{
    Console.WriteLine("This will only be executed when the Exception is a NullReferenceException.");
    Console.WriteLine("Plus our variable is correctly typed for our handler to work with!");
});

// Specific Catch with the IDialogResult
new DialogCallback().OnError<NullReferenceException>((nre, result) =>
{
    Console.WriteLine($"Button Result: {result.Result}");
    Console.WriteLine(nre);
});
Note

Each of the OnError samples above also has an equivalent OnErrorAsync which accepts a delegate returning a Task as well.

Next Steps

  • IDialogAware ViewModels
  • IDialogWindow (WPF & Uno Platform Only)
  • Edit on GitHub
  • Ask questions
  • Follow @PrismLib
  • Follow @BrianLagunas
  • Follow @DanJSiegel
Back to top Copyright 2015-2024 Prism Software, LLC