UNIXworkcode

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading; 6 using System.Threading.Tasks; 7 using System.Windows; 8 9 namespace UI 10 { 11 public class Application 12 { 13 private static Application instance; 14 15 private System.Windows.Application application; 16 17 private Thread thread; 18 19 public String Name; 20 21 public IApplicationCallbacks callbacks; 22 23 public List<Window> Windows = new List<Window>(); 24 public ApplicationMenu Menu = new ApplicationMenu(); 25 public MainToolBar ToolBar = new MainToolBar(); 26 27 private Application() : base() 28 { 29 thread = new Thread(() => RunApplication()); 30 thread.SetApartmentState(ApartmentState.STA); 31 } 32 33 public static Application GetInstance() 34 { 35 if (instance == null) 36 { 37 instance = new Application(); 38 GC.KeepAlive(instance); 39 } 40 return instance; 41 } 42 43 public Thread Start() 44 { 45 thread.Start(); 46 return thread; 47 } 48 49 private void RunApplication() 50 { 51 application = new System.Windows.Application(); 52 53 if(callbacks != null) 54 { 55 callbacks.OnStartup(); 56 } 57 application.Run(); 58 if(callbacks != null) 59 { 60 callbacks.OnExit(); 61 } 62 } 63 64 public void AddWindow(Window window) 65 { 66 Windows.Add(window); 67 } 68 69 public void RemoveWindow(Window window) 70 { 71 Windows.Remove(window); 72 if (Windows.Count == 0) 73 { 74 application.Shutdown(); 75 } 76 } 77 } 78 79 public class ResultExec<T> 80 { 81 public T Result; 82 public Func<T> Func; 83 84 public void Exec() 85 { 86 Result = Func.Invoke(); 87 } 88 } 89 90 public class VoidExec 91 { 92 public Action Action; 93 94 public void Exec() 95 { 96 Action.Invoke(); 97 } 98 } 99 100 public interface IApplicationCallbacks 101 { 102 void OnStartup(); 103 void OnOpen(); 104 void OnExit(); 105 } 106 } 107