问题描述
我遇到一个非常奇怪的问题,无法确定是什么原因造成的。
我可以创建一个Controls:MetroWindow并让一个按钮显示simpleChildWindow:ChildWindow,这可以正常工作。问题是当我退出窗口并尝试再次运行脚本时,它将不会加载XAML。我得到的错误是:
Exception calling "Load" with "1" argument(s): "Initialization of 'MahApps.Metro.SimpleChildWindow.ChildWindow' threw an exception."
At line:1 char:1
+ $syncHash.($synchash.app+"_Window") = [Windows.Markup.XamlReader]::Lo ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [],MethodInvocationException
+ FullyQualifiedErrorId : XamlParseException
我尝试了不同版本的MahApps.Metro.dll和MahApps.Metro.SimpleChildWindow.dll,但无法解决此错误。
如果我关闭PowerShell会话并再次启动该脚本,则它可以正常运行,但是在同一会话中第二次尝试运行该脚本时都将失败。
这是我正在运行的脚本:
#Create the Parent SyncHash that each runspace can access
$Global:syncHash = [hashtable]::Synchronized(@{})
$syncHash.scriptRoot = $PSScriptRoot #Pass the base directory (scriptRoot) to the runspace
#region Assemblies to Load
$assemblyList = @("PresentationFramework","PresentationCore","WindowsBase")
$assemblyList | Foreach-Object {Add-Type -AssemblyName $_}
# Mahapps Library Asemblies
Get-ChildItem ($syncHash.scriptRoot + "\assembly") -Filter "*.dll" -File -Recurse| ForEach-Object {Add-Type -Path $_.FullName}
#endregion Assemblies
#region XAML
#Get the Main XAML from file
$xamlText = Get-Content ($syncHash.scriptRoot + "\resources\XML\TestChildForm.xaml")
$syncHash.xamlLoader = (New-Object System.Xml.XmlDocument)
$syncHash.xamlLoader.LoadXml($xamlText)
$newRunspace = [runspacefactory]::CreateRunspace()
$newRunspace.ApartmentState = "STA"
$newRunspace.ThreadOptions = "ReuseThread"
$newRunspace.open()
$newRunspace.SessionStateProxy.Setvariable("syncHash",$syncHash)
#CODE set to add to the New Runspace
$parentCODE = [PowerShell]::Create().AddScript({
$syncHash.reader = (New-Object System.Xml.XmlNodeReader $syncHash.xamlLoader)
$syncHash.Test_Window = [Windows.Markup.XamlReader]::Load( $syncHash.reader )
$syncHash.xamlLoader.SelectNodes("//*[@*[contains(translate(name(.),'n','N'),'Name')]]") | ForEach-Object {
#Find all of the form types and add them as members to the synchash
$syncHash.Add($_.Name,$syncHash.Test_Window.FindName($_.Name) )
}
$syncHash.test_but1.Add_Click({
$syncHash.test_child.IsOpen = $true
})
#Show the GUI running in newRunspace
$syncHash.Test_Window.ShowDialog() | Out-Null
$syncHash.Error = $Error
})
$parentCODE.Runspace = $newRunspace
$data = $parentCODE.BeginInvoke()
While ($data.IsCompleted -ne $true) {Start-Sleep 0.5}
Get-Runspace | Where-Object {$_.Id -eq $newRunspace.Id} | ForEach-Object {$_.dispose()}
位于。\ resources \ XML中的TestChildForm.xaml如下:
<Controls:MetroWindow
xmlns:Controls='clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro'
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
xmlns:simpleChildWindow="clr-namespace:MahApps.Metro.SimpleChildWindow;assembly=MahApps.Metro.SimpleChildWindow"
Title="Test Main Window"
WindowStartupLocation="Manual"
ShowInTaskbar="true" ShowMinButton="False" ShowMaxRestoreButton="False"
SizetoContent="WidthAndHeight" AllowsTransparency="True" ResizeMode="CanResizeWithgrip">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,/MahApps.Metro;component/Styles/Themes/Light.Cobalt.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<simpleChildWindow:ChildWindow Name="test_Child"
CloseByEscape="False"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch"
Padding="15"
ChildWindowImage="Error"
Title="TestChild 1">
<Grid>
<Label Content="Test Child Window"/>
</Grid>
</simpleChildWindow:ChildWindow>
<Button Name="test_but1" Content="Open Child Window" Width="120" Height="40" Margin="5"/>
</Grid>
</Controls:MetroWindow>
通过在Visual Studio中安装nuget软件包,然后将dll复制到项目中的。\ assembly文件夹中,我得到了MahApps dll。 。\ assembly中包含的dll是:
- ControlzEx.dll
- MahApps.Metro.dll
- MahApps.Metro.SimpleChildWindow.dll
- Microsoft.Xaml.BehavIoUrs.dll
- System.Windows.Interactivity.dll
首次运行脚本后,它将显示该窗口,单击该按钮将显示子窗口。运行脚本后检查错误没有显示任何错误。再次运行脚本后,它将引发错误。看来,所有错误均源于加载SimpleChildWindow的问题。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)