我有这个概念验证的 powershell 脚本,它将以其当前的形式工作,并将显示一个空白对话框,但以 wpf-ui 库为主题。
如果我将 $reader = (New-Object System.Xml.XmlNodeReader $xaml) 更改为 $reader = (New-Object System.Xml.XmlNodeReader $xaml2) 那么它会添加与 $xaml 相同的 xaml 代码,但来自文件它不会打开对话框。
我尝试了各种方法让它发挥作用,但似乎不明白为什么。
$Global:syncHash = [hashtable]::Synchronized(@{})
$newRunspace = [runspacefactory]::CreateRunspace()
$newRunspace.ApartmentState = "STA"
$newRunspace.ThreadOptions = "ReuseThread"
$newRunspace.Open()
$newRunspace.SessionStateProxy.SetVariable("syncHash", $syncHash)
$Global:Current_Folder = Split-Path $MyInvocation.MyCommand.Path
Add-Type -Path "$Current_Folder\Wpf.Ui.dll"
Add-Type -AssemblyName PresentationFramework
$psCmd = [PowerShell]::Create().AddScript({
[xml]$xaml = @"
<ui:FluentWindow xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
xmlns:controls="clr-namespace:Wpf.Ui.Controls" Title="WPF UI" Width="800" Height="500" WindowStartupLocation="CenterScreen">
<ui:FluentWindow.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Wpf.Ui;component/Controls/FluentWindow/FluentWindow.xaml" />
<ResourceDictionary Source="pack://application:,,,/Wpf.Ui;component/Controls/TitleBar/TitleBar.xaml" />
<ui:ThemesDictionary Theme="Dark" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</ui:FluentWindow.Resources>
<Grid>
<ui:TitleBar Name="TitleBar" Title="Test App" Margin="0" ShowMaximize="False" ShowMinimize="False">
</ui:TitleBar>
</Grid>
</ui:FluentWindow>
"@
function LoadXaml($filename) {
$XamlLoader = New-Object System.Xml.XmlDocument
$XamlLoader.Load($filename)
return $XamlLoader
}
$xaml2 = LoadXaml "$Current_Folder\simplegui.xaml"
$reader = (New-Object System.Xml.XmlNodeReader $xaml)
$syncHash.Window = [Windows.Markup.XamlReader]::Load( $reader )
$syncHash.Window.ShowDialog() | Out-Null
$syncHash.Error = $Error
})
$psCmd.Runspace = $newRunspace
$psCmd.BeginInvoke()
工作代码
$Global:syncHash = [hashtable]::Synchronized(@{})
$newRunspace = [runspacefactory]::CreateRunspace()
$newRunspace.ApartmentState = "STA"
$newRunspace.ThreadOptions = "ReuseThread"
$newRunspace.Open()
$newRunspace.SessionStateProxy.SetVariable("syncHash", $syncHash)
$Global:Current_Folder = Split-Path $MyInvocation.MyCommand.Path
Add-Type -Path "$Current_Folder\Wpf.Ui.dll"
Add-Type -AssemblyName PresentationFramework
$psCmd = [PowerShell]::Create().AddScript({
param($FolderPath)
function LoadXaml($filename) {
$XamlLoader = New-Object System.Xml.XmlDocument
$XamlLoader.Load($filename)
return $XamlLoader
}
$xaml = LoadXaml "$FolderPath\simplegui.xaml"
$reader = (New-Object System.Xml.XmlNodeReader $xaml)
$syncHash.Window = [Windows.Markup.XamlReader]::Load( $reader )
$syncHash.Window.ShowDialog() | Out-Null
$syncHash.Error = $Error
})
$psCmd.Runspace = $newRunspace
$psCmd.AddParameter("FolderPath", $Current_Folder)
$psCmd.BeginInvoke()