PowerShell Winform GUI 中的 WebView2 [英] WebView2 in PowerShell Winform GUI

查看:135
本文介绍了PowerShell Winform GUI 中的 WebView2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法找到有关此的任何信息,因此我不确定这是否完全可能.我目前正在尝试将 WebView2 控件集成到我的 PowerShell Winform GUI 中.我可以在调试脚本时看到创建了 webView2 控件,但没有加载任何内容.我安装了 WebView2 运行时.

I have not been able to find any information on this so I'm not sure its entirely possible. I'm currently trying to integrate the WebView2 control into my PowerShell Winform GUI. I can see while debugging my script that the webView2 control is created, but nothing ever loads. I have the WebView2 runtime installed.

这是我目前所拥有的:

function Show-test-WebView2Control_psf {

#----------------------------------------------
#region Import the Assemblies
#----------------------------------------------
[void][reflection.assembly]::LoadFile('C:\Tests\Assemblies\Debug\Microsoft.Web.WebView2.WinForms.dll')
[void][reflection.assembly]::LoadFile('C:\Tests\Assemblies\Microsoft.Web.WebView2.Core.dll')
[void][reflection.assembly]::LoadFile('C:\Tests\Assemblies\Microsoft.Web.WebView2.Wpf.dll')
[void][reflection.assembly]::Load('System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
[void][reflection.assembly]::Load('System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
#endregion Import Assemblies

#----------------------------------------------
#region Generated Form Objects
#----------------------------------------------
[System.Windows.Forms.Application]::EnableVisualStyles()
$form1 = New-Object 'System.Windows.Forms.Form'
$buttonRefresh = New-Object 'System.Windows.Forms.Button'
$buttonGo = New-Object 'System.Windows.Forms.Button'
$textbox1 = New-Object 'System.Windows.Forms.TextBox'
[Microsoft.Web.WebView2.WinForms.WebView2] $webview = New-Object 'Microsoft.Web.WebView2.WinForms.WebView2'
$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
#endregion Generated Form Objects

#----------------------------------------------
# User Generated Script
#----------------------------------------------

$form1_Load={
    #TODO: Initialize Form Controls here
    $webview.Source = ([uri]::new($textbox1.Text))
    $webview.Visible = $true
}

$webview_Ready = {
    $webview.NavigateToString("https://www.google.com")
}


$webview_CoreWebView2Ready={
    $webview.Source = [System.Uri]$textbox1.Text
}

$buttonGo_Click={
    #TODO: Place custom script here
    $webview.Source = [System.Uri] $textbox1.Text;
}

$webview_SourceChanged={
    $form1.Text = $webview.Source.AbsoluteUri;
}

# --End User Generated Script--
#----------------------------------------------
#region Generated Events
#----------------------------------------------

$Form_StateCorrection_Load=
{
    #Correct the initial state of the form to prevent the .Net maximized form issue
    $form1.WindowState = $InitialFormWindowState
}

$Form_Cleanup_FormClosed=
{
    #Remove all event handlers from the controls
    try
    {
        $buttonGo.remove_Click($buttonGo_Click)
        $webview.remove_CoreWebView2Ready($webview_CoreWebView2Ready)
        $webview.remove_SourceChanged($webview_SourceChanged)
        $form1.remove_Load($form1_Load)
        $form1.remove_Load($Form_StateCorrection_Load)
        $form1.remove_FormClosed($Form_Cleanup_FormClosed)
    }
    catch { Out-Null <# Prevent PSScriptAnalyzer warning #> }
}
#endregion Generated Events

#----------------------------------------------
#region Generated Form Code
#----------------------------------------------
$form1.SuspendLayout()
#
# form1
#
$form1.Controls.Add($buttonRefresh)
$form1.Controls.Add($buttonGo)
$form1.Controls.Add($textbox1)
$form1.Controls.Add($webview)
$form1.AutoScaleDimensions = New-Object System.Drawing.SizeF(6, 13)
$form1.AutoScaleMode = 'Font'
$form1.ClientSize = New-Object System.Drawing.Size(619, 413)
$form1.Name = 'form1'
$form1.Text = 'Form'
$form1.add_Load($form1_Load)
#
# buttonRefresh
#
$buttonRefresh.Location = New-Object System.Drawing.Point(13, 13)
$buttonRefresh.Name = 'buttonRefresh'
$buttonRefresh.Size = New-Object System.Drawing.Size(75, 23)
$buttonRefresh.TabIndex = 3
$buttonRefresh.Text = 'Refresh'
$buttonRefresh.UseVisualStyleBackColor = $True
#
# buttonGo
#
$buttonGo.Location = New-Object System.Drawing.Point(538, 9)
$buttonGo.Name = 'buttonGo'
$buttonGo.Size = New-Object System.Drawing.Size(75, 23)
$buttonGo.TabIndex = 2
$buttonGo.Text = 'Go'
$buttonGo.UseVisualStyleBackColor = $True
$buttonGo.add_Click($buttonGo_Click)
#
# textbox1
#
$textbox1.Location = New-Object System.Drawing.Point(96, 13)
$textbox1.Name = 'textbox1'
$textbox1.Size = New-Object System.Drawing.Size(435, 20)
$textbox1.TabIndex = 1
$textbox1.Text = 'https://www.bing.com'
#
# webview
#
$webview.Location = New-Object System.Drawing.Point(0, 49)
$webview.Name = 'webview'
$webview.Size = New-Object System.Drawing.Size(619, 364)
$webview.TabIndex = 0
$webview.ZoomFactor = 1
$webview.add_SourceChanged($webview_SourceChanged)

$form1.ResumeLayout()
#endregion Generated Form Code

#----------------------------------------------

#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($Form_StateCorrection_Load)
#Clean up the control events
$form1.add_FormClosed($Form_Cleanup_FormClosed)
#Show the Form
    return $form1.ShowDialog()

} #End Function

    #Call the form
    Show-test-WebView2Control_psf | Out-Null

有没有办法做到这一点?如果是这样,有人可以告诉我我做错了什么吗?

Is there a way to do this? If so, can someone please tell me what I'm doing wrong?

推荐答案

尝试自己的代码我必须解决两个问题:

Trying out your code on my own I had to resolve two issues:

  • 来自 webview2 nuget 包(作为参考,我使用了 WebView2 nuget 1.0.781-prerelease package) 我从 net45 文件夹中复制了 winforms 和核心 DLL,并将 x64 webview2loader.dll 全部复制到 wv2 文件夹中.您需要确保将相应 CPU 架构的 webview2loader.dll 复制到核心 dll 旁边.
  • 默认的 WebView2 用户数据文件夹位于包含主机可执行文件的文件夹中.在这种情况下,主机可执行文件是 powershell,并且包含的​​文件夹不能被正常进程写入.因此,您需要更改用户数据文件夹,您可以通过 CoreWebView2CreationProperties 初始化 WebView2 之前的 UserDataFolder 属性.
  • From the webview2 nuget package (for reference I used the WebView2 nuget 1.0.781-prerelease package) I copied the winforms and core DLL from the net45 folder and I copied the x64 webview2loader.dll all into the wv2 folder. You need to ensure the webview2loader.dll of the appropriate CPU architecture is copied next to the core dll.
  • The default WebView2 user data folder is in the folder containing the host executable. In this case the host executable is powershell and the containing folder isn't writable by normal processes. So you need to change the user data folder which you can do via the CoreWebView2CreationProperties UserDataFolder property before the WebView2 is initialized.

我有下面的修改代码.需要额外的工作来调整和关闭 WebView2 并解决其他问题.

I have the modified code below. Additional work is required to resize and close the WebView2 and resolve other issues.

function Show-test-WebView2Control_psf {

#----------------------------------------------
#region Import the Assemblies
#----------------------------------------------
# I've put the following files in `C:\users\davris\tmp\wv2`
#     Microsoft.Web.WebView2.Core.dll
#     Microsoft.Web.WebView2.WinForms.dll
#     WebView2Loader.dll
[void][reflection.assembly]::LoadFile('C:\users\davris\tmp\wv2\Microsoft.Web.WebView2.WinForms.dll')
[void][reflection.assembly]::LoadFile('C:\users\davris\tmp\wv2\Microsoft.Web.WebView2.Core.dll')
[void][reflection.assembly]::Load('System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
[void][reflection.assembly]::Load('System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
#endregion Import Assemblies

#----------------------------------------------
#region Generated Form Objects
#----------------------------------------------
[System.Windows.Forms.Application]::EnableVisualStyles()
$form1 = New-Object 'System.Windows.Forms.Form'
$buttonRefresh = New-Object 'System.Windows.Forms.Button'
$buttonGo = New-Object 'System.Windows.Forms.Button'
$textbox1 = New-Object 'System.Windows.Forms.TextBox'
[Microsoft.Web.WebView2.WinForms.WebView2] $webview = New-Object 'Microsoft.Web.WebView2.WinForms.WebView2'
$webview.CreationProperties = New-Object 'Microsoft.Web.WebView2.WinForms.CoreWebView2CreationProperties'
$webview.CreationProperties.UserDataFolder = "C:\users\davris\tmp\wv2\data";
$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
#endregion Generated Form Objects

#----------------------------------------------
# User Generated Script
#----------------------------------------------

$form1_Load={
    #TODO: Initialize Form Controls here
    $webview.Source = ([uri]::new($textbox1.Text))
    $webview.Visible = $true
}

$buttonGo_Click={
    #TODO: Place custom script here
    $webview.Source = [System.Uri] $textbox1.Text;
}

$webview_SourceChanged={
    $form1.Text = $webview.Source.AbsoluteUri;
}

# --End User Generated Script--
#----------------------------------------------
#region Generated Events
#----------------------------------------------

$Form_StateCorrection_Load=
{
    #Correct the initial state of the form to prevent the .Net maximized form issue
    $form1.WindowState = $InitialFormWindowState
}

$Form_Cleanup_FormClosed=
{
    #Remove all event handlers from the controls
    try
    {
        $buttonGo.remove_Click($buttonGo_Click)
        $webview.remove_SourceChanged($webview_SourceChanged)
        $form1.remove_Load($form1_Load)
        $form1.remove_Load($Form_StateCorrection_Load)
        $form1.remove_FormClosed($Form_Cleanup_FormClosed)
    }
    catch { Out-Null <# Prevent PSScriptAnalyzer warning #> }
}
#endregion Generated Events

#----------------------------------------------
#region Generated Form Code
#----------------------------------------------
$form1.SuspendLayout()
#
# form1
#
$form1.Controls.Add($buttonRefresh)
$form1.Controls.Add($buttonGo)
$form1.Controls.Add($textbox1)
$form1.Controls.Add($webview)
$form1.AutoScaleDimensions = New-Object System.Drawing.SizeF(6, 13)
$form1.AutoScaleMode = 'Font'
$form1.ClientSize = New-Object System.Drawing.Size(619, 413)
$form1.Name = 'form1'
$form1.Text = 'Form'
$form1.add_Load($form1_Load)
#
# buttonRefresh
#
$buttonRefresh.Location = New-Object System.Drawing.Point(13, 13)
$buttonRefresh.Name = 'buttonRefresh'
$buttonRefresh.Size = New-Object System.Drawing.Size(75, 23)
$buttonRefresh.TabIndex = 3
$buttonRefresh.Text = 'Refresh'
$buttonRefresh.UseVisualStyleBackColor = $True
#
# buttonGo
#
$buttonGo.Location = New-Object System.Drawing.Point(538, 9)
$buttonGo.Name = 'buttonGo'
$buttonGo.Size = New-Object System.Drawing.Size(75, 23)
$buttonGo.TabIndex = 2
$buttonGo.Text = 'Go'
$buttonGo.UseVisualStyleBackColor = $True
$buttonGo.add_Click($buttonGo_Click)
#
# textbox1
#
$textbox1.Location = New-Object System.Drawing.Point(96, 13)
$textbox1.Name = 'textbox1'
$textbox1.Size = New-Object System.Drawing.Size(435, 20)
$textbox1.TabIndex = 1
$textbox1.Text = 'https://www.bing.com'
#
# webview
#
$webview.Location = New-Object System.Drawing.Point(0, 49)
$webview.Name = 'webview'
$webview.Size = New-Object System.Drawing.Size(619, 364)
$webview.TabIndex = 0
$webview.ZoomFactor = 1
$webview.add_SourceChanged($webview_SourceChanged)

$form1.ResumeLayout()
#endregion Generated Form Code

#----------------------------------------------

#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($Form_StateCorrection_Load)
#Clean up the control events
$form1.add_FormClosed($Form_Cleanup_FormClosed)
#Show the Form
    return $form1.ShowDialog()

} #End Function

    #Call the form
    Show-test-WebView2Control_psf | Out-Null

这篇关于PowerShell Winform GUI 中的 WebView2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆