Visual Studio消息:文档项目没有代码范围的文件。 (为PowerShell建造GUI)
在Visual Studio中,我创建了一个新的PowerShell脚本项目,以构建脚本的GUI。现在,Moreso了解它将如何工作的概念。
在GUI中,我想在文本框中输入文件名,然后在使用文本框中输入的文件名单击按钮时创建文件。
我命名了文本框和按钮(请参阅XML)。当我尝试为按钮添加单击事件处理程序时,我会在vs中获取此消息:文档项目没有代码范围的文件。在添加事件处理程序之前,添加代码范围文件和类定义。
我在这里缺少什么? (另外,我已经看到用户从VS中获取XML代码,并直接在PowerShell ISE/脚本中使用来开发GUI。这是否是使用Visual Studio的更好的路由VS?)
我不一定要寻找解决方案的代码,但更多是了解为PowerShell脚本构建GUI的概念。我想从按钮触发PowerShell代码,因此如何创建从按钮到代码执行的桥梁/链接。我已经写了多年了,但从未涉足GUI世界。所以这对我来说是新的。
/I.SSTATIC.NET/XTSII.png“ alt =”在此处输入图像描述”>
编辑: 如果有人希望启动这种类型的开发,以下是一个示例:
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我的确切案例。
我放弃了使用Visual Studio进行PowerShell,而是将Visual Studio代码用于脚本或其他任何内容。 (我喜欢Ultra Edit,但这是偏爱)
至于GUI,我的个人建议是使用C#基本项目构建GUI,您只需打开它,猛击您先前存在的XAML,编辑它,将其复制粘贴掉
...我也发生了,我刚刚发现了如何轻松处理事件,可以随意看看
PowerShell寄存器wpf empers wpf everts
也要注意一些诸如点击之类的事件 即使没有此功能,$ button.addclick()也会通过scriptblock或函数。
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.