[英]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.在 Visual Studio 中,我创建了一个新的 PowerShell 脚本项目来为脚本构建 GUI。 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.在 GUI 中,我想在文本框中输入文件名,然后在单击按钮时使用在文本框中输入的文件名创建文件。
I named the textbox and the button (see xml).我命名了文本框和按钮(参见 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.当我尝试为按钮添加单击事件处理程序时,我在 VS 中收到此消息:文档项没有代码隐藏文件。 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?) (或者,我看到用户从 VS 获取 xml 代码并直接在 PowerShell ISE/脚本中使用来开发 GUI。与使用 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.我不一定要寻找解决方案的代码,而是更多地了解为 PowerShell 脚本构建 GUI 的概念。 I would like to trigger PowerShell code from the buttons, so how would I create that bridge/link from button to code execution.我想从按钮触发 PowerShell 代码,那么我将如何创建从按钮到代码执行的桥/链接。 I have been scripting for years but never dabbled into the GUI world.我多年来一直在编写脚本,但从未涉足 GUI 世界。 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.我放弃了将 Visual Studio 用于 Powershell,而是将 Visual Studio 代码用于脚本或其他任何东西。 (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...至于 gui,我个人的建议是使用 c# 基本项目构建 gui,您只需打开它,将您预先存在的 xaml 放入其中,编辑它,复制粘贴它...
It also happens i just found out how to handle events easily, feel free to take a look Powershell Register WPF events它也发生了,我刚刚发现了如何轻松处理事件,随意看看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.还要注意,即使没有这个,像点击这样的事件通常也很容易处理,$button.addclick() 会传递一个脚本块或一个函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.