|
| 1 | +using Microsoft.DotNet.Interactive; |
| 2 | +using Microsoft.DotNet.Interactive.Commands; |
| 3 | +using Microsoft.DotNet.Interactive.CSharp; |
| 4 | +using Microsoft.DotNet.Interactive.Server; |
| 5 | +using System.CommandLine; |
| 6 | +using System.CommandLine.Invocation; |
| 7 | +using System.IO; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using System.Windows; |
| 10 | + |
| 11 | +namespace WpfConnect |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Interaction logic for App.xaml |
| 15 | + /// </summary> |
| 16 | + public partial class App : Application |
| 17 | + { |
| 18 | + private CompositeKernel _Kernel; |
| 19 | + |
| 20 | + private const string NamedPipeName = "InteractiveWpf"; |
| 21 | + |
| 22 | + private bool RunOnDispatcher { get; set; } |
| 23 | + |
| 24 | + protected override void OnStartup(StartupEventArgs e) |
| 25 | + { |
| 26 | + base.OnStartup(e); |
| 27 | + _Kernel = new CompositeKernel(); |
| 28 | + _Kernel.UseLog(); |
| 29 | + |
| 30 | + AddDispatcherCommand(_Kernel); |
| 31 | + |
| 32 | + CSharpKernel csharpKernel = RegisterCSharpKernel(); |
| 33 | + |
| 34 | + _ = Task.Run(async () => |
| 35 | + { |
| 36 | + //Load WPF app assembly |
| 37 | + await csharpKernel.SendAsync(new SubmitCode(@$"#r ""{typeof(App).Assembly.Location}"" |
| 38 | +using {nameof(WpfConnect)};")); |
| 39 | + //Add the WPF app as a variable that can be accessed |
| 40 | + await csharpKernel.SetVariableAsync("App", this); |
| 41 | + |
| 42 | + //Start named pipe |
| 43 | + _Kernel.UseNamedPipeKernelServer(NamedPipeName, new DirectoryInfo(".")); |
| 44 | + }); |
| 45 | + } |
| 46 | + |
| 47 | + protected override void OnExit(ExitEventArgs e) |
| 48 | + { |
| 49 | + _Kernel?.Dispose(); |
| 50 | + base.OnExit(e); |
| 51 | + } |
| 52 | + |
| 53 | + private void AddDispatcherCommand(Kernel kernel) |
| 54 | + { |
| 55 | + var dispatcherCommand = new Command("#!dispatcher", "Enable or disable running code on the Dispatcher") |
| 56 | + { |
| 57 | + new Option<bool>("--enabled", getDefaultValue:() => true) |
| 58 | + }; |
| 59 | + dispatcherCommand.Handler = CommandHandler.Create<bool>(enabled => |
| 60 | + { |
| 61 | + RunOnDispatcher = enabled; |
| 62 | + }); |
| 63 | + kernel.AddDirective(dispatcherCommand); |
| 64 | + } |
| 65 | + |
| 66 | + private CSharpKernel RegisterCSharpKernel() |
| 67 | + { |
| 68 | + var csharpKernel = new CSharpKernel() |
| 69 | + .UseDefaultFormatting() |
| 70 | + .UseNugetDirective() |
| 71 | + .UseKernelHelpers() |
| 72 | + .UseWho() |
| 73 | + .UseDotNetVariableSharing() |
| 74 | + //This is added locally |
| 75 | + .UseWpf(); |
| 76 | + |
| 77 | + _Kernel.Add(csharpKernel); |
| 78 | + |
| 79 | + csharpKernel.AddMiddleware(async (KernelCommand command, KernelInvocationContext context, KernelPipelineContinuation next) => |
| 80 | + { |
| 81 | + if (RunOnDispatcher) |
| 82 | + { |
| 83 | + await Dispatcher.InvokeAsync(async () => await next(command, context)); |
| 84 | + } |
| 85 | + else |
| 86 | + { |
| 87 | + await next(command, context); |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + return csharpKernel; |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments