ive got the below PS script created its so far the only way i could get runspaces working, but when i build as an exe and run it, the WPF doesnt open i just get a blank Exe black screen?
The ps1 launches from Vs19 and from powershell console?
function Get-XamlObject {
[CmdletBinding()]
param(
[Parameter(Position = 0,
Mandatory = $true,
ValuefromPipelineByPropertyName = $true,
ValuefromPipeline = $true)]
[Alias(“FullName”)]
[System.String[]]$Path
)
BEGIN
{
Set-StrictMode -Version Latest
$expandedParams = $null
$PSBoundParameters.GetEnumerator() | ForEach-Object { $expandedParams += ' -' + $_.key + ' '; $expandedParams += $_.value }
Write-Verbose "Starting: $($MyInvocation.MyCommand.Name)$expandedParams"
$output = @{ }
#Add-Type -AssemblyName presentationframework, presentationcore
#Add in the frameworks so that we can create the WPF GUI
[System.Reflection.Assembly]::LoadWithPartialName('PresentationFramework') | Out-Null
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
} #BEGIN
PROCESS {
try
{
foreach ($xamlFile in $Path)
{
#Change content of Xaml file to be a set of powershell GUI objects
$inputXML = Get-Content -Path $xamlFile -ErrorAction Stop
[xml]$xaml = $inputXML -replace 'mc:Ignorable="d"', '' -replace "x:N", 'N' -replace 'x:Class=".*?"', '' -replace 'd:DesignHeight="\d*?"', '' -replace 'd:DesignWidth="\d*?"', ''
$tempform = [Windows.Markup.XamlReader]::Load((New-Object System.Xml.XmlNodeReader $xaml -ErrorAction Stop))
#Grab named objects from tree and put in a flat structure using Xpath
$namedNodes = $xaml.SelectNodes("//*[@*[contains(translate(name(.),'n','N'),'Name')]]")
$namedNodes | ForEach-Object {
$output.Add($_.Name, $tempform.FindName($_.Name))
} #foreach-object
} #foreach xamlpath
} #try
catch
{
throw $error[0]
} #catch
} #PROCESS
END
{
Write-Output $output
Write-Verbose "Finished: $($MyInvocation.Mycommand)"
} #END
}
#Set Starting path and create Synchronised hash table to be read across multiple runspaces
$script:syncHash = [hashtable]::Synchronized(@{})
$syncHash.path = $PSScriptRoot #Join-path $PSScriptRoot #‘add folder if needed’
#Load function(s) into Sessionstate object for injection into runspace
$ssGetXamlObject = Get-Content Function:\Get-XamlObject -ErrorAction Stop
$ssfeGetXamlObject = New-Object System.Management.Automation.Runspaces.SessionStateFunctionEntry -ArgumentList ‘Get-XamlObject’, $ssGetXamlObject
#Add Function(s) to session state
$InitialSessionState = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault()
$InitialSessionState.Commands.Add($ssfeGetXamlObject)
#Add Session State to runspace at creation
$runspace = [runspacefactory]::CreateRunspace($InitialSessionState)
$powerShell = [powershell]::Create()
$powerShell.runspace = $runspace
#Helps to prevent memory leaks, show runspace config in console
$runspace.ThreadOptions = “ReuseThread”
#Needs to be in STA mode for WPF to work
$runspace.ApartmentState = “STA”
$runspace.Open()
$runspace.SessionStateProxy.SetVariable(“syncHash”,$syncHash)
[void]$powerShell.Addscript({
$wpf = Get-ChildItem -Path $syncHash.path -Filter *.xaml -file | Where-Object { $_.Name -ne 'App.xaml' } | Get-XamlObject
$wpf.GetEnumerator() | ForEach-Object {$script:SyncHash.add($_.name,$_.value)} #Add all WPF objects to synchash variable
# region Checkboxes
# gcse chkbox
$syncHash.gcse_chkbox.add_checked({
# disable and clear otherexam chkbox
$syncHash.otherexam_chkbox.IsEnabled = $false
$syncHash.otherexam_chkbox.IsChecked = $false
# enable location chkboxes
$syncHash.colchester_chkbox.IsEnabled = $true
$syncHash.braintree_chkbox.IsEnabled = $true
$syncHash.clacton_chkbox.IsEnabled = $true
})
$syncHash.gcse_chkbox.add_unchecked({
$syncHash.otherexam_chkbox.IsEnabled = $true
$syncHash.otherexam_chkbox.IsChecked = $false
$syncHash.colchester_chkbox.IsEnabled = $False
$syncHash.colchester_chkbox.Ischecked = $false
$syncHash.braintree_chkbox.IsEnabled = $false
$syncHash.braintree_chkbox.Ischecked = $false
$syncHash.clacton_chkbox.IsEnabled = $false
$syncHash.clacton_chkbox.Ischecked = $false
$syncHash.copyfiles_chkbox.IsEnabled = $false
$syncHash.copyfiles_chkbox.IsChecked = $false
$syncHash.deletefiles_chkbox.IsEnabled = $false
$syncHash.deletefiles_chkbox.Ischecked = $false
$syncHash.startrange_txtbox.IsEnabled = $false
$syncHash.startrange_textbox.clear()
$syncHash.endrange_txtbox.Clear()
$syncHash.files_listbox.IsEnabled = $false
$syncHash.files_listbox.clear()
$syncHash.files_btn.IsEnabled = $false
$syncHash.folder_btn.IsEnabled = $false
})
$script:syncHash.exams_window.ShowDialog() | Out-Null
#end of [void]$powershell.addscript
})
$AsyncObject = $PowerShell.BeginInvoke()
#===================================================================