开发者

Problem with WPK when using PowerShell with .NET4

The following code works, when I do not use .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开发者_JAVA百科 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}

Press the show me button and the grid fills with 3 rows.

But when I use the .Net 4.0 framework by adding powershell_ise.exe.Config as such

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

I get the following error:

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'.

I suppose, that in some cases using WPK with -asJob causes the problem, but I'm not sure.


Open the Start-WPFJob.ps1 file, and on line 48:

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

replace with:

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

WPK was only designed for .NET 3.5. You might have a few more to fix. :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜