|
| 1 | +using Avalonia; |
| 2 | +using Avalonia.Controls; |
| 3 | +using Avalonia.Controls.ApplicationLifetimes; |
| 4 | +using Avalonia.Markup.Xaml; |
| 5 | +using Live.Avalonia; |
| 6 | +using ReactiveUI; |
| 7 | +using System; |
| 8 | +using System.Diagnostics; |
| 9 | +using System.Reactive; |
| 10 | +using Todo.Services; |
| 11 | +using Todo.ViewModels; |
| 12 | +using Todo.Views; |
| 13 | + |
| 14 | +namespace Todo |
| 15 | +{ |
| 16 | + public class App : Application, ILiveView |
| 17 | + { |
| 18 | + public override void Initialize() |
| 19 | + { |
| 20 | + AvaloniaXamlLoader.Load(this); |
| 21 | + } |
| 22 | + |
| 23 | + public override void OnFrameworkInitializationCompleted() |
| 24 | + { |
| 25 | + if (Debugger.IsAttached || IsProduction()) |
| 26 | + { |
| 27 | + // Debugging requires pdb loading etc, so we disable live reloading |
| 28 | + // during a test run with an attached debugger. |
| 29 | + var window = new Window(); |
| 30 | + window.Content = CreateView(window); |
| 31 | + window.Show(); |
| 32 | + } |
| 33 | + else |
| 34 | + { |
| 35 | + // Here, we create a new LiveViewHost, located in the 'Live.Avalonia' |
| 36 | + // namespace, and pass an ILiveView implementation to it. The ILiveView |
| 37 | + // implementation should have a parameterless constructor! Next, we |
| 38 | + // start listening for any changes in the source files. And then, we |
| 39 | + // show the LiveViewHost window. Simple enough, huh? |
| 40 | + var window = new LiveViewHost(this, Console.WriteLine); |
| 41 | + window.StartWatchingSourceFilesForHotReloading(); |
| 42 | + window.Show(); |
| 43 | + } |
| 44 | + |
| 45 | + // Here we subscribe to ReactiveUI default exception handler to avoid app |
| 46 | + // termination in case if we do something wrong in our view models. See: |
| 47 | + // https://www.reactiveui.net/docs/handbook/default-exception-handler/ |
| 48 | + // |
| 49 | + // In case if you are using another MV* framework, please refer to its |
| 50 | + // documentation explaining global exception handling. |
| 51 | + RxApp.DefaultExceptionHandler = Observer.Create<Exception>(Console.WriteLine); |
| 52 | + base.OnFrameworkInitializationCompleted(); |
| 53 | + } |
| 54 | + |
| 55 | + public object CreateView(Window window) |
| 56 | + { |
| 57 | + var db = new Database(); |
| 58 | + window.DataContext ??= new MainWindowViewModel(db); |
| 59 | + return new MainWindow(); |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + private static bool IsProduction() |
| 64 | + { |
| 65 | +#if DEBUG |
| 66 | + return false; |
| 67 | +#else |
| 68 | + return true; |
| 69 | +#endif |
| 70 | + } |
| 71 | + } |
| 72 | +} |
0 commit comments