Datastandard – Cool custom dialog from menu

Let’s create your own custom dialog without any help of Datastandard and still with PowerShell!Webp.net-gifmaker (3)

 

 

The issue with dialogs from Datastandard

I am sure you have faced a very similar situation like I did:
My initial goal was to create a very simple dialog for just showing some data when the users clicks on my context menu entry.

But if you are using the dialogs from Datastandard they come with some logic, depending on the method you call, some examples:

  1. GetCreateDialog($folderId): It creates always a new file and you have to specify a Template
  2. GetEditFolderDialog($folderId): It creates a new folder or updates the properties

The other dialogs also have similar features, but what if you don’t need any of those features?

 

The coolDialog as solution

1. Create your XAML

First, we have to provide a XAML file (What is that?) with our needs which we can later load in PowerShell.
The root must be the Window element and here is an example of “The coolDialog”.

2. Load your XAML in PowerShell

If you have now a valid XAML file with the <Window> as root, then you can load it with one fo the overloads of the static XamlReader.Load methods, like this snippet:

[xml]$xamlContent = Get-Content "$($env:ProgramData)\Autodesk\Vault 2018\Extensions\DataStandard\Vault.Custom\addinVault\Menus\TheCoolDialog.xaml"
$theCoolWindow = [Windows.Markup.XamlReader]::Load((New-Object System.Xml.XmlNodeReader -ArgumentList @($xamlContent)))

If you execute this snippet it will NOT show the window! For accomplishing that you need to call ShowDialog().
After you copy this code you need to replace the path to your XAML file!

3. Fill the dialog with data

Of course, you want to fill your dialog with some dynamic data. In my example of the coolDialog there is a TextBox where the Text is bound to the property FullName: This means the TextBox expects to have access to an object with the Property FullName

After the first 2 steps the dialog has no data, therefore the Text will be empty.

Now, we give an object, in my case a Vault Folder to the DataContext of the parent control:

    $folderId = $vaultContext.CurrentSelectionSet | select -First 1 -ExpandProperty "Id"
    if(-not $folderId) {
        throw "No valid folder selected!"
    }
    $fldr = $vault.DocumentService.GetFolderById($folderId)
    $folder = New-Object Autodesk.DataManagement.Client.Framework.Vault.Currency.Entities.Folder -ArgumentList @($vaultConnection, $fldr)
    $theCoolWindow.FindName("StackPanelCoolView").DataContext = $folder

The folder has a property FullName and therefore that value will be shown in the TextBox.

4. Handle events

I am pretty sure you want to have something happen when the customer clicks a button, therefore we look to handle events now.

In PowerShell, you can subscribe to every available event of a control by calling the method add_<EventName>($scriptBlock).

Let’s extend our coolDialog so it will close when clicking on the Close Button:

    $theCoolWindow.FindName("BtnCancel").add_Click({
        $theCoolWindow.Close()
    })

Tip: If you want to see all available events for a certain control, like for a Button just google “wpf button” and goto the related msdn page.

5. Bring your window to life

The last step is to let the window appear as a Graphical User Interface visible to the customer.
This can be easily accomplished by calling ShowDialog():

$theCoolWindow.ShowDialog() | Out-Null

Why do I pipe it through Out-Null you ask? Because ShowDialog returns a result and I don’t handle it. If you want to handle depending on how the user exited the dialog read these Remarks.

The full version of the PowerShell script for the coolDialog you can download here.
But remember this example was programmed for a menu entry in Datastandard for Vault 2018.2
You will find all downloads at the end of this post!


Troubleshooting

Props[], PSCmd[], PSVal.. do not work in my XAML

All of Datastandard features what you usually use to bind in your Dialog are NOT working in a custom dialog. Here you can see all the Binding which are affected!

 

Events not working (Vault crash?)

I could identify why, but sometimes the scriptBlock of the event has no access to the variables outside of that scope.
The solution is to mark the variables as global which are initialized out of the event and are also used within the Event:

$global:theCoolWindow = [Windows.Markup.XamlReader]::Load((New-Object System.Xml.XmlNodeReader -ArgumentList @($xamlContent)))
$theCoolWindow.FindName("BtnCancel").add_Click({
        $global:theCoolWindow.Close()
    })

It depends on your environment but this could be also a cause that your Application crashed.

ShowDialog() vs Show()

 

Do not call Show(), it will work in the first place by showing your window. BUT every $scriptBlock triggered by an event will throw an exception and will sometimes crash your Application.
Be sure to use the ShowDialog() method!


 

Whatever, I congratulate you to your custom dialog where you have the total freedom of adding controls and the logic behind!

 

Happy developing,

Patrick

 

Downloads

This example was programmed for a menu entry in Datastandard for Vault 2018.2

This entry was posted in Data Standard. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s