How can I detect when a user closes the web page and when he closes it, display an alert message using the viewmodel with the browser.OpenAsync method?
Asked
Active
Viewed 470 times
0
-
1I don't believe there is any way to do this with the Essentials Browser. You can always write your own page with a WebBrowser control if you need more control over the behavior – Jason Apr 28 '22 at 22:44
-
For extern Browser it's impossible to detect the user behavior , but for internal webview you can create a custom renderer and override `onCloseWindow` method in WebChromeClient , refer to [here](https://stackoverflow.com/questions/7165036/how-do-i-detect-when-a-webview-page-is-trying-to-close) . – ColeX Apr 29 '22 at 06:03
-
You could use OnResume() (from App.xaml.cs) to be notified when the app resumed and check if the active viewModel is the one with browser.OpenAsync and do your code on that event. This approach is a more general one, if you need more specific stuff, you should probably go with your own webview, as @ColeX-MSFT suggested. – alexgavru Apr 30 '22 at 23:47
-
OnAppearing() is triggered after the user returns from the open browser page to the app content page. You could simply set a boolean property to true before calling await Browser.OpenAsync. Then, simply check the value within OnAppearing. If true, display alert and set the property back to false. – chri3g91 Aug 09 '22 at 07:24
1 Answers
0
Simple example Javascript.
window.onbeforeunload = function () {
alert("Do you really want to close?");
};

Karim Fahmy
- 215
- 1
- 11