Hi,
so i’ve been trying to create my first GUI. For this question i’ve made a testcase. I hava an inputfield, if this -ieq “k” then first print “ok” then change it to “lets go”. At the moment, it only shows Lets go.
$inputXML = @"
"@
$inputXML = $inputXML -replace 'mc:Ignorable="d"','' -replace "x:N",'N' -replace '^<Win.*', '<Window'
[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
[xml]$XAML = $inputXML
#Read XAML
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
try{$Form=[Windows.Markup.XamlReader]::Load( $reader )}
catch{Write-Host "Unable to load Windows.Markup.XamlReader. Double-check syntax and ensure .net is installed."}
#===========================================================================
# Store Form Objects In PowerShell
#===========================================================================
$xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name "WPF$($_.Name)" -Value $Form.FindName($_.Name)}
Function Get-FormVariables{
if ($global:ReadmeDisplay -ne $true){Write-host "If you need to reference this display again, run Get-FormVariables" -ForegroundColor Yellow;$global:ReadmeDisplay=$true}
write-host "Found the following interactable elements from our form" -ForegroundColor Cyan
get-variable WPF*
}
Get-FormVariables
#===========================================================================
# Actually make the objects work
#===========================================================================
$WPFbutton.Add_Click({
$WPFlistView.Items.Clear()
$personeelsnummer = $WPFtextBox.Text
if($WPFtextBox.Text -ieq "k"){
$WPFlabel1.Content = "ok"
$Form.Refresh()
Write-host "Updating Window"
$WPFlabel1.Content = "lets go"
$Form.Refresh()
Write-host "Updating Window 2"
}
})
#debug
$Form | gm | Select Name, Membertype | Out-GridView
$Form.ShowDialog() | out-null
$Form.Refresh() results in :
Method invocation failed because [System.Windows.Window] does not contain a met
hod named 'Refresh'.
At line:90 char:6
+ $Form.Refresh()
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Have you nanaged to get a WPF label to refresh in C#? it should be a matter of copying the method from there really. Very busy day in the office but I’m curious and will investigate later if I have time
You are likely running into a common issue with PowerShell and GUIs. Since PowerShell will by default process your code as a single thread. In a lot of situations, the thread gets to processing something else, so the GUI cannot update. Check out this thread on a similar topic with a progress bar. There is also a link there to an innovative solution so that the GUI runs in a separate processing thread.
I don’t think you will need to start using multiple threads, as in theory you are only running a method to refresh. If you were running this at the same time as trying to run a command which would take 5 seconds to invoke, I’d probably agree.
Powershell can use whatever assemblies you import, and doesn’t lack any of the ability of other languages to actually execute, only how many process it can run simultaneously.
could you please upload your code to pastebin again? I have some free time this afternoon.
Also, in my WinForms, I dot this in certain places, which did fix some issues powershell had with carrying out events:
[System.Windows.Forms.Application]::DoEvents()
WPF may require something else. As I said, if you can upload again, I will have a look.
I will be interested to see what Steven finds, but I can say for sure that I have seen this issue particularly with the start-sleep cmdlet since start-sleep keeps the thread from processing anything while it’s sleeping. I will also say that I don’t usually see this issue with labels. It’s usually more complex form controls that I have seen this on, like progress bars.
I have created a new GUI just for testing purposes.
It only has one button and a label that has to change twices.
If we get this working, i can build my full GUI again.
My application that i needed to make is done by creating a GUI typing out every item of the layout (HTML style).
But i hope we get this working so i can use Visual Studio to create my interfaces, it will save a lot of time.
I was where you were 6 months ago. Once I learnt how to use Powershell syntax inside out (by doing massive complex projects in my spare time). I started on C# and it’s opened up a whole other side of Powershell, namely because you can interact with .NET better.
Good luck
I’m actually looking at getting that method to run in powershell, so I’ll let you know if I get it working.