teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

見直しキャンペーン中

2023/07/22 08:21

投稿

TN8001
TN8001

スコア10111

answer CHANGED
@@ -1,79 +1,79 @@
1
- ちょうど微妙な時期なのですが、`WebView2`(いわゆる Chromium Edge)がそろそろ正式リリースされるはずです(まだプレビュー)
2
- 既に`WebView`には`ObsoleteAttribute `が付いており、あまり使わせたくないのかもしれません^^;
3
- `WebView`・`WebView2`は互換性もなく非常に悩ましいです。
4
-
5
-
6
- `WebView2`を試す場合は下記の上で、`Microsoft.Web.WebView2`を`0.9.538-prerelease`にダウングレードしてください(理由は不明だがそうしないと手順通りにできない)
7
- [Microsoft Edge WebView 2 Windows フォームアプリ - Microsoft Edge Development | Microsoft Docs](https://docs.microsoft.com/ja-jp/microsoft-edge/webview2/gettingstarted/winforms)
8
-
9
- 使用例
10
- ```C#
11
- using System;
12
- using System.Diagnostics;
13
- using System.Windows.Forms;
14
- using Microsoft.Toolkit.Forms.UI.Controls;
15
- using Microsoft.Toolkit.Win32.UI.Controls.Interop.WinRT;
16
- using Microsoft.Web.WebView2.Core;
17
- using Microsoft.Web.WebView2.WinForms;
18
-
19
- namespace Questions274706
20
- {
21
- public partial class Form1 : Form
22
- {
23
- private readonly WebView webView; // Warning CS0618 Obsolete付けるのはまだ早い気がするが^^;
24
- private readonly WebView2 webView2;
25
-
26
- public Form1()
27
- {
28
- InitializeComponent();
29
-
30
- var panel = new TableLayoutPanel
31
- {
32
- ColumnCount = 2,
33
- RowCount = 1,
34
- Dock = DockStyle.Fill,
35
- };
36
- panel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F));
37
- panel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F));
38
- Controls.Add(panel);
39
-
40
- webView = new WebView
41
- {
42
- Source = new Uri("https://teratail.com/questions/274706"),
43
- Dock = DockStyle.Fill,
44
- };
45
- webView.NavigationCompleted += WebView_NavigationCompleted;
46
- panel.Controls.Add(webView, 0, 0);
47
-
48
-
49
- webView2 = new WebView2
50
- {
51
- Source = new Uri("https://teratail.com/questions/274706"),
52
- Dock = DockStyle.Fill,
53
- };
54
- webView2.NavigationCompleted += WebView2_NavigationCompleted;
55
- panel.Controls.Add(webView2, 1, 0);
56
- }
57
-
58
- private void WebView_NavigationCompleted(object sender, WebViewControlNavigationCompletedEventArgs e)
59
- {
60
- // InvokeScriptAsyncもある
61
- var result = webView.InvokeScript("eval", new string[] { "document.getElementById('l-mainContents').innerText" });
62
- Debug.WriteLine(result);
63
-
64
- var t = "定形郵便";
65
- webView.InvokeScript("eval", new string[] { $"document.getElementById('boxSearch-query').setAttribute('value','{t}');" });
66
- }
67
-
68
- private async void WebView2_NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e)
69
- {
70
- // ExecuteScriptはない
71
- var result = await webView2.ExecuteScriptAsync("document.getElementById('l-mainContents').innerText");
72
- Debug.WriteLine(result);
73
-
74
- var t = "定形郵便";
75
- await webView2.ExecuteScriptAsync($"document.getElementById('boxSearch-query').setAttribute('value','{t}');");
76
- }
77
- }
78
- }
1
+ ちょうど微妙な時期なのですが、`WebView2`(いわゆる Chromium Edge)がそろそろ正式リリースされるはずです(まだプレビュー)
2
+ 既に`WebView`には`ObsoleteAttribute`が付いており、あまり使わせたくないのかもしれません^^;
3
+ `WebView`・`WebView2`は互換性もなく非常に悩ましいです。
4
+
5
+
6
+ `WebView2`を試す場合は下記の上で、`Microsoft.Web.WebView2`を`0.9.538-prerelease`にダウングレードしてください(理由は不明だがそうしないと手順通りにできない)
7
+ [Microsoft Edge WebView 2 Windows フォームアプリ - Microsoft Edge Development | Microsoft Docs](https://docs.microsoft.com/ja-jp/microsoft-edge/webview2/gettingstarted/winforms)
8
+
9
+ 使用例
10
+ ```cs
11
+ using System;
12
+ using System.Diagnostics;
13
+ using System.Windows.Forms;
14
+ using Microsoft.Toolkit.Forms.UI.Controls;
15
+ using Microsoft.Toolkit.Win32.UI.Controls.Interop.WinRT;
16
+ using Microsoft.Web.WebView2.Core;
17
+ using Microsoft.Web.WebView2.WinForms;
18
+
19
+ namespace Questions274706
20
+ {
21
+ public partial class Form1 : Form
22
+ {
23
+ private readonly WebView webView; // Warning CS0618 Obsolete付けるのはまだ早い気がするが^^;
24
+ private readonly WebView2 webView2;
25
+
26
+ public Form1()
27
+ {
28
+ InitializeComponent();
29
+
30
+ var panel = new TableLayoutPanel
31
+ {
32
+ ColumnCount = 2,
33
+ RowCount = 1,
34
+ Dock = DockStyle.Fill,
35
+ };
36
+ panel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F));
37
+ panel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F));
38
+ Controls.Add(panel);
39
+
40
+ webView = new WebView
41
+ {
42
+ Source = new Uri("https://teratail.com/questions/274706"),
43
+ Dock = DockStyle.Fill,
44
+ };
45
+ webView.NavigationCompleted += WebView_NavigationCompleted;
46
+ panel.Controls.Add(webView, 0, 0);
47
+
48
+
49
+ webView2 = new WebView2
50
+ {
51
+ Source = new Uri("https://teratail.com/questions/274706"),
52
+ Dock = DockStyle.Fill,
53
+ };
54
+ webView2.NavigationCompleted += WebView2_NavigationCompleted;
55
+ panel.Controls.Add(webView2, 1, 0);
56
+ }
57
+
58
+ private void WebView_NavigationCompleted(object sender, WebViewControlNavigationCompletedEventArgs e)
59
+ {
60
+ // InvokeScriptAsyncもある
61
+ var result = webView.InvokeScript("eval", new string[] { "document.getElementById('l-mainContents').innerText" });
62
+ Debug.WriteLine(result);
63
+
64
+ var t = "定形郵便";
65
+ webView.InvokeScript("eval", new string[] { $"document.getElementById('boxSearch-query').setAttribute('value','{t}');" });
66
+ }
67
+
68
+ private async void WebView2_NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e)
69
+ {
70
+ // ExecuteScriptはない
71
+ var result = await webView2.ExecuteScriptAsync("document.getElementById('l-mainContents').innerText");
72
+ Debug.WriteLine(result);
73
+
74
+ var t = "定形郵便";
75
+ await webView2.ExecuteScriptAsync($"document.getElementById('boxSearch-query').setAttribute('value','{t}');");
76
+ }
77
+ }
78
+ }
79
79
  ```