#How to use App.xaml when rendering WPF GUI in PowerShell core?

5 messages · Page 1 of 1 (latest)

limpid cloud
#

This video (https://www.youtube.com/watch?v=WRWReC2Kvio), 7 minutes in, talk about needing to replace x:name, with name before loading to xmlnodereader...

I have always just used forms, so I had to see this WPF for myself 🙂

Run WPF GUI with a PowerShell script on Windows.
WANT TO SUPPORT?
💰 Patreon: https://www.patreon.com/agiledevart


00:00 Introduction
01:02 Reasons why running GUI scripts
02:06 Running a CoinBrowser WPF powershell script
04:18 Powershell script execution policy enable/disable
05:19 Powershell script line by line
06:42 Parsing the XAML file ...

â–¶ Play video
wary smelt
#

In my experience, WPF doesn't like relative paths when loaded in PowerShell. Instead of pack://application:,,,/App.xaml try passing the full path to App.xaml.

upper rock
#

generally, you can't use pack:// if you didn't pack it 😉

tiny topaz
# limpid cloud This video (https://www.youtube.com/watch?v=WRWReC2Kvio), 7 minutes in, talk abo...

You can use this to grab the x:Name attributes 🙂

$nsManager = [System.Xml.XmlNamespaceManager]::New($xaml.NameTable)
$nsManager.AddNamespace("x", "http://schemas.microsoft.com/winfx/2006/xaml")

foreach ($node in $xaml.SelectNodes("//*[@x:Name or @Name]", $nsManager)) {
  try {
    Set-Variable -Name ($node.Name) -Value $Form.FindName($node.Name)
  }
  catch {
    Write-Host "Error setting variable:`n$_"
  }
}
shadow ruin
#

You might get better type completions if you replace new-object with the type name.
( New-object is the older style, before they added the ::new() syntax )

# before
$MainReader = New-Object -TypeName 'System.Xml.XmlNodeReader' -ArgumentList $MainXAML
$MainWindow = [Windows.Markup.XamlReader]::Load($MainReader)

# after 
[Xml.XmlNodeReader] $MainReader = $MainXAML 
$MainWindow = [Windows.Markup.XamlReader]::Load( $MainReader )

# if you want to explicitly call the constructor, call the 'new' method
$MainReader = [Xml.XmlNodeReader]::new( $MainXAML )