繁体   English   中英

无法从 Visual Studio 代码打开 Powershell 对话框

[英]Cant open Powershell dialog box from Visual Studio code

运行以下代码以使用 Powershell ISE 中的“打开文件对话框”工作正常。 但是,在 Visual Studio Code 中运行相同的代码不会打开对话框......没有错误左右,它似乎正在运行......

有没有人有解决方案或遇到过同样的问题?

对话框的代码:

function Get-Files{
    $openFileDialog = New-Object windows.forms.openfiledialog
    $openFileDialog.Multiselect = $true
    $openFileDialog.filter = 'All files (*.*)| *.*'   
    $openFileDialog.filter = 'DataSource Or Report Files|*.rdl|All Files|*.*'     
    $openFileDialog.initialDirectory = [System.IO.Directory]::SetCurrentDirectory('c:\temp\') #'
    $openFileDialog.initialDirectory = [System.IO.Directory]::GetCurrentDirectory()
    $openFileDialog.title = 'Select Development *.RDL File to Copy:' 
    $openFileDialog.ShowHelp = $true
    Write-Host 'Select Datasource or Report File... (see FileOpen Dialog):' -ForegroundColor Green  
    if('Ok' -eq $openFileDialog.ShowDialog()){
        $openFileDialog.FileNames
    }
    
}

if($files = Get-Files){
    $fbd = [System.Windows.Forms.FolderBrowserDialog]::new()
    $fbd.ShowDialog()
    $files | %{$_.CopyTo($fbd.SelectedPath)}
}

在 ISE 中执行此操作时,它对自动加载所需内容非常有帮助。

在控制台主机和 VSCode 中,需要提供在 ISE 实例中自动加载的所有内容。 好吧,至少我不得不这样做。

那么,您确定代码正在运行吗?

当从编辑器运行代码时,我经常让 VSCode 在通过 F8(选择)或 F5(全部)等使用运行代码时变得非常古怪。 加载时间很长,代码大纲无缘无故挂起(导致速度变慢并且代码在完成之前不会运行)等。

修复是禁用除 PowerShell 扩展之外的所有扩展,或从集成终端保存并作为脚本运行,或将代码块复制并粘贴到集成终端中,或在编辑器中选择代码,然后按 F1 并键入“运行选择”活动终端中的文本'( 您可以为此 进行键绑定,我使用 shift+r)...

...让它运行。 即使是这种时髦也会发生,这意味着有时需要一段时间才能开始。

#keybindings.json

{
    "key": "shift+r",
    "command": "workbench.action.terminal.runSelectedText"
}

另外,VSCode 真的不喜欢别名。 它会将它们标记为代码中的错误,直到您修复它们。 它甚至可以为您修复它们,并有一个扩展所有别名选项,甚至 ISE 也有一个插件可以做到这一点。 别名适用于交互式内容,但绝对不应该在脚本中使用。 即使按照微软。

• 别名的最佳实践在 PowerShell 脚本中使用别名的最佳实践https://devblogs.microsoft.com/scripting/best-practice-for-using-aliases-in-powershell-scripts https://devblogs.microsoft.com/脚本/使用-powershell-aliases-best-practices

 Why worry about aliases in the first place? What is the big deal about using aliases anyway? If they make the code easier to type, what is the harm in using them in scripts? There are two things at work when it comes to a script. The first is that no alias is guaranteed to exist—even aliases that are created by Windows PowerShell.

最后,需要记住在调用需要焦点的 UX/UI 时强制置顶。

例如:

# Load UX/UI resources
Add-Type -AssemblyName  System.Drawing,
                        PresentationCore,
                        PresentationFramework,
                        System.Windows.Forms,
                        microsoft.VisualBasic
[System.Windows.Forms.Application]::EnableVisualStyles()
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::
SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12

$FolderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog
$FolderBrowser.Description = 'Select the folder containing the data'

# Set focus
$result = $FolderBrowser.ShowDialog((New-Object System.Windows.Forms.Form -Property @{TopMost = $true }))

if ($result -eq [Windows.Forms.DialogResult]::OK){
    $FolderBrowser.SelectedPath
}
else {
    # Other code here
}

似乎有效,它在后台打开,因此无法识别。 此外,此类 Windows 窗体不会出现在任务栏中,因此,您需要最小化/关闭所有正在运行的应用程序才能看到该窗体。 使用多个屏幕时要注意...

暂无
暂无

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

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