首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Powershell无法加载Windows.Markup.XamlReader

Powershell无法加载Windows.Markup.XamlReader
EN

Stack Overflow用户
提问于 2016-09-08 16:59:16
回答 3查看 6.3K关注 0票数 1

我只是取自foxdeploy.com的示例代码来创建一个带有图形用户界面的PowerShell脚本。

它可以在Windows10机器上运行,但由于某种原因,它不能在Windows7或Windows Server2008 R2上运行,因为它应该在最后运行(不是这段代码,而是带有图形用户界面的最终PS脚本)。

代码如下:

代码语言:javascript
复制
#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行失败

代码语言:javascript
复制
$xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name "WPF$($_.Name)" -Value $Form.FindName($_.Name)}

到目前为止,我注意到的是,$Form在Windows7上不会返回任何东西,而在Windows10上它会返回一些属性。所以我认为真正的问题在第42行:

代码语言:javascript
复制
 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行执行脚本时,它返回catch-message。

那么我该如何解决这个问题呢?安装了最新的.NET版本,我猜语法应该没问题,因为它可以在Windows10上运行。

EN

回答 3

Stack Overflow用户

发布于 2016-09-14 21:34:15

我也有同样的问题。我在VS 2015 Enterprise中设计了一个表单。XAML与您的(来自FoxDeploy的教程)完全相同。在我的个人客户端(Windows7,Powershell4.0,.NET Framework4.0)上,它工作得很好。但是,当我试图在Windows Server2008 R2 (Powershell2.0,.NET 3.5)上启动该脚本时,我遇到了与您相同的问题。更新.NET和Powershell版本不是一个观点,因为它是一个客户使用服务的高效系统。因此,我找到了一个解决方法来在这个环境中运行我的XAML:

我从下面编辑了XAML "window“标记:

代码语言:javascript
复制
<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">

要这样做:

代码语言:javascript
复制
<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。

代码语言:javascript
复制
powershell -sta

默认情况下,Powershell ISE已处于此模式。

我不能解释为什么删除的代码会在系统上发生这个错误,但也许我可以帮助其他人,他们不得不在“旧”windows环境中与XAML和Powershell作斗争。

票数 2
EN

Stack Overflow用户

发布于 2019-04-15 02:13:04

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

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

原来,当我在visual studio中设计我的WPF时,我双击了一个,我知道我确实做到了……我只是不认为这会把事情搞砸。

“WPF”EndHour_txtBox_Copy_TextChanged“行是我抛出这个错误的原因。只是在未来需要注意的东西。并不是所有的WPF项目都可以开箱即用。

票数 0
EN

Stack Overflow用户

发布于 2021-12-15 22:15:07

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

代码语言:javascript
复制
Add-Type -Assembly PresentationFramework
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39386687

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档