88// language governing permissions and limitations under the License.
99
1010using Esri . ArcGISRuntime . Security ;
11+ using Microsoft . Web . WebView2 . Core ;
12+ using Microsoft . Web . WebView2 . Wpf ;
1113using System ;
1214using System . Collections . Generic ;
1315using System . Threading . Tasks ;
1416using System . Windows ;
15- using System . Windows . Controls ;
16- using System . Windows . Navigation ;
1717using System . Windows . Threading ;
1818
1919namespace ArcGIS . Helpers
@@ -115,18 +115,17 @@ public Task<IDictionary<string, string>> AuthorizeAsync(Uri serviceUri, Uri auth
115115 // Challenge for OAuth credentials on the UI thread.
116116 private void AuthorizeOnUIThread ( string authorizeUri )
117117 {
118- // Create a WebBrowser control to display the authorize page.
119- WebBrowser webBrowser = new WebBrowser ( ) ;
118+ // Initialize a WebView2 control to display the authorize page.
119+ WebView2 webBrowser = new WebView2 ( ) { MinWidth = 500 , MinHeight = 500 } ;
120120
121121 // Handle the navigation event for the browser to check for a response to the redirect URL.
122- webBrowser . Navigating += WebBrowserOnNavigating ;
122+ webBrowser . NavigationStarting += WebBrowserOnNavigationStarting ;
123123
124124 // Display the web browser in a new window.
125125 _authWindow = new Window
126126 {
127127 Content = webBrowser ,
128- Width = 450 ,
129- Height = 450 ,
128+ SizeToContent = SizeToContent . WidthAndHeight ,
130129 WindowStartupLocation = WindowStartupLocation . CenterOwner
131130 } ;
132131
@@ -136,9 +135,15 @@ private void AuthorizeOnUIThread(string authorizeUri)
136135 _authWindow . Owner = Application . Current . MainWindow ;
137136 }
138137
139- // Handle the window closed event then navigate to the authorize url.
138+ // Handle window loaded event as the WebView2 control can only be initialized after it is visible in the UI
139+ _authWindow . Loaded += async ( s , e ) =>
140+ {
141+ await webBrowser . EnsureCoreWebView2Async ( ) ;
142+ webBrowser . CoreWebView2 . Navigate ( authorizeUri ) ;
143+ } ;
144+
145+ // Handle the window closed event
140146 _authWindow . Closed += OnWindowClosed ;
141- webBrowser . Navigate ( authorizeUri ) ;
142147
143148 // Display the window.
144149 _authWindow . ShowDialog ( ) ;
@@ -163,17 +168,16 @@ private void OnWindowClosed(object sender, EventArgs e)
163168 _authWindow = null ;
164169 }
165170
166- // Handle browser navigation (content changing).
167- private void WebBrowserOnNavigating ( object sender , NavigatingCancelEventArgs e )
171+ // Handle browser navigation
172+ private void WebBrowserOnNavigationStarting ( object sender , CoreWebView2NavigationStartingEventArgs e )
168173 {
169174 // Check for a response to the callback url.
170175 const string portalApprovalMarker = "/oauth2/approval" ;
171- WebBrowser webBrowser = sender as WebBrowser ;
172176
173- Uri uri = e . Uri ;
177+ Uri uri = new Uri ( e . Uri ) ;
174178
175179 // If no browser, uri, or an empty url, return.
176- if ( webBrowser == null || uri == null || string . IsNullOrEmpty ( uri . AbsoluteUri ) )
180+ if ( sender == null || uri == null || string . IsNullOrEmpty ( uri . AbsoluteUri ) )
177181 return ;
178182
179183 // Check for redirect.
0 commit comments