Program.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using System.Threading;
  5. using Avalonia;
  6. using Avalonia.Controls;
  7. using Avalonia.Controls.Remote;
  8. using Avalonia.Remote.Protocol;
  9. using Avalonia.Threading;
  10. using ControlCatalog;
  11. namespace RemoteDemo
  12. {
  13. class Program
  14. {
  15. static void Main(string[] args)
  16. {
  17. AppBuilder.Configure<App>().UsePlatformDetect().SetupWithoutStarting();
  18. var l = new TcpListener(IPAddress.Loopback, 0);
  19. l.Start();
  20. var port = ((IPEndPoint) l.LocalEndpoint).Port;
  21. l.Stop();
  22. var transport = new BsonTcpTransport();
  23. transport.Listen(IPAddress.Loopback, port, sc =>
  24. {
  25. Dispatcher.UIThread.Post(() =>
  26. {
  27. new RemoteServer(sc).Content = new MainView();
  28. });
  29. });
  30. var cts = new CancellationTokenSource();
  31. transport.Connect(IPAddress.Loopback, port).ContinueWith(t =>
  32. {
  33. Dispatcher.UIThread.Post(() =>
  34. {
  35. var window = new Window()
  36. {
  37. Content = new RemoteWidget(t.Result)
  38. };
  39. window.Closed += delegate { cts.Cancel(); };
  40. window.Show();
  41. });
  42. });
  43. Dispatcher.UIThread.MainLoop(cts.Token);
  44. }
  45. }
  46. }