Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF&x2B;XAML+;数据网格&x2B;Powershell 2.0_Wpf_Xaml_Powershell_Datagrid - Fatal编程技术网

WPF&x2B;XAML+;数据网格&x2B;Powershell 2.0

WPF&x2B;XAML+;数据网格&x2B;Powershell 2.0,wpf,xaml,powershell,datagrid,Wpf,Xaml,Powershell,Datagrid,我试图在WPF中放置一个数据网格。在Powershell中非常简单。。但是当我在Powershell 2.0中运行代码时,它找不到错误 示例代码: Add-Type -AssemblyName PresentationFramework $xaml = [xml] @" <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsof

我试图在WPF中放置一个数据网格。在Powershell中非常简单。。但是当我在Powershell 2.0中运行代码时,它找不到错误

示例代码:

Add-Type -AssemblyName PresentationFramework

$xaml = [xml] @"
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="My window" Height="300" Width="300">
<DockPanel>
    <Button x:Name="okButton" Content="OK" DockPanel.Dock="Bottom" />
    <DataGrid x:Name="DataGrid" DockPanel.Dock="Top"/>
</DockPanel>
</Window>
"@

$reader = New-Object System.Xml.XmlNodeReader $xaml
$form = [Windows.Markup.XamlReader]::Load($reader)

$okButton = $form.FindName("okButton")

$okButton.add_Click({ $form.Close() })

$form.WindowStartupLocation = "CenterScreen"
$form.ShowDialog();
添加类型-AssemblyName PresentationFramework
$xaml=[xml]@”
"@
$reader=New Object System.Xml.XmlNodeReader$xaml
$form=[Windows.Markup.XamlReader]::加载($reader)
$okButton=$form.FindName(“okButton”)
$okButton.add_单击({$form.Close()})
$form.WindowStartupLocation=“中心屏幕”
$form.ShowDialog();
如果删除此行,一切正常:

<DataGrid x:Name="DataGrid" DockPanel.Dock="Top"/>

有人有什么建议吗

我在Powershell 2.0中遇到错误:

使用“1”参数调用“Load”时出现异常:“标记'DataGrid' XML命名空间“”中不存在 /2006/xaml/presentation'。行“0”位置“0”。“第15行字符:42
+$form=[Windows.Markup.XamlReader]::Load默认情况下,PowerShell 2.0将使用.NET framework 2.0。在.NETV2.0中没有数据网格。类型:

[environment]::Version
如果你得到了这个,或者其他一些从2开始的东西

Major  Minor  Build  Revision
-----  -----  -----  --------
2      0      50727  7905
您的PowerShell会话正在使用NET 2.0

编辑:

要在PS 2.0上修复此问题

使用以下内容创建文件C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe.config和C:\Windows\System32\WindowsPowerShell\v1.0\powershell\U ise.exe.config:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <startup useLegacyV2RuntimeActivationPolicy="true"> 
        <supportedRuntime version="v4.0" />    
    </startup> 
</configuration>

假设已加载.NET 4。

您会收到哪些确切错误?我脑海中有两个可能的选项:不同的.net版本,或者需要加载其他引用,或者两者都有。请参阅更新的问题,错误是..在PS 4.0、.net 4.5中工作正常。你有什么版本的.NET?我知道它有。。问题是为什么它在PS 2.0中不起作用。。我的环境仅为PS 2.0..:/不必太激动;)我想说的是,这个错误似乎是特定于PowerShell版本2的。我还可以确认.NET版本并不重要。很好地回答了为什么它不起作用。。但是为什么我能够在
System.Windows.Forms
中使用:
新对象System.Windows.Forms.DataGridView
?System.Windows.Forms和PresentationFramework是两种截然不同的动物。您的代码加载
System.Windows.Controls.DataGrid
。我假设我无法在Powershell 2.0中加载
System.Windows.Controls.DataGrid
。。对吧?:'(是的,这是正确的。要修复它,您必须拥有.NET 4和fix PS 2才能使用它。
Add-Type -Path "C:\Windows\Microsoft.NET\Framework\v4.0.30319\WPF\PresentationFramework.dll"