2

当我不使用 .NET 4.0 框架时,以下代码有效

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}

按下显示按钮,网格将填充 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 会导致问题,但我不确定。

4

1 回答 1

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。您可能还有一些需要修复。:)

于 2011-05-01T15:18:35.663 回答