简体   繁体   English

WPF + XAML + DataGrid + Powershell 2.0

[英]WPF + XAML + DataGrid + Powershell 2.0

I'm trying to put a data grid in a WPF. 我试图将数据网格放在WPF中。 Simple enough in Powershell.. But when i run the code in Powershell 2.0, it can't find gives me errors. 在Powershell中足够简单。但是当我在Powershell 2.0中运行代码时,找不到它给了我错误。

Sample code: 样例代码:

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();

If you remove this line, everything work fine: 如果删除此行,则一切正常:

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

Anyone has any suggestions? 有人有建议吗?

Error i get in Powershell 2.0: 我在Powershell 2.0中遇到错误:

Exception calling "Load" with "1" argument(s): "The tag 'DataGrid' does not exist in XML namespace ' http://schemas.microsoft.com/winfx /2006/xaml/presentation'. Line '0' Position '0'." 带有“ 1”自变量的异常调用“加载”:“标记'DataGrid'在XML名称空间' http://schemas.microsoft.com/winfx / 2006 / xaml / presentation'中不存在。行'0'位置“ 0”。” At line:15 char:42 + $form = [Windows.Markup.XamlReader]::Load <<<< ($reader) + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException 在第15行:char:42 + $ form = [Windows.Markup.XamlReader] :: Load <<<<($ reader)+ CategoryInfo:NotSpecified:(:) [],MethodInvocationException + FullyQualifiedErrorId:DotNetMethodException

By default PowerShell 2.0 will use .NET framework 2.0. 默认情况下,PowerShell 2.0将使用.NET Framework 2.0。 There is no DataGrid in .NET v 2.0. .NET v 2.0中没有DataGrid。 Type : 类型:

[environment]::Version

If you get this, or something else starting with 2... 如果得到这个,或者其他以2开头的...

Major  Minor  Build  Revision
-----  -----  -----  --------
2      0      50727  7905

Your PowerShell session is using NET 2.0. 您的PowerShell会话正在使用NET 2.0。 How to "Enable .Net 4.0 access from PowerShell V2"? 如何“从PowerShell V2启用.Net 4.0访问”?

EDIT: 编辑:

To fix this on PS 2.0 .... 要在PS 2.0上修复此问题....

Create files C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe.config and C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell_ise.exe.config with the following content: 创建文件C:\\ Windows \\ System32 \\ WindowsPowerShell \\ v1.0 \\ powershell.exe.config和C:\\ Windows \\ System32 \\ WindowsPowerShell \\ v1.0 \\ powershell_ise.exe.config,其内容如下:

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

After this, you can load .NET 4 run-times: 之后,您可以加载.NET 4运行时:

Add-Type -Path "C:\Windows\Microsoft.NET\Framework\v4.0.30319\WPF\PresentationFramework.dll"

assuming you have .NET 4 loaded. 假设您已加载.NET 4。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM