Loading...

Modern dialog with PowerShell and WPF

Reply A+ A-

In this post I will show you the new version of the CustomDialogs library, that allows you to display cool dialog box, with PowerShell and WPF, to replace your od school message box.

How to get sample ?


What is CustomDialogs ?
The library I will talk about it is called SimpleDialog.
It has been created by Maurício Vielmo Schmaedeck who did a really nice job.
You can find the GitHub repo here.
You can find my first post here.

How it looks like ?
The library is composed of different kind of dialogs.
- Information
- Success 
- Warning
- Error
- Critical error
- Progress
- Input

In this step I guess you already have a ps1 and XAML files. If not you can find them in the download link.

Download SimpleDialog
1 / Download the Nuget.exe file. You can find it here.
2 / Copy the exe in a folder
3 / Open PowerShell and go to the folder containing the exe
4 / Type the below command:
5 / Go in the folder SimpleDialogs.2.0.1-alpha\lib\net45 

6 / There you can find the dll to use in your project, CustomDialogs.dll
7 / Create an assembly folder in your project
8 / Copy the CustomDialogs.dll in the assembly folder

How to integrate it ?
XAML part
1 / Copy the below lines in the Window part.
2 / See below my Window part after
3 / Add the below Resources part after the Window part.
4 / Add the DialogContainer Control below at the end of your XAML, before the </Grid>
Like on Windows 10 and MahApps, you can display your dialog depending of two theme.
- Light
- Dark

This can be manage in the resources part. In our example we have configured it to Light.
To change it to Dark just change Light with Dark, as below:
See below a dialog with Light theme:

See below a dialog with Dark theme:


PS1 part
1 / Add the below line, at the beginning of your script, to load the assembly
2 / Your dialog will be displayed after clicking on a button.

How to use it ?
As mentioned before the dialog should be displayed after clicking on a button.
In my XAML I added three buttons.
See below the GUI:

The simpleDialog is composed of different dialogs which are divised in three classes:
- MessageDialog
- InputDialog 
- ProgressDialog

To use one of them you will first need to create the class as below:
Then use the below line to display the dialog
Message
Input
Progress

See below dialog in action.

So far, we have seen how to create and display dialog, now let's see how to customize them in order to change the dialog category (information, sucess, warning...), message, title, color...

How to customize a Dialogs ?
You can customize easily your dialog using some attributes as below:

Change title
To change the dialog title use the Title attribute as below:
See below the dialog in action:

Change message
To change the dialog message use the Message attribute as below:
See below the dialog in action:

Change Severity
The MessageDialog class allows you to display different kind of dialog:
- Information
- Success
- Warning
- Error
- Critical

By default without using one of those category, the dialog looks like as below:
You can display a specific category easily using the MessageSeverity attribute on your dialog.
For instance to display a critical error dialog set MessageSeverity to Critical.
See below the PowerShell script in the button:
See below the dialog in action:

See below how looks like other dialogs:


Change dialog buttons
You can display one, two or three button in your dialog.
For that use the following attributes and set them to True or False
- ShowFirstButton
- ShowSecondButton
- ShowThirdButton

In the below example I display two buttons:
See below the dialog:

To get the selected button you will have to use the Add_ButtonClicked event.
In the below example my dialog contains two buttons OK and cancel.
I want to know which button has been clicked.

See below the dialog code to do that:
Change color
To change the dialog color use the Title attribute as below:
See below the dialog in action:

Display a countdown
You can display a countdown inside a button.
For that use the attribute, as below:
See below the dialog in action:

InputDialog: get the typed text
To use an Inputdialog proceed as for the MessageDialog:
- Create the InputDialog class
- Set the MessageSeverity to Critical
- Display the dialog

See below PowerShell code to do that:
See below how to get what use has typed in the input dialog:
See below the dialog in action:


Get Dialog properties, event, method
Each dialog has multiple attributes to customize it.
To list all available properties proceed as beelow:
1 / Open PowerShell ISE
2 / Type the below line depending of the dialog to use:
3 / All properties will be listed. For instance, see below properties available for a MessageDialog

To list all informations about a dialog (event, method, properties) proceed as below:
See below an overview for the MessageDialog:




WPF_Progress 6452982702853008670

Enregistrer un commentaire

Accueil item

Award

Learn KQL in one month

Sponsors

You want to support me ?

Mes articles en français

Books in French


Stats