与.NET4一起使用PowerShell时出现WPK问题

时间:2011-05-01 15:12:18

标签: powershell .net-4.0 wpk

当我不使用.NET 4.0 Framwork

时,以下代码可以正常工作
ipmo WPK


$ConnectionString = $ConnectionString = "Server=localhost;Integrated Security=True"

$conn = new-object System.Data.SQLClient.SQLConnection
$conn.ConnectionString = $ConnectionString 
$conn.Open() 


function Invoke-sql1
{
    param( [string]$sql,
           [System.Data.SQLClient.SQLConnection]$connection
           )
    $cmd = new-object System.Data.SQLClient.SQLCommand($sql,$connection)
    $ds = New-Object system.Data.DataSet
    $da = New-Object System.Data.SQLClient.SQLDataAdapter($cmd)
    $da.fill($ds) | Out-Null
    return $ds.tables[0]
}


function Show-Bockmarks ($resource) {
    New-Window   {
    New-Grid -ColumnDefinitions @(
            New-ColumnDefinition -Width 900*
        ) -RowDefinitions @(
            New-RowDefinition -Height 30
            New-RowDefinition -Height 600*
        )     {

        New-StackPanel -Orientation horizontal -column 0 -row 0 -Children {
            New-Button -Name Search "Press me" -On_Click {
            $ff_sql = @"
SELECT 'abc' title, getdate() dateAdded , 'xyz' url
UNION
SELECT 'efg' title, getdate() dateAdded , 'xyz' url
"@
            $conn = $resource.conn
            $window.Title = "$($conn.database) Database Browser"
            $TableView = $window | Get-ChildControl TableView
            $TableView.ItemsSource = Invoke-sql1 -sql $ff_sql -connection $conn
             } 
             New-Button -Name Cancel "Close" -On_Click {$window.Close()} 
        }
        New-ListView -Column 0 -Row 1 -Name TableView -View {
           New-GridView -AllowsColumnReorder -Columns {
               New-GridViewColumn "title" 
               New-GridViewColumn "dateAdded" 
               New-GridViewColumn "url" 
           }
        }
    }
    } -asjob -Resource $resource
}

Show-Bockmarks -resource @{conn = $conn}

按下show me按钮,网格填充3行。

但是当我通过添加powershell_ise.exe.Config来使用.Net 4.0框架时

<?xml version="1.0"?>
<configuration>
    <startup useLegacyV2RuntimeActivationPolicy="true">
        <supportedRuntime version="v4.0.30319" />
        <supportedRuntime version="v2.0.50727" />
    </startup>
</configuration>

我收到以下错误:

Add-Type : c:\Users\berndk.MMEDVNT\AppData\Local\Temp\eps22jnq.0.cs(24) : The type 'System.Windows.Markup.IQueryAmbient' is defined in an assembly that is not referenced. You must add a
 reference to assembly 'System.Xaml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

我想,在某些情况下使用带-asJob的WPK会导致问题,但我不确定。

1 个答案:

答案 0 :(得分:1)

打开Start-WPFJob.ps1文件,然后打开第48行:

    Add-Type -IgnoreWarnings -ReferencedAssemblies "WindowsBase",
    "PresentationCore", 
    "PresentationFramework" @"

替换为:

    Add-Type -IgnoreWarnings -ReferencedAssemblies "WindowsBase",
    "PresentationCore", 
    "PresentationFramework",
    "System.Xaml" @"

WPK仅适用于.NET 3.5。您可能需要修复一些问题。 :)