@@ -41,28 +41,21 @@ bool AppWindow::ConfigureAndExecuteFind(
4141}
4242```
4343```csharp
44- bool AppWindow::ConfigureAndExecuteFind (
45- const std::wstring& searchTerm,
44+ public async Task< bool> ConfigureAndExecuteFindAsync (
45+ string searchTerm,
4646 bool caseSensitive,
4747 bool highlightAllMatches,
48- COREWEBVIEW2_FIND_DIRECTION direction)
48+ CoreWebView2FindDirection direction)
4949{
50- // Query for the ICoreWebView2StagingEnvironment5 interface.
51- auto webView2Environment5 = m_webViewEnvironment.try_query<ICoreWebView2StagingEnvironment5>();
52- CHECK_FEATURE_RETURN(webView2Environment5);
53-
54- // Create the find configuration.
55- wil::com_ptr<ICoreWebView2StagingFindConfiguration> findConfiguration;
56- CHECK_FAILURE(webView2Environment5->CreateFindConfiguration(&findConfiguration));
57-
58- // Apply the find operation configurations.
59- CHECK_FAILURE(findConfiguration->put_FindTerm(searchTerm.c_str()));
60- CHECK_FAILURE(findConfiguration->put_IsCaseSensitive(caseSensitive));
61- CHECK_FAILURE(findConfiguration->put_ShouldHighlightAllMatches(highlightAllMatches));
62- CHECK_FAILURE(findConfiguration->put_FindDirection(direction));
50+ var findConfiguration = new CoreWebView2FindConfiguration
51+ {
52+ FindTerm = searchTerm,
53+ IsCaseSensitive = caseSensitive,
54+ ShouldHighlightAllMatches = highlightAllMatches,
55+ FindDirection = direction
56+ };
6357
64- // Proceed to execute the find operation with the configured settings.
65- return ExecuteFindOperation(findConfiguration.get());
58+ return await ExecuteFindOperationAsync(findConfiguration);
6659}
6760```
6861### Start a Find Operation
@@ -109,40 +102,16 @@ bool AppWindow::ExecuteFindOperation(ICoreWebView2StagingFindConfiguration* conf
109102```
110103```csharp
111104//! [StartFindOnPage]
112- bool AppWindow::ExecuteFindOperation(ICoreWebView2StagingFindConfiguration* configuration)
105+ public async Task< bool> ExecuteFindOperationAsync(CoreWebView2FindConfiguration configuration)
113106{
114- // Query for the ICoreWebView2Staging17 interface to access the Find feature.
115- auto webView2staging17 = m_webView.try_query<ICoreWebView2Staging17>();
116- CHECK_FEATURE_RETURN(webView2staging17);
107+ var webViewFind = webView.CoreWebView2.FindController; // Assuming webView is your WebView2 control
117108
118- // Get the Find interface.
119- wil::com_ptr<ICoreWebView2StagingFind> webView2stagingfind;
120- CHECK_FAILURE(webView2staging17->get_Find(&webView2stagingfind));
121-
122- // Apply custom UI settings and highlight configurations.
123- CHECK_FAILURE(webView2stagingfind->put_UseCustomUI(false)); // Assuming you want to use the default UI, adjust as necessary.
124- CHECK_FAILURE(webView2stagingfind->put_ShouldHighlightAllMatches(true)); // This should match the passed parameter if dynamic.
125- CHECK_FAILURE(webView2stagingfind->PassHighlightSettings());
126-
127- // Start the find operation with the configured findConfiguration.
128- HRESULT result = webView2stagingfind->StartFind(
129- configuration,
130- Callback<ICoreWebView2StagingFindOperationCompletedHandler>(
131- [this](HRESULT result, LONG ActiveIdx, LONG MatchesCount) -> HRESULT
132- {
133- if (SUCCEEDED(result))
134- {
135- // Handle successful find operation
136- // For example, updating UI elements to reflect the find results
137- }
138- else
139- {
140- // Handle errors appropriately
141- }
142- return S_OK;
143- }).Get());
109+ // Assume CoreWebView2.FindController has been appropriately implemented or wrapped to expose async tasks.
110+ // This example assumes such an implementation for illustration.
111+ await webViewFind.StartFindAsync(configuration);
144112
145- return SUCCEEDED(result);
113+ // Optionally, handle completion or results with events or further async operations.
114+ return true;
146115}
147116
148117//! [StartFindOnPage]
@@ -165,14 +134,10 @@ bool AppWindow::StopFind()
165134```
166135``` csharp
167136// ! [StopFind]
168- bool AppWindow :: StopFind ()
137+ public void StopFindOperation ()
169138{
170- auto webView2staging17 = m_webView .try_query <ICoreWebView2Staging17 >();
171- CHECK_FEATURE_RETURN (webView2staging17 );
172- wil :: com_ptr < ICoreWebView2StagingFind > webView2stagingfind ;
173- CHECK_FAILURE (webView2staging17 ->get_Find (& webView2stagingfind ));
174- CHECK_FAILURE (webView2stagingfind ->StopFind ());
175- return true ;
139+ var webViewFind = webView .CoreWebView2 .FindController ; // Assuming webView is your WebView2 control
140+ webViewFind .StopFind ();
176141}
177142// ! [StopFind]
178143```
@@ -206,20 +171,12 @@ bool AppWindow::GetMatchCount()
206171``` csharp
207172// ! [GetMatchCount]
208173bool AppWindow :: GetMatchCount ()
174+ public async Task < int > GetMatchCountAsync ()
209175{
210- auto webView2staging17 = m_webView .try_query <ICoreWebView2Staging17 >();
211- CHECK_FEATURE_RETURN (webView2staging17 );
212- wil :: com_ptr < ICoreWebView2StagingFind > webView2stagingfind ;
213- CHECK_FAILURE (webView2staging17 ->get_Find (& webView2stagingfind ));
214- LONG matchCount ;
215- CHECK_FAILURE (webView2stagingfind ->get_MatchesCount (& matchCount ));
216-
217- // Update UI or handle matchCount as you wish
218- // For example, you could show a message box
219- std :: wstring matchCountStr = L " Match Count: " + std :: to_wstring (matchCount );
220- MessageBox (m_mainWindow , matchCountStr .c_str (), L " Find Operation" , MB_OK );
221-
222- return true ;
176+ var webViewFind = webView .CoreWebView2 .FindController ; // Assuming webView is your WebView2 control
177+ var matchCount = await webViewFind .GetMatchesCountAsync ();
178+ MessageBox .Show ($" Match Count: {matchCount }" , " Find Operation" , MessageBoxButton .OK );
179+ return matchCount ;
223180}
224181// ! [GetMatchCount]
225182```
@@ -234,10 +191,10 @@ void OnMatchCountChanged(LONG matchesCount)
234191```
235192
236193```csharp
237- void OnMatchCountChanged(LONG matchesCount )
194+ private void OnMatchCountChanged(object sender, EventArgs e )
238195{
239- // Handle match count changes
240- // Update UI elements or perform actions based on the new match count
196+ // Assuming EventArgs or a derived type carries the new match count
197+ MessageBox.Show($"Match count changed. New count: {e.MatchCount}", "Find Operation");
241198}
242199```
243200### Retrieve the Index of the Active Match
@@ -270,23 +227,14 @@ bool AppWindow::GetActiveMatchIndex()
270227
271228``` csharp
272229// ! [GetActiveMatchIndex]
273- bool AppWindow :: GetActiveMatchIndex ()
230+ public async Task < int > GetActiveMatchIndexAsync ()
274231{
275- auto webView2staging17 = m_webView .try_query <ICoreWebView2Staging17 >();
276- CHECK_FEATURE_RETURN (webView2staging17 );
277- wil :: com_ptr < ICoreWebView2StagingFind > webView2stagingfind ;
278- CHECK_FAILURE (webView2staging17 ->get_Find (& webView2stagingfind ));
279- LONG activeMatchIndex ;
280- CHECK_FAILURE (webView2stagingfind ->get_ActiveMatchIndex (& activeMatchIndex ));
281-
282- // Update UI or handle activeMatchIndex as you wish
283- // For example, you could show a message box
284- std :: wstring activeMatchIndexStr =
285- L " Active Match Index: " + std :: to_wstring (activeMatchIndex );
286- MessageBox (m_mainWindow , activeMatchIndexStr .c_str (), L " Find Operation" , MB_OK );
287-
288- return true ;
232+ var webViewFind = webView .CoreWebView2 .FindController ; // Assuming webView is your WebView2 control
233+ var activeMatchIndex = await webViewFind .GetActiveMatchIndexAsync ();
234+ MessageBox .Show ($" Active Match Index: {activeMatchIndex }" , " Find Operation" , MessageBoxButton .OK );
235+ return activeMatchIndex ;
289236}
237+
290238// ! [GetActiveMatchIndex]
291239```
292240
@@ -300,10 +248,10 @@ void OnActiveMatchIndexChanged(ICoreWebView2* sender, ICoreWebView2StagingFindAc
300248```
301249
302250```csharp
303- void OnActiveMatchIndexChanged(ICoreWebView2* sender, ICoreWebView2StagingFindActiveMatchIndexChangedEventArgs* args )
251+ private void OnActiveMatchIndexChanged(object sender, EventArgs e )
304252{
305- // Handle active match index changes
306- // Update UI to reflect the change in the active match index
253+ // Assuming EventArgs or a derived type carries the new active match index
254+ MessageBox.Show($"Active match index changed. New index: {e.ActiveMatchIndex}", "Find Operation");
307255}
308256```
309257
0 commit comments