Monday 19 March 2018

Mahapps Custom Dialog



When you design your apps, sometimes you need to add a custom content to your dialog form. In this post, I'm going to show you how to create your own custom dialog with PowerShell using Mahapps.

Here is a preview of the result:


You can put whatever content you want inside the dialog. Each dialog in the picture is a custom dialog.


Ø  What do we need?

-        You need PowerShell of course. J
-        All the Mahapps *.dll files
-        And some enthusiasm (Don’t worry there is nothing complicated)

Ø  How it's done?

I recommend you to look at my previous post "how to use built-in mahapps dialog" in case they already met your needs. Otherwise, you’re at the right place.

1) Load Mahapps Assembly

[System.Reflection.Assembly]::LoadFrom('assembly\MahApps.Metro.dll')| out-null
[System.Reflection.Assembly]::LoadFrom('assembly\System.Windows.Interactivity.dll') | out-null

2) Load your Main xaml and the content of your dialog as xaml too.

$xamlDialog  = LoadXml(".\template\Dialog.xaml")
$read=(New-Object System.Xml.XmlNodeReader $xamlDialog)
$DialogForm=[Windows.Markup.XamlReader]::Load( $read )

3) Create a new Mahapps CustomDialog object

This is the important part. This object will receive the form to show. And we specify that it wil be attached to the main form with new($Form).

# Create a new Dialog attached to Main Form
$CustomDialog         = [MahApps.Metro.Controls.Dialogs.CustomDialog]::new($Form)

4) Specify the content of the newly created Custom dialog

We just need to attach the Dialog form to the custom dialog object we just created.
$CustomDialog.AddChild($DialogForm)

5) Show the custom Dialog
To show the dialog, you need to create a dialogSettings object first.

$settings             = [MahApps.Metro.Controls.Dialogs.MetroDialogSettings]::new()
$settings.ColorScheme = [MahApps.Metro.Controls.Dialogs.MetroDialogColorScheme]::Theme

You can directly use the $settings without editing the colorscheme. I just wanted to show an example of how to customize the Dialog Settings.

Now we’re set. You just have to execute (within a button add_click for instance) this line to show the CustomDialog.

# Show custom dialog form
[MahApps.Metro.Controls.Dialogs.DialogManager]::ShowMetroDialogAsync($Form, $CustomDialog, $settings)

     Parameters are:
1-    Window Parent : $Form
2-    CustomDialog object: $CustomDialog
3-    MetroDialogSettings object: $settings

6) Close MahApps Custom Dialog

Everything is fine until now … but how do we close it?
Easy peasy! J Just call RequestCloseAsync() on the CustomDialog object.

    # Close the Custom Dialog
    $CustomDialog.RequestCloseAsync()


Now you are ready to use it everywhere! 

Download Files
Like always, the source code is available here.





Share:

2 comments:

  1. Great job, thank you so much for sharing.
    It seems like Powershell 5x is neded, right? it does not work in Version 4 ?!
    I got this error in PSVersion 4: "Method invocation failed because [MahApps.Metro.Controls.Dialogs.CustomDialog] does not contain a method named 'new'."

    ReplyDelete
    Replies
    1. Hi,
      No, it works with PS version 4 and above.
      It needs .NET 4.5 though. Are you using the assembly provided in my git repos or downloaded your own on mahapps nuget?

      Delete

Popular Posts

Join us on Facebook

STATS

Contact Form

Name

Email *

Message *