Integrate the text editor from Visual Studio Code into my Visual Studio C# .Net WPF Application

Aidan Kinzie 66 Reputation points
2022-07-28T09:31:51.85+00:00

I want to know how to integrate the text editor from Visual Studio Code into my Visual Studio C# .Net WPF Application.

Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Reza Aghaei 4,986 Reputation points MVP Volunteer Moderator
    2022-07-28T21:30:53.493+00:00

    The Monaco Editor is the code editor that powers VS Code.

    I've already posted an answer in stackoverflow for WinForms: How to use the Monaco editor inside a Windows Forms application?, here I'll post a WPF version of my answer which is pretty similar.

    How to use the Monaco editor inside a WPF application

    You can use a WebView2 control to show the Monaco editor in a WPF, then you can have a code editor which supports editing the syntax-highlighted code which supports intellisense and much more.
    Please note that the Monaco Editor no longer supports IE 11. The last version that was tested on IE 11 is 0.18.1.

    WPF Monaco Editor

    To do so, follow these steps:

    1. Create a WPF Application (.NET, or .NET Framework)
    2. Install Microsoft.Web.WebView2 NuGet package (The Monaco Editor no longer supports IE 11. The last version that was tested on IE 11 is 0.18.1)
    3. Create a folder named Monaco in your project.
    4. Download Monaco editor from Monaco Editor site. (I tested by downloading version 0.33.0)
    5. In the file explorer, open the Mocano folder, then extract the downloaded file and copy the min subfolder of extracted files into your Monaco folder.
    6. Add index.html file to the Monaco folder in filesystem, with the following content: <!DOCTYPE html>
      <html>
      <head>
      <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
      <link rel="stylesheet"
      data-name="vs/editor/editor.main"
      href="./min/vs/editor/editor.main.css" />
      <style>
      html, body { height: 100%; margin: 0; }
      #container { height: 100%; }
      </style>
      </head>
      <body>
      <div id="container"></div>
      <script src="./min/vs/loader.js"></script>
      <script>
      require.config({ paths: { 'vs': './min/vs' } });
      </script>
      <script src="./min/vs/editor/editor.main.nls.js"></script>
      <script src="./min/vs/editor/editor.main.js"></script>
      <script>
      var editor = monaco.editor.create(document.getElementById('container'), {
      value: 'function helloWorld() {\n\tconsole.log("Hello world!");\n}',
      language: 'javascript'
      });
      </script>
      </body>
      </html>
    7. Right click on the project file and choose edit. Then find the following piece of code(if exists): <ItemGroup>
      <Folder Include="Monaco\" />
      </ItemGroup>
    8. And replace it with the following: <ItemGroup>
      <Content Include="Monaco**">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
      </Content>
      </ItemGroup> It basically includes all the files under the Monaco folder into the project and also copies them into the output directory.
      Please note, for a .NET Framework project you need to first unload the project, and then after editing the project file, reload it.
    9. Drop an instance of WebView2 on the main window, like this: <Grid>
      <Wpf:WebView2 x:Name="webView21"/>
      </Grid>
    10. Handle the Load event of the window with the following code:
      private void Window_Loaded(object sender, RoutedEventArgs e)   
      
      {
      this.webView21.Source =
      new Uri(System.IO.Path.Combine(
      System.AppDomain.CurrentDomain.BaseDirectory,
      @"Monaco\index.html"));
      }
    11. Run the application and see the result, the code editor with syntax-highlighted code which supports intellisense.
    2 people found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.