-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathMainWindow.axaml.cs
65 lines (56 loc) · 2.83 KB
/
MainWindow.axaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using System.IO;
using System.Reflection;
using System.Threading;
namespace CSharpEditorDemo
{
public class MainWindow : Window
{
CSharpEditor.Editor Editor;
public MainWindow()
{
InitializeComponent();
this.Opened += async (s, e) =>
{
// Initial source code
string sourceText = "";
using (Stream stream = this.GetType().Assembly.GetManifestResourceStream("CSharpEditorDemo.HelloWorld.cs"))
using (StreamReader reader = new StreamReader(stream))
{
sourceText = reader.ReadToEnd();
}
// Minimal set of references for a console application - double check these with your target framework version - sometimes they change.
string systemRuntime = Path.Combine(Path.GetDirectoryName(typeof(object).Assembly.Location), "System.Runtime.dll");
CSharpEditor.CachedMetadataReference[] minimalReferences = new CSharpEditor.CachedMetadataReference[]
{
CSharpEditor.CachedMetadataReference.CreateFromFile(systemRuntime), // System.Runtime.dll
CSharpEditor.CachedMetadataReference.CreateFromFile(typeof(object).Assembly.Location), // System.Private.CoreLib.dll
CSharpEditor.CachedMetadataReference.CreateFromFile(typeof(System.Console).Assembly.Location) // System.Console.dll
};
Editor = await CSharpEditor.Editor.Create(sourceText, references: minimalReferences, compilationOptions: new CSharpCompilationOptions(OutputKind.ConsoleApplication));
Grid.SetRow(Editor, 1);
this.FindControl<Grid>("MainGrid").Children.Add(Editor);
};
this.FindControl<Button>("RunButton").Click += async (s, e) =>
{
Assembly assembly = (await Editor.Compile(Editor.SynchronousBreak, Editor.AsynchronousBreak)).Assembly;
if (assembly != null)
{
// Run on a separate thread, in order to enable breakpoints in synchronous functions.
// No need to use a separate thread if the entry point is async.
new Thread(() =>
{
assembly.EntryPoint.Invoke(null, new object[assembly.EntryPoint.GetParameters().Length]);
}).Start();
}
};
}
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
}
}