簡體   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