This is a combination of multiple stuff that I’ve found online. What I’ve done is combining, adapting and made functions out of them. https://www.dev4sys.com/2016/06/powershellwpf-part-iii-how-to-load.html https://smsagent.blog/2017/08/24/a-customisable-wpf-messagebox-for-powershell/ https://mahapps.com/
And just to make it clear, this code is a concept one, I’ve excluded stuff in some of the functions on purpose.
So, one of the things that took a lot of my time doing this was to figuring out that if you’re adding an object after the XAML is loaded, you also need to register it. As with the TextBlock object in this example: $TextBlock.RegisterName("TextBlock1",$TextBlock) Else you won’t be able to invoke changes to it and you might get errors like: “Exception calling "Invoke" with "1" argument(s): "The property 'Text' cannot be found on this object. Verify that the property exists and can be set."
I’ve also tried to find a way to make a function that can be called from within another function and that executes the calling function again using Dispatcher.Invoke. Unsuccessfully so far though.
Works:
Function run2 ($strCommand)
{
$ret=Invoke-Expression $strCommand
write-host $ret
if ($ret -contains 444)
{
write-host "444"
$d=$hash.Window.Dispatcher.Invoke(
{
try
{ ChangeTextBlock -BlockName "TextBlock1" -Text "Run2" –DynColor AliceBlue
}
catch
{
write-host $_
}
}
),
"Normal"
}
}
Doesn’t Work: Function Run3 ($strCommand)
{
$ret=Invoke-Expression $strCommand
write-host $ret
if ($ret -contains 444)
{
write-host "444"
$d=$hash.Window.Dispatcher.Invoke(
{
try
{
Invoke-Expression -Command "$strCommand" -ErrorAction SilentlyContinue
}
catch
{
write-host $_
}
}
),
"Normal"
}
}
Run3 'ChangeTextBlock -BlockName "TextBlock1" -Text "Run3" -DynColor AliceBlue' | Out-Null
Comments