{"id":100265,"date":"2018-11-19T07:00:00","date_gmt":"2018-11-19T22:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=100265"},"modified":"2019-12-21T08:35:03","modified_gmt":"2019-12-21T16:35:03","slug":"20181119-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20181119-00\/?p=100265","title":{"rendered":"How do I suppress the &#8220;Did you mean to switch apps?&#8221; warning message from my XAML WebView control?"},"content":{"rendered":"<p>A customer had an application that used a <a href=\"https:\/\/docs.microsoft.com\/en-us\/uwp\/api\/windows.ui.xaml.controls.webview\"> UWP XAML WebView control<\/a>. There are some points at which the user can click an element in the hosted HTML which triggers the main app to navigate to another part of the app entirely. The customer didn&#8217;t give any details, but I can imagine this being, say, an app that contains multiple mini-web-apps (say, some web games), and on the main page of each game, there&#8217;s a button called <i>Play a different game<\/i>.<\/p>\n<p>Or maybe it&#8217;s something else entirely. Whatever.<\/p>\n<p>The customer managed to find a way to do this: The app registered for a protocol, let&#8217;s call it <code>contoso-game:<\/code>, and the <i>Exit<\/i> button navigates the WebView control to <code>contoso-game:mainmenu<\/code>. This works fine, but when the user clicks the <i>Exit<\/i> button, there is a warning dialog that appears first:<\/p>\n<div style=\"border: solid 1px black; font-family: Segoe UI, sans-serif; width: 30pc;\">\n<div style=\"background-color: white; color: black; font-size: 9pt; padding: 6px;\">Did you mean to switch apps?<\/div>\n<div style=\"background-color: #0067b2; color: white; padding: 10px;\">\n<div style=\"font-size: 20pt;\">Did you mean to switch apps?<\/div>\n<div style=\"font-size: 12pt; padding: 10px 0px; font-weight: 300;\">&#8220;Contoso&#8221; is trying to open &#8220;Contoso&#8221;.<\/div>\n<div style=\"overflow: hidden;\">\n<div style=\"float: right; margin: 10px; text-align: center;\"><span style=\"display: inline-block; width: 7em; padding: 2px 0px; border: solid 2px white;\">Yes<\/span> \u00a0\u00a0 <span style=\"display: inline-block; width: 7em; padding: 2px 0px; border: solid 2px white; background-color: #1f8cfa;\">No<\/span><\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>Is there a way to suppress this dialog box? It looks silly to have the program ask permission to switch to itself.<\/p>\n<p>Your program can handle <a href=\"https:\/\/docs.microsoft.com\/en-us\/uwp\/api\/windows.ui.xaml.controls.webview.unsupportedurischemeidentified\"> the <code>WebView.<code><\/code>Unsupported\u00adUri\u00adScheme\u00adIdentified<\/code> event<\/a>.<\/p>\n<pre>&lt;WebView\r\n    ...\r\n    UnsupportedUriSchemeIdentified=\"OnUnsupportedUriSchemeIdentified\" \/&gt;\r\n\r\nvoid OnUnsupportedUriSchemeIdentified(\r\n    WebView sender,\r\n    WebViewUnsupportedUriSchemeIdentifiedEventArgs e) \r\n{ \r\n    \/\/ Silently allow all navigations back to the app itself.\r\n    if (e.Uri.Scheme == \"contoso-game\") \r\n    { \r\n        e.Handled = true; \r\n\r\n        \/\/ Use the existing URI parser on the main page.\r\n        MainPage.InternalNavigateToUri(e.Uri);\r\n    } \r\n}\r\n<\/pre>\n<p>When the user clicks a link that uses a <code>contoso-game<\/code> protocol, we mark the event as handled (so that the WebView control takes no further action), and then forward the URI to the existing code in our main page that deals with URI activations. Basically, we short-circuit the activation and handle it internally. This is particularly useful if there is more than once running instance of the Contoso program: Short-circuiting the activation means that the navigation is handled by instance the user the clicked on.<\/p>\n<p>Note that intercepting the event from the WebView means that you don&#8217;t need to register the protocol in their application manifest because the protocol is never activated. Instead, the program intercepts it and performs an internal navigation.<\/p>\n<p>Of course, if the customer were using the <code>contoso-game<\/code> for other reasons, then they should leave it in their manifest.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It will ask you what to do, and you can tell it to do nothing.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-100265","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>It will ask you what to do, and you can tell it to do nothing.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/100265","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=100265"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/100265\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=100265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=100265"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=100265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}