【问题标题】:Powershell can't load Windows.Markup.XamlReaderPowershell 无法加载 Windows.Markup.XamlReader
【发布时间】:2016-09-08 08:59:16
【问题描述】:

我刚刚从 foxdeploy.com 获取示例代码来创建一个带有 GUI 的 PowerShell 脚本。

它可以在 Windows 10 机器上运行,但由于某种原因它不能在 Windows 7 或 Windows Server 2008 R2 上运行,它应该在最后运行(不是这个代码,而是带有 GUI 的最终 PS 脚本)。

代码如下:

#ERASE ALL THIS AND PUT XAML BELOW between the @" "@
$inputXML = @"
<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication2"
        mc:Ignorable="d"
        Title="FoxDeploy Awesome Tool" Height="416.794" Width="598.474" Topmost="True">
    <Grid Margin="0,0,45,0">
        <Image x:Name="image" HorizontalAlignment="Left" Height="100" Margin="24,28,0,0" VerticalAlignment="Top" Width="100" Source="C:\Users\Stephen\Dropbox\Docs\blog\foxdeploy favicon.png"/>
        <TextBlock x:Name="textBlock" HorizontalAlignment="Left" Height="100" Margin="174,28,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="282" FontSize="16"><Run Text="Use this tool to find out all sorts of useful disk information, and also to get rich input from your scripts and tools"/><InlineUIContainer>
                <TextBlock x:Name="textBlock1" TextWrapping="Wrap" Text="TextBlock"/>
            </InlineUIContainer></TextBlock>
        <Button x:Name="button" Content="get-DiskInfo" HorizontalAlignment="Left" Height="35" Margin="393,144,0,0" VerticalAlignment="Top" Width="121" FontSize="18.667"/>
        <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="35" Margin="186,144,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="168" FontSize="16"/>
        <Label x:Name="label" Content="ComputerName" HorizontalAlignment="Left" Height="46" Margin="24,144,0,0" VerticalAlignment="Top" Width="138" FontSize="16"/>
        <ListView x:Name="listView" HorizontalAlignment="Left" Height="156" Margin="24,195,0,0" VerticalAlignment="Top" Width="511" FontSize="16">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Drive Letter" DisplayMemberBinding ="{Binding 'Drive Letter'}" Width="120"/>
                    <GridViewColumn Header="Drive Label" DisplayMemberBinding ="{Binding 'Drive Label'}" Width="120"/>
                    <GridViewColumn Header="Size(MB)" DisplayMemberBinding ="{Binding Size(MB)}" Width="120"/>
                    <GridViewColumn Header="FreeSpace%" DisplayMemberBinding ="{Binding FreeSpace%}" Width="120"/>
                </GridView>
            </ListView.View>
        </ListView>

    </Grid>
</Window>

"@       

$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
#===========================================================================

Function Get-DiskInfo {
param($computername =$env:COMPUTERNAME)

Get-WMIObject Win32_logicaldisk -ComputerName $computername | Select-Object @{Name='ComputerName';Ex={$computername}},`
                                                                    @{Name=‘Drive Letter‘;Expression={$_.DeviceID}},`
                                                                    @{Name=‘Drive Label’;Expression={$_.VolumeName}},`
                                                                    @{Name=‘Size(MB)’;Expression={[int]($_.Size / 1MB)}},`
                                                                    @{Name=‘FreeSpace%’;Expression={[math]::Round($_.FreeSpace / $_.Size,2)*100}}
                                                                 }

$WPFtextBox.Text = $env:COMPUTERNAME

$WPFbutton.Add_Click({
$WPFlistView.Items.Clear()
start-sleep -Milliseconds 840
Get-DiskInfo -computername $WPFtextBox.Text | % {$WPFlistView.AddChild($_)}
})
#Sample entry of how to add data to a field

#$vmpicklistView.items.Add([pscustomobject]@{'VMName'=($_).Name;Status=$_.Status;Other="Yes"})

#===========================================================================
# Shows the form
#===========================================================================
write-host "To show the form, run the following" -ForegroundColor Cyan
$Form.ShowDialog() | out-null

脚本似乎在 $Form.FindName 的第 49 行失败

$xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name "WPF$($_.Name)" -Value $Form.FindName($_.Name)}

到目前为止,我注意到的是 $Form 在 Windows 7 上不返回任何内容,而在 Windows 10 上它返回一些属性。所以我认为真正的问题在第 42 行:

 try{$Form=[Windows.Markup.XamlReader]::Load( $reader )}
catch{Write-Host "Unable to load Windows.Markup.XamlReader. Double-check syntax and ensure .net is installed."}

当我将脚本执行到第 43 行时,它会返回捕获消息。

那么我该如何解决这个问题?安装了最新的 .NET 版本,我想语法应该没问题,因为它适用于 Wondows 10。

【问题讨论】:

  • 如果删除Try/Catch 块会出现什么错误?
  • 尝试在 Win7+PS5.1 上运行代码,没有发现错误:puu.sh/r40Wp/c08f57cf1f.png 按钮有效。
  • @MathiasR.Jessen 我的安装是德语,但应该是“您不能在空值表达式上调用方法”错误(我之前在同一行得到的相同)跨度>
  • @wOxxOm 看起来这就是问题所在,我安装了 PS2.0,更新到 4.0 并且成功了!没想到。

标签: wpf windows xaml powershell


【解决方案1】:

我遇到了同样的问题。我在 VS 2015 Enterprise 中设计了一个表单。 XAML 与您的(FoxDeploy 教程)完全相同。在我的个人客户端(Windows 7、Powershell 4.0、.NET Framework 4.0)上运行良好。但是当我试图在 Windows Server 2008 R2(Powershell 2.0、.NET 3.5)上启动脚本时,我遇到了和你一样的问题。更新 .NET 和 Powershell 版本不是一种意见,因为它是一个客户使用服务的生产系统。所以我找到了一种解决方法来在这个环境中运行我的 XAML:

我由此编辑了 XAML“窗口”标签:

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApplication2"
    mc:Ignorable="d"
    Title="FoxDeploy Awesome Tool" Height="416.794" Width="598.474" Topmost="True">

到这里:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="FoxDeploy Awesome Tool" Height="416.794" Width="598.474" Topmost="True">

在此之后作为最后一步,我必须在单线程单元模式下运行 powershell。

powershell -sta

Powershell ISE 已经默认处于此模式。

我无法解释为什么删除的代码会导致系统上发生此错误,但也许我可以帮助其他一些不得不在“旧”Windows 环境中使用 XAML 和 Powershell 的人。

【讨论】:

  • 我在 Windows 10 中尝试过 Powershell -sta 但仍然无法使用,你明白为什么吗? [xml]$xaml = @" schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="schemas.microsoft.com/winfx/2006/xaml"> "@ $reader=(New-Object System.Xml.XmlNodeReader $xaml) $Form=[Windows.Markup.XamlReader]::Load( $reader ) $WebBrowser = $Form.FindName("WebBrowser")
【解决方案2】:

我知道这已经晚了几年。但我只是在使用 WPF/Powershell 时遇到了这个问题。

我最终检查并删除了 groups 中的每个项目,然后一个接一个地查看它是否是导致此错误的对象之一。

原来,当我在 Visual Studio 中设计我的 WPF 时,我双击了一个,我知道我这样做了……只是不认为它会搞砸任何事情。

“TextChanged="EndHour_txtBox_Copy_TextChanged" 行是我引发此错误的原因。只是将来需要注意的事情。并非所有 WPF 项目都可以开箱即用。

【讨论】:

    【解决方案3】:

    [Windows.Markup.XamlReader] 需要 PresentationFramework 程序集,因此请在文件开头添加以下代码:

    Add-Type -Assembly PresentationFramework
    

    【讨论】:

    • 要检查特定环境中加载的程序集,请使用以下代码:[System.AppDomain]::CurrentDomain.GetAssemblies() | ? Location | Select Name, Location, Version
    猜你喜欢
    • 2013-06-05
    • 1970-01-01
    • 2017-09-22
    • 1970-01-01
    • 1970-01-01
    • 2013-04-27
    • 2020-07-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多