44
55using System ;
66using System . Collections . Generic ;
7+ using System . Diagnostics ;
78using System . Linq ;
89using System . Text ;
910using System . Threading . Tasks ;
@@ -25,8 +26,10 @@ namespace WebView2WpfBrowser
2526 /// </summary>
2627 public partial class MainWindow : Window
2728 {
29+ public static RoutedCommand InjectScriptCommand = new RoutedCommand ( ) ;
2830 bool _isNavigating = false ;
2931
32+
3033 public MainWindow ( )
3134 {
3235 InitializeComponent ( ) ;
@@ -82,14 +85,70 @@ void BrowseStopCmdExecuted(object target, ExecutedRoutedEventArgs e)
8285 webView . Stop ( ) ;
8386 }
8487
88+ void CoreWebView2RequiringCmdsCanExecute ( object sender , CanExecuteRoutedEventArgs e )
89+ {
90+ e . CanExecute = webView != null && webView . CoreWebView2 != null ;
91+ }
92+
93+ double ZoomStep ( )
94+ {
95+ if ( webView . ZoomFactor < 1 )
96+ {
97+ return 0.25 ;
98+ }
99+ else if ( webView . ZoomFactor < 2 )
100+ {
101+ return 0.5 ;
102+ }
103+ else
104+ {
105+ return 1 ;
106+ }
107+ }
108+
109+ void IncreaseZoomCmdCanExecute ( object sender , CanExecuteRoutedEventArgs e )
110+ {
111+ e . CanExecute = webView != null ;
112+ }
113+
114+ void IncreaseZoomCmdExecuted ( object target , ExecutedRoutedEventArgs e )
115+ {
116+ webView . ZoomFactor += ZoomStep ( ) ;
117+ }
118+
119+ void DecreaseZoomCmdCanExecute ( object sender , CanExecuteRoutedEventArgs e )
120+ {
121+ e . CanExecute = ( webView != null ) && ( webView . ZoomFactor - ZoomStep ( ) > 0.0 ) ;
122+ }
123+
124+ void DecreaseZoomCmdExecuted ( object target , ExecutedRoutedEventArgs e )
125+ {
126+ webView . ZoomFactor -= ZoomStep ( ) ;
127+ }
128+
129+ async void InjectScriptCmdExecuted ( object target , ExecutedRoutedEventArgs e )
130+ {
131+ var dialog = new TextInputDialog (
132+ title : "Inject Script" ,
133+ description : "Enter some JavaScript to be executed in the context of this page." ,
134+ defaultInput : "window.getComputedStyle(document.body).backgroundColor" ) ;
135+ if ( dialog . ShowDialog ( ) == true ) {
136+ string scriptResult = await webView . ExecuteScriptAsync ( dialog . Input . Text ) ;
137+ MessageBox . Show ( this , scriptResult , "Script Result" ) ;
138+ }
139+ }
140+
85141 void GoToPageCmdCanExecute ( object sender , CanExecuteRoutedEventArgs e )
86142 {
87143 e . CanExecute = webView != null && ! _isNavigating ;
88144 }
89145
90- void GoToPageCmdExecuted ( object target , ExecutedRoutedEventArgs e )
146+ async void GoToPageCmdExecuted ( object target , ExecutedRoutedEventArgs e )
91147 {
92- webView . Source = new Uri ( ( string ) e . Parameter ) ;
148+ // Setting webView.Source will not trigger a navigation if the Source is the same
149+ // as the previous Source. CoreWebView.Navigate() will always trigger a navigation.
150+ await webView . EnsureCoreWebView2Async ( ) ;
151+ webView . CoreWebView2 . Navigate ( ( string ) e . Parameter ) ;
93152 }
94153
95154 void WebView_NavigationStarting ( object sender , CoreWebView2NavigationStartingEventArgs e )
0 commit comments