简体   繁体   中英

Visual Studio message: The document item has no code-behind file. (Building GUI for Powershell)

In Visual Studio, I created a new PowerShell Script Project to build a GUI for a script. Its something simple for now to moreso understand the concept of how this would work.

In the GUI I want to enter a filename in a textbox and then create a file when I click the button using the filename I entered in the textbox.

I named the textbox and the button (see xml). When I try to add a click event handler for the button I get this message in VS: The document item has no code-behind file. Add a code-behind file and a class definition before adding event handlers.

What am I missing here? (Alternatively, I've seen users grab the xml code from VS and use directly in PowerShell ISE/script to develop GUI. Would this be a better route vs using Visual Studio?)

I am not necessarily looking for the code to the solution, but more to understand the concept of building a GUI for PowerShell scripts. I would like to trigger PowerShell code from the buttons, so how would I create that bridge/link from button to code execution. I have been scripting for years but never dabbled into the GUI world. So this is new to me.

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

Edit: If anyone is looking to start this type of development, here is a sample:

Add-Type -Assembly PresentationFramework

[xml]$xmlFile = @"
<Window
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="MainWindow" Height="250" Width="525">
    <Grid>
        <Label x:Name="lbl_Filename" Content="Filename" HorizontalAlignment="Left" Margin="68,38,0,0" VerticalAlignment="Top" Width="69" Height="23"/>
        <Button x:Name="btn_CreateFile" Content="Create File" HorizontalAlignment="Left" Margin="294,64,0,0" VerticalAlignment="Top" Width="96"/>
        <TextBox x:Name="txt_Filename" HorizontalAlignment="Left" Margin="68,66,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="185"/>
    </Grid>
</Window>
"@


$xamlReader = (New-Object System.Xml.XmlNodeReader $xmlFile)
$xamlDialog = [Windows.Markup.XamlReader]::Load($xamlReader)


$btn_CreateFile = $xamlDialog.FindName("btn_CreateFile")
$txt_Filename   = $xamlDialog.FindName("txt_Filename")


$btn_CreateFile.Add_Click({
    $xamlDialog.Hide()
    $txtstr_Filename = $txt_Filename.Text.ToString()
    New-Item -Type File -Path "$($HOME)\$($txtstr_Filename)" | Out-Null
})



$xamlDialog.ShowDialog() | Out-Null

This was my precise case. I gave up using visual studio for powershell, rather use visual studio code for the script or anything else. (i love ultra edit but that's preference)

As for the gui my personal recommendation is to build gui using c# basic project, you just open it, slam your pre-existing xaml in it, edit it, copy paste it away...

It also happens i just found out how to handle events easily, feel free to take a look Powershell Register WPF events

Also note that some events like clicks are usually easy to handle even without this, $button.addclick() will do passing a scriptblock or a function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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