|
| 1 | +using System.Windows; |
| 2 | +using Microsoft.Web.WebView2.Core; |
| 3 | + |
| 4 | +namespace WebView2WpfCompositionControl |
| 5 | +{ |
| 6 | + /// <summary> |
| 7 | + /// Interaction logic for MainWindow.xaml |
| 8 | + /// </summary> |
| 9 | + public partial class MainWindow : Window |
| 10 | + { |
| 11 | + public MainWindow() |
| 12 | + { |
| 13 | + InitializeComponent(); |
| 14 | + |
| 15 | + webView.CoreWebView2InitializationCompleted += WebView_CoreWebView2InitializationCompleted; |
| 16 | + webView.NavigationStarting += WebView_NavigationStarting; |
| 17 | + webView.NavigationCompleted += WebView_NavigationCompleted; |
| 18 | + webView.WebMessageReceived += WebView_WebMessageReceived; |
| 19 | + webView.Source = new Uri("https://www.bing.com"); |
| 20 | + } |
| 21 | + |
| 22 | + private void WebView_CoreWebView2InitializationCompleted(object? sender, CoreWebView2InitializationCompletedEventArgs e) |
| 23 | + { |
| 24 | + status.Content = "Initialized:" + (e.InitializationException?.ToString() ?? "Success"); |
| 25 | + } |
| 26 | + |
| 27 | + private void WebView_WebMessageReceived(object? sender, CoreWebView2WebMessageReceivedEventArgs e) |
| 28 | + { |
| 29 | + status.Content = e.TryGetWebMessageAsString(); |
| 30 | + } |
| 31 | + |
| 32 | + private void WebView_NavigationStarting(object? sender, CoreWebView2NavigationStartingEventArgs e) |
| 33 | + { |
| 34 | + status.Content = "NavigationStarting"; |
| 35 | + } |
| 36 | + |
| 37 | + private void WebView_NavigationCompleted(object? sender, CoreWebView2NavigationCompletedEventArgs args) |
| 38 | + { |
| 39 | + // Update address bar |
| 40 | + addressBar.Text = webView.Source.ToString(); |
| 41 | + string info = (args.IsSuccess ? webView.CoreWebView2.DocumentTitle : "error"); |
| 42 | + status.Content = $"NavigationCompleted:{info}"; |
| 43 | + } |
| 44 | + |
| 45 | + private void GoButton_Click(object sender, RoutedEventArgs e) |
| 46 | + { |
| 47 | + if (webView != null && webView.CoreWebView2 != null) |
| 48 | + { |
| 49 | + String uri = addressBar.Text; |
| 50 | + if (uri.StartsWith("http://")) { uri.Replace("http://", "https://"); } |
| 51 | + try |
| 52 | + { |
| 53 | + webView.CoreWebView2.Navigate(uri); |
| 54 | + } |
| 55 | + catch |
| 56 | + { |
| 57 | + status.Content = "InvalidUrl"; |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private void ToggleButton_Click(object sender, RoutedEventArgs e) |
| 63 | + { |
| 64 | + airspaceButton.Visibility = (airspaceButton.IsVisible ? Visibility.Collapsed : Visibility.Visible); |
| 65 | + status.Content = "Toggle:" + airspaceButton.Visibility; |
| 66 | + } |
| 67 | + |
| 68 | + private void AirspaceButton_Click(object sender, RoutedEventArgs e) |
| 69 | + { |
| 70 | + status.Content = "AirspaceClicked"; |
| 71 | + } |
| 72 | + |
| 73 | + private void AddressBar_GotFocus(object sender, RoutedEventArgs e) |
| 74 | + { |
| 75 | + status.Content = "GotFocus:AddressBar"; |
| 76 | + } |
| 77 | + |
| 78 | + private void GoButton_GotFocus(object sender, RoutedEventArgs e) |
| 79 | + { |
| 80 | + status.Content = "GotFocus:GoButton"; |
| 81 | + } |
| 82 | + |
| 83 | + private void ToggleButton_GotFocus(object sender, RoutedEventArgs e) |
| 84 | + { |
| 85 | + status.Content = "GotFocus:ToggleButton"; |
| 86 | + } |
| 87 | + |
| 88 | + private void AirspaceButton_GotFocus(object sender, RoutedEventArgs e) |
| 89 | + { |
| 90 | + status.Content = "GotFocus:AirspaceButton"; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | +} |
| 95 | + |
0 commit comments