回答編集履歴
1
見直しキャンペーン中
test
CHANGED
@@ -1,157 +1,79 @@
|
|
1
1
|
ちょうど微妙な時期なのですが、`WebView2`(いわゆる Chromium Edge)がそろそろ正式リリースされるはずです(まだプレビュー)
|
2
|
-
|
3
|
-
既に`WebView`には`ObsoleteAttribute
|
2
|
+
既に`WebView`には`ObsoleteAttribute`が付いており、あまり使わせたくないのかもしれません^^;
|
4
|
-
|
5
3
|
`WebView`・`WebView2`は互換性もなく非常に悩ましいです。
|
6
4
|
|
7
5
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
6
|
`WebView2`を試す場合は下記の上で、`Microsoft.Web.WebView2`を`0.9.538-prerelease`にダウングレードしてください(理由は不明だがそうしないと手順通りにできない)
|
12
|
-
|
13
7
|
[Microsoft Edge WebView 2 Windows フォームアプリ - Microsoft Edge Development | Microsoft Docs](https://docs.microsoft.com/ja-jp/microsoft-edge/webview2/gettingstarted/winforms)
|
14
8
|
|
15
|
-
|
16
|
-
|
17
9
|
使用例
|
18
|
-
|
19
|
-
```
|
10
|
+
```cs
|
20
|
-
|
21
11
|
using System;
|
22
|
-
|
23
12
|
using System.Diagnostics;
|
24
|
-
|
25
13
|
using System.Windows.Forms;
|
26
|
-
|
27
14
|
using Microsoft.Toolkit.Forms.UI.Controls;
|
28
|
-
|
29
15
|
using Microsoft.Toolkit.Win32.UI.Controls.Interop.WinRT;
|
30
|
-
|
31
16
|
using Microsoft.Web.WebView2.Core;
|
32
|
-
|
33
17
|
using Microsoft.Web.WebView2.WinForms;
|
34
18
|
|
35
|
-
|
36
|
-
|
37
19
|
namespace Questions274706
|
38
|
-
|
39
20
|
{
|
40
|
-
|
41
21
|
public partial class Form1 : Form
|
42
|
-
|
43
22
|
{
|
44
|
-
|
45
23
|
private readonly WebView webView; // Warning CS0618 Obsolete付けるのはまだ早い気がするが^^;
|
46
|
-
|
47
24
|
private readonly WebView2 webView2;
|
48
25
|
|
49
|
-
|
50
|
-
|
51
26
|
public Form1()
|
52
|
-
|
53
27
|
{
|
54
|
-
|
55
28
|
InitializeComponent();
|
56
29
|
|
57
|
-
|
58
|
-
|
59
30
|
var panel = new TableLayoutPanel
|
60
|
-
|
61
31
|
{
|
62
|
-
|
63
32
|
ColumnCount = 2,
|
64
|
-
|
65
33
|
RowCount = 1,
|
66
|
-
|
67
34
|
Dock = DockStyle.Fill,
|
68
|
-
|
69
35
|
};
|
70
|
-
|
71
36
|
panel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F));
|
72
|
-
|
73
37
|
panel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F));
|
74
|
-
|
75
38
|
Controls.Add(panel);
|
76
39
|
|
77
|
-
|
78
|
-
|
79
40
|
webView = new WebView
|
80
|
-
|
81
41
|
{
|
82
|
-
|
83
42
|
Source = new Uri("https://teratail.com/questions/274706"),
|
84
|
-
|
85
43
|
Dock = DockStyle.Fill,
|
86
|
-
|
87
44
|
};
|
88
|
-
|
89
45
|
webView.NavigationCompleted += WebView_NavigationCompleted;
|
90
|
-
|
91
46
|
panel.Controls.Add(webView, 0, 0);
|
92
47
|
|
93
48
|
|
94
|
-
|
95
|
-
|
96
|
-
|
97
49
|
webView2 = new WebView2
|
98
|
-
|
99
50
|
{
|
100
|
-
|
101
51
|
Source = new Uri("https://teratail.com/questions/274706"),
|
102
|
-
|
103
52
|
Dock = DockStyle.Fill,
|
104
|
-
|
105
53
|
};
|
106
|
-
|
107
54
|
webView2.NavigationCompleted += WebView2_NavigationCompleted;
|
108
|
-
|
109
55
|
panel.Controls.Add(webView2, 1, 0);
|
110
|
-
|
111
56
|
}
|
112
57
|
|
113
|
-
|
114
|
-
|
115
58
|
private void WebView_NavigationCompleted(object sender, WebViewControlNavigationCompletedEventArgs e)
|
116
|
-
|
117
59
|
{
|
118
|
-
|
119
60
|
// InvokeScriptAsyncもある
|
120
|
-
|
121
61
|
var result = webView.InvokeScript("eval", new string[] { "document.getElementById('l-mainContents').innerText" });
|
122
|
-
|
123
62
|
Debug.WriteLine(result);
|
124
63
|
|
64
|
+
var t = "定形郵便";
|
65
|
+
webView.InvokeScript("eval", new string[] { $"document.getElementById('boxSearch-query').setAttribute('value','{t}');" });
|
66
|
+
}
|
125
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);
|
126
73
|
|
127
74
|
var t = "定形郵便";
|
128
|
-
|
129
|
-
webView.
|
75
|
+
await webView2.ExecuteScriptAsync($"document.getElementById('boxSearch-query').setAttribute('value','{t}');");
|
130
|
-
|
131
76
|
}
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
private async void WebView2_NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e)
|
136
|
-
|
137
|
-
{
|
138
|
-
|
139
|
-
// ExecuteScriptはない
|
140
|
-
|
141
|
-
var result = await webView2.ExecuteScriptAsync("document.getElementById('l-mainContents').innerText");
|
142
|
-
|
143
|
-
Debug.WriteLine(result);
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
var t = "定形郵便";
|
148
|
-
|
149
|
-
await webView2.ExecuteScriptAsync($"document.getElementById('boxSearch-query').setAttribute('value','{t}');");
|
150
|
-
|
151
|
-
}
|
152
|
-
|
153
77
|
}
|
154
|
-
|
155
78
|
}
|
156
|
-
|
157
79
|
```
|