简体   繁体   English

用 powershell 显示一点 wpf 表格

[英]display a little wpf form with powershell

I try to display a little XAML form which was created on visual studio 2019(application WPF .NET framework project).But it doesn't work I have the 2 errors:我尝试显示在 Visual Studio 2019 上创建的一点 XAML 表单(应用程序 WPF .NET 框架项目)。但它不起作用我有 2 个错误:

Line |
  25 |  $MainForm = [Windows.Markup.XamlReader]::Load($Reader)
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Exception calling "Load" with "1" argument(s): "Cannot set unknown member  '{http://schemas.openxmlformats.org/markup-compatibility/2006}Ignorable'."


Line |
  30 |  $MainForm.ShowDialog() | Out-Null
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | You cannot call a method on a null-valued expression.

Do you have a solution please?请问你有解决办法吗? Here is my code on my powershell file.ps1, I have added the XAML code in my powershell file:这是我在 powershell file.ps1 上的代码,我在 powershell 文件中添加了 XAML 代码:

Add-Type -AssemblyName PresentationFramework
Add-Type -AssemblyName System.Windows.Forms

[XML]$XAML =
@'
<Window x:Class="WpfApp5.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp5"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid/>
</Window>
'@


$XAML.Window.RemoveAttribute("x:Class")
$Reader = New-Object System.Xml.XmlNodeReader $XAML
$MainForm = [Windows.Markup.XamlReader]::Load($Reader)
$MainForm.ShowDialog() | Out-Null

I delete manually this line, and it works now:我手动删除了这一行,它现在可以工作了:

mc:Ignorable="d"

In somes sites, they have this code to delete automaticallay the xaml code.在某些站点中,他们有此代码可以自动删除 xaml 代码。 here is the code:这是代码:

$inputXAMLClean = $inputXAML -replace 'mc:Ignorable="d"','' -replace "x:N",'N' -replace 'x:Class=".*?"','' -replace 'd:DesignHeight="\d*?"','' -replace 'd:DesignWidth="\d*?"','



But when I try to add the code:
$inputXAMLClean = $inputXAML -replace 'mc:Ignorable="d"',''

 I have the error:

Line |
  31 |  $Reader = New-Object System.Xml.XmlNodeReader $XAML1
     |            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Cannot convert argument "0", with value: "System.Xml.XmlDocument", for "XmlNodeReader" to type "System.Xml.XmlNode": "Cannot convert the "System.Xml.XmlDocument" value
     | of type "System.String" to type "System.Xml.XmlNode"."



Here is the good code,it works:


Add-Type -AssemblyName PresentationFramework
Add-Type -AssemblyName System.Windows.Forms

[XML]$XAML =
@'
<Window x:Class="WpfApp5.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp5"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Label Content="Label" HorizontalAlignment="Left" Margin="280,179,0,0" VerticalAlignment="Top"/>
        <Button Content="Button" HorizontalAlignment="Left" Margin="457,136,0,0" VerticalAlignment="Top" Width="75"/>
    </Grid>
</Window>

'@


$XAML.Window.RemoveAttribute("x:Class")
$XAML.Window.RemoveAttribute("mc:Ignorable")
$Reader = New-Object System.Xml.XmlNodeReader $XAML
$MainForm = [Windows.Markup.XamlReader]::Load($Reader)
$MainForm.ShowDialog() | Out-Null

I'd suggest some error checking as follows:我建议进行一些错误检查,如下所示:

#Load required libraries
$TermMsg = {
  [Windows.Forms.MessageBox]::Show($Message,
                                   "Program Terminated:",
  [Windows.Forms.MessageBoxButtons]::OK ,
  [Windows.Forms.MessageBoxIcon]::Information) | Out-Null}

Add-Type -AssemblyName "System.Windows.Forms"
Add-Type -AssemblyName "System.io"

If ($host.Name -eq 'ConsoleHost' -or
    $host.Name -eq 'Visual Studio Code Host') {

  try{  <#+------------------------------------------------+
          | Check that the proper assemblies are loaded    |
          | Required for PS Console and Visual Code, while |
          | only Systems.Windows.Forms needed for PSISE!   |
          +------------------------------------------------+
        #>
    $ATArgs = @{AssemblyName = "PresentationCore",
                               "PresentationFramework",
                               "WindowsBase"
                ErrorAction = 'Stop'}
    Add-Type @ATArgs
  }
  catch {
    $Message =
        "Failed to load Windows Presentation Framework" +
        " and/or other assemblies required for this program!"
     & $TermMsg
    Exit
  }

} #End If ($host...

[xml]$xaml = @"
<Window
  xmlns =
    "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x  = "http://schemas.microsoft.com/winfx/2006/xaml"
    Name          = "Window"
    Title         = "Test Form"
    WindowStartupLocation = "CenterScreen"
    Width         = "450"
    Height        = "800"
    FontSize      = "18"
    Background    = "Blue" 
    Foreground    = "White"
    ShowInTaskbar = "True">

    <Grid>
         <TextBox   x:Name="tboxVerInfo"
                    Width="400"
                    Height="30"
                    HorizontalAlignment="Left"
                    VerticalAlignment  ="Top"
                    Margin = "10,10,5,0" />
    </Grid>
</Window>
"@

$NOArgs = @{TypeName     = 'System.Xml.XmlNodeReader'
            ArgumentList = $xaml}
$reader=(New-Object @NOArgs)

Try  {$MainForm=[Windows.Markup.XamlReader]::Load( $reader )
      $MainForm.ShowDialog() | Out-Null}

Catch{Write-Host $("Unable to load Windows.Markup.XamlReader." +
                 "`nDouble-check syntax and ensure " +
                 ".net is installed.")
     }

Note: this makes sure the proper assemblies are loaded for the environment you are using, ie ISE,PS Console, VSCode.注意:这确保为您正在使用的环境(即 ISE、PS 控制台、VSCode)加载了正确的程序集。 HTH高温高压

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

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