MessageDialogService.Uno 1.0.3-feature.Uno5Update.2

This is a prerelease version of MessageDialogService.Uno.
dotnet add package MessageDialogService.Uno --version 1.0.3-feature.Uno5Update.2                
NuGet\Install-Package MessageDialogService.Uno -Version 1.0.3-feature.Uno5Update.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MessageDialogService.Uno" Version="1.0.3-feature.Uno5Update.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MessageDialogService.Uno --version 1.0.3-feature.Uno5Update.2                
#r "nuget: MessageDialogService.Uno, 1.0.3-feature.Uno5Update.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install MessageDialogService.Uno as a Cake Addin
#addin nuget:?package=MessageDialogService.Uno&version=1.0.3-feature.Uno5Update.2&prerelease

// Install MessageDialogService.Uno as a Cake Tool
#tool nuget:?package=MessageDialogService.Uno&version=1.0.3-feature.Uno5Update.2&prerelease                

Message Dialog Service

License Version Downloads

An abstraction layer for showing message dialogs in .Net applications. This supports WinUI, UWP, Android and iOS.

await _messageDialogService.ShowMessage(ct, mb => mb
   .Title("Oops!")
   .Content("Something went wrong.")
   .OkCommand()
);

Preview

From left to right: WinUI, iOS and Android. alternate text is missing from this package README image

Getting Started

Here is how to setup the service in your WinUI or mobile apps made with Uno Platform. If you want to know how to setup this service for UWP, check out getting started with UWP.

  1. Install the MessageDialogService.Uno.WinUI NuGet package.

  2. Create a MessageDialogService instance.

    using Windows.ApplicationModel.Resources;
    
    // (...)
    
    var currentWindow = yourWindow; // Get the current window.;
    var dispatcherQueue = currentWindow.DispatcherQueue;
    var resourceLoader = ResourceLoader.GetForViewIndependentUse();
    var resourceResolver = resourceKey => resourceLoader.GetString(resourceKey);
    
    var messageDialogService = new MessageDialogService.MessageDialogService(
       dispatcherQueue,   
    #if __IOS__ || __ANDROID__
       new MessageDialogBuilderDelegate(resourceResolver)
    #else
       // On Windows, the builder delegate needs a window handle.
       new MessageDialogBuilderDelegate(
          resourceResolver,
          WinRT.Interop.WindowNative.GetWindowHandle(currentWindow)
       )
    #endif
    );
    
  3. Use the service to prompt a message dialog.

    await messageDialogService.ShowMessage(ct, mb => mb
       .Title("Oops!")
       .Content("Something went wrong.")
       .OkCommand()
    );
    

Next Steps

Localize the Default Buttons

If you plan on using the default commands (such as OkCommand()), you need to localize them. Here are the resource keys for the default commands.

Command Resource Key Suggested Value (en)
OkCommand() MessageDialog_Ok_Label "Ok"
CancelCommand() MessageDialog_Cancel_Label "Cancel"
RetryCommand() MessageDialog_Retry_Label "Retry"
CloseCommand() MessageDialog_Close_Label "Close"

Setup the Service using Dependency Injection

Here is some code showing how to setup the service using Microsoft.Extensions.DependencyInjection.

private static IServiceCollection AddMessageDialog(this IServiceCollection services)
{
   return services
#if __IOS__ || __ANDROID__
      .AddSingleton<IMessageDialogBuilderDelegate>(s => new MessageDialogBuilderDelegate(
         key => s.GetRequiredService<IStringLocalizer>()[key]
      ))
#else
      .AddSingleton<IMessageDialogBuilderDelegate>(s => new MessageDialogBuilderDelegate(
         key => s.GetRequiredService<IStringLocalizer>()[key],
         WinRT.Interop.WindowNative.GetWindowHandle(App.Instance.CurrentWindow)
      ))
#endif
      .AddSingleton<IMessageDialogService, MessageDialogService.MessageDialogService>();
}

Setup the Service in Test Projects

You can use the AcceptOrDefaultMessageDialogService in test projects to simulate a message dialog that always takes the accept or default command.

Here is some code showing how to set it up using Microsoft.Extensions.DependencyInjection. First, you'll need to install the MessageDialogService NuGet package in your test project.

private static IServiceCollection AddTestMessageDialog(this IServiceCollection services)
{
   return services.AddSingleton<IMessageDialogService, AcceptOrDefaultMessageDialogService>();
}

Features

Use Resource Keys for Dialog Content

You can use resource keys for the dialog title and content using TitleResource and ContentResource. This is useful for localization.

await _messageDialogService.ShowMessage(ct, mb => mb
   .TitleResource("GenericErrorTitle")
   .ContentResource("GenericErrorContent")
   .OkCommand()
);

Get the Result of the Dialog

You can get the result of the dialog using ShowMessage. This will return a MessageDialogResult enum value.

var result = await _messageDialogService.ShowMessage(ct, mb => mb
   .Title("Logout")
   .Content("Are you sure you want to logout?")
   .CancelCommand()
   .AcceptCommand(acceptResourceKey: "Logout_Confirm")
);

if (result == MessageDialogResult.Accept)
{
   // Logout
}

Use Your Own Return Type

When MessageDialogResult doesn't cover all your needs, you can use your own return type by using the ShowMessage<TResult> method. This will return a TResult value.

public enum CustomDialogResult
{
   Option1,
   Option2,
   Option3,
   Option4,
   Option5
}

// (...)

var customResult = await _messageDialogService.ShowMessage<CustomDialogResult>(ct, mb => mb
   .Title("Some Custom Title")
   .Content("Some custom content.")
   .Command(CustomDialogResult.Option1, label: "Option 1")
   .Command(CustomDialogResult.Option2, label: "Option 2")
   .Command(CustomDialogResult.Option3, label: "Option 3")
   .Command(CustomDialogResult.Option4, label: "Option 4")
   .Command(CustomDialogResult.Option5, label: "Option 5")
);

switch (customResult)
{
   // Handle the result.
}

Highlight Destructive Commands (iOS Only)

You can use the isDesctructive parameter of the Command and CommandResource methods to make the command red on iOS. This option has no effect on Windows and Android.

var result = await _messageDialogService.ShowMessage(ct, mb => mb
   .Title("Delete")
   .Content("Are you sure you want to delete the selected items?")
   .CancelCommand()
   .Command(MessageDialogResult.Accept, label: "Delete", isDestructive: true)
);

if (result == MessageDialogResult.Accept)
{
   // Delete
}

It will look like this: alternate text is missing from this package README image

Changelog

Please consult the CHANGELOG for more information about version history.

License

This project is licensed under the Apache 2.0 license - see the LICENSE file for details.

Contributing

Please read CONTRIBUTING.md for details on the process for contributing to this project.

Be mindful of our Code of Conduct.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed.  monoandroid11.0 is compatible.  monoandroid12.0 is compatible. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Universal Windows Platform uap10.0.19041 is compatible. 
Xamarin.iOS xamarinios was computed.  xamarinios10 is compatible. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.3-feature.Uno5Update.2 174 11/3/2023
1.0.2 777 9/11/2023
1.0.1 144 9/11/2023
1.0.0 143 9/11/2023
0.6.0-dev.58 2,164 10/26/2022
0.5.0-feature.update-dotnet... 130 8/31/2022
0.5.0-feature.update-dotnet... 149 8/10/2022
0.5.0-feature.update-dotnet... 131 8/10/2022
0.5.0-feature.dotnet6.45 118 9/6/2022
0.5.0-feature.dotnet6.35 115 9/6/2022
0.5.0-dev.56 108 10/18/2022
0.5.0-dev.54 1,549 10/11/2022
0.5.0-dev.52 531 9/28/2022
0.5.0-dev.48 163 9/22/2022
0.5.0-dev.46 141 9/7/2022
0.5.0-dev.35 153 8/23/2022
0.5.0-dev.33 134 8/18/2022
0.5.0-dev.29 146 8/12/2022
0.5.0-dev.14 13,139 5/2/2022
0.5.0-dev.13 53,456 3/30/2022
0.4.0-feature.uno-ui-4.10 167 3/15/2022
0.4.0-feature.uno-ui-4.9 140 3/15/2022
0.4.0-dev.10 409 3/15/2022
0.3.0-feature.uno-ui-4.9 5,179 12/21/2021
0.3.0-dev.8 19,959 3/30/2021
0.3.0-dev.5 19,061 3/17/2021
0.2.0-dev.3 24,645 9/10/2020
0.2.0-dev.2 247 9/9/2020