MainWindow.xaml.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using Microsoft.Web.WebView2.Core;
  2. using System.IO;
  3. using System.Windows;
  4. namespace WPF_VS_Editor
  5. {
  6. /// <summary>
  7. /// Interaction logic for MainWindow.xaml
  8. /// </summary>
  9. public partial class MainWindow : Window
  10. {
  11. private static string MonacoFolder = Path.Combine(
  12. AppDomain.CurrentDomain.BaseDirectory,
  13. @"Monaco");
  14. const string defaultText = @"function helloWorld() {\n\tconsole.log(""Hello world!"");\n}";
  15. public string Text { get; private set; } = defaultText;
  16. public MainWindow()
  17. {
  18. InitializeComponent();
  19. webView21.NavigationCompleted += WebView21_NavigationCompleted;
  20. webView21.WebMessageReceived += WebView21_WebMessageReceived;
  21. webView21.Source = new Uri(Path.Combine(MonacoFolder, "index.html"));
  22. }
  23. private void WebView21_NavigationCompleted(object? sender, CoreWebView2NavigationCompletedEventArgs e)
  24. {
  25. webView21.ExecuteScriptAsync(@$"buildEditor(`{Text}`);");
  26. }
  27. private void WebView21_WebMessageReceived(object? sender, CoreWebView2WebMessageReceivedEventArgs e)
  28. {
  29. Text = e.TryGetWebMessageAsString();
  30. }
  31. private void Button_Click(object sender, RoutedEventArgs e)
  32. {
  33. MessageBox.Show(Text);
  34. }
  35. }
  36. }