|
| 1 | +using System; |
| 2 | + |
| 3 | +using Microsoft.Extensions.Hosting; |
| 4 | +using Microsoft.Extensions.Localization; |
| 5 | +using Microsoft.Extensions.Logging; |
| 6 | + |
| 7 | +using Uno.Resizetizer; |
| 8 | +using UnoFileDownloader.Business; |
| 9 | +using UnoFileDownloader.Business.Models; |
| 10 | +using UnoFileDownloader.Presentation; |
| 11 | +using UnoFileDownloader.Utils; |
| 12 | + |
| 13 | +namespace UnoFileDownloader; |
| 14 | +public partial class App : Application |
| 15 | +{ |
| 16 | + /// <summary> |
| 17 | + /// Initializes the singleton application object. This is the first line of authored code |
| 18 | + /// executed, and as such is the logical equivalent of main() or WinMain(). |
| 19 | + /// </summary> |
| 20 | + public App() |
| 21 | + { |
| 22 | + this.InitializeComponent(); |
| 23 | + } |
| 24 | + |
| 25 | + protected Window? MainWindow { get; private set; } |
| 26 | + protected IHost? Host { get; private set; } |
| 27 | + |
| 28 | + protected async override void OnLaunched(LaunchActivatedEventArgs args) |
| 29 | + { |
| 30 | + |
| 31 | + |
| 32 | + var builder = this.CreateBuilder(args) |
| 33 | + .UseToolkitNavigation() |
| 34 | + .Configure(host => |
| 35 | + { |
| 36 | + host.UseConfiguration(configure: configBuilder => |
| 37 | + configBuilder |
| 38 | + .EmbeddedSource<App>() |
| 39 | + .Section<AppConfig>()) |
| 40 | + .UseLocalization() |
| 41 | + .ConfigureServices((context, services) => |
| 42 | + { |
| 43 | + services.AddSingleton<IDispatcherQueueProvider>(c => |
| 44 | + { |
| 45 | + return new DispatcherQueueProvider(MainWindow!.DispatcherQueue); |
| 46 | + }); |
| 47 | + |
| 48 | + // 下载文件管理器 |
| 49 | + services.AddSingleton<DownloadFileListManager>(); |
| 50 | + }) |
| 51 | + .UseNavigation(ReactiveViewModelMappings.ViewModelMappings, RegisterRoutes) |
| 52 | + ; |
| 53 | + }); |
| 54 | + MainWindow = builder.Window; |
| 55 | + //MainWindow = new Window(); |
| 56 | +#if DEBUG |
| 57 | + MainWindow.EnableHotReload(); |
| 58 | +#endif |
| 59 | + Host = await builder.NavigateAsync<Shell>(); |
| 60 | + var localizer = (Uno.Extensions.Localization.ResourceLoaderStringLocalizer) Host.Services.GetRequiredService<IStringLocalizer>(); |
| 61 | + string title = localizer["ApplicationName"]; |
| 62 | + |
| 63 | + MainWindow.Title = title; |
| 64 | + |
| 65 | + //// Do not repeat app initialization when the Window already has content, |
| 66 | + //// just ensure that the window is active |
| 67 | + //if (MainWindow.Content is not Frame rootFrame) |
| 68 | + //{ |
| 69 | + // // Create a Frame to act as the navigation context and navigate to the first page |
| 70 | + // rootFrame = new Frame(); |
| 71 | + |
| 72 | + // // Place the frame in the current Window |
| 73 | + // MainWindow.Content = rootFrame; |
| 74 | + |
| 75 | + // rootFrame.NavigationFailed += OnNavigationFailed; |
| 76 | + //} |
| 77 | + |
| 78 | + //if (rootFrame.Content == null) |
| 79 | + //{ |
| 80 | + // // When the navigation stack isn't restored navigate to the first page, |
| 81 | + // // configuring the new page by passing required information as a navigation |
| 82 | + // // parameter |
| 83 | + // rootFrame.Navigate(typeof(Shell), args.Arguments); |
| 84 | + //} |
| 85 | + |
| 86 | + MainWindow.SetWindowIcon(); |
| 87 | + // Ensure the current window is active |
| 88 | + MainWindow.Activate(); |
| 89 | + } |
| 90 | + |
| 91 | + private static void RegisterRoutes(IViewRegistry views, IRouteRegistry routes) |
| 92 | + { |
| 93 | + views.Register |
| 94 | + ( |
| 95 | + new ViewMap(ViewModel: typeof(ShellModel), View: typeof(Shell)), |
| 96 | + new ViewMap<MainPage, MainModel>(), |
| 97 | + new ViewMap<AboutPage, AboutModel>(), |
| 98 | + new ViewMap<NewTaskPage, NewTaskModel>(), |
| 99 | + new DataViewMap<SecondPage, SecondModel, Entity>() |
| 100 | + ); |
| 101 | + |
| 102 | + // 必须写注册哦,否则无法跳转 |
| 103 | + routes.Register |
| 104 | + ( |
| 105 | + new RouteMap("", View: views.FindByViewModel<ShellModel>(), |
| 106 | + Nested: new RouteMap[] |
| 107 | + { |
| 108 | + new RouteMap("Main", View: views.FindByViewModel<MainModel>()), |
| 109 | + new RouteMap("Second", View: views.FindByViewModel<SecondModel>()), |
| 110 | + new RouteMap("About", View: views.FindByViewModel<AboutModel>()), |
| 111 | + new RouteMap("NewTask",View: views.FindByViewModel<NewTaskModel>()), |
| 112 | + } |
| 113 | + ) |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + /// <summary> |
| 118 | + /// Invoked when Navigation to a certain page fails |
| 119 | + /// </summary> |
| 120 | + /// <param name="sender">The Frame which failed navigation</param> |
| 121 | + /// <param name="e">Details about the navigation failure</param> |
| 122 | + void OnNavigationFailed(object sender, NavigationFailedEventArgs e) |
| 123 | + { |
| 124 | + throw new InvalidOperationException($"Failed to load {e.SourcePageType.FullName}: {e.Exception}"); |
| 125 | + } |
| 126 | + |
| 127 | + /// <summary> |
| 128 | + /// Configures global Uno Platform logging |
| 129 | + /// </summary> |
| 130 | + public static void InitializeLogging() |
| 131 | + { |
| 132 | +#if DEBUG |
| 133 | + // Logging is disabled by default for release builds, as it incurs a significant |
| 134 | + // initialization cost from Microsoft.Extensions.Logging setup. If startup performance |
| 135 | + // is a concern for your application, keep this disabled. If you're running on the web or |
| 136 | + // desktop targets, you can use URL or command line parameters to enable it. |
| 137 | + // |
| 138 | + // For more performance documentation: https://platform.uno/docs/articles/Uno-UI-Performance.html |
| 139 | + |
| 140 | + var factory = LoggerFactory.Create(builder => |
| 141 | + { |
| 142 | +#if __WASM__ |
| 143 | + builder.AddProvider(new global::Uno.Extensions.Logging.WebAssembly.WebAssemblyConsoleLoggerProvider()); |
| 144 | +#elif __IOS__ || __MACCATALYST__ |
| 145 | + builder.AddProvider(new global::Uno.Extensions.Logging.OSLogLoggerProvider()); |
| 146 | +#else |
| 147 | + builder.AddConsole(); |
| 148 | +#endif |
| 149 | + |
| 150 | + // Exclude logs below this level |
| 151 | + builder.SetMinimumLevel(LogLevel.Information); |
| 152 | + |
| 153 | + // Default filters for Uno Platform namespaces |
| 154 | + builder.AddFilter("Uno", LogLevel.Warning); |
| 155 | + builder.AddFilter("Windows", LogLevel.Warning); |
| 156 | + builder.AddFilter("Microsoft", LogLevel.Warning); |
| 157 | + |
| 158 | + // Generic Xaml events |
| 159 | + // builder.AddFilter("Microsoft.UI.Xaml", LogLevel.Debug ); |
| 160 | + // builder.AddFilter("Microsoft.UI.Xaml.VisualStateGroup", LogLevel.Debug ); |
| 161 | + // builder.AddFilter("Microsoft.UI.Xaml.StateTriggerBase", LogLevel.Debug ); |
| 162 | + // builder.AddFilter("Microsoft.UI.Xaml.UIElement", LogLevel.Debug ); |
| 163 | + // builder.AddFilter("Microsoft.UI.Xaml.FrameworkElement", LogLevel.Trace ); |
| 164 | + |
| 165 | + // Layouter specific messages |
| 166 | + // builder.AddFilter("Microsoft.UI.Xaml.Controls", LogLevel.Debug ); |
| 167 | + // builder.AddFilter("Microsoft.UI.Xaml.Controls.Layouter", LogLevel.Debug ); |
| 168 | + // builder.AddFilter("Microsoft.UI.Xaml.Controls.Panel", LogLevel.Debug ); |
| 169 | + |
| 170 | + // builder.AddFilter("Windows.Storage", LogLevel.Debug ); |
| 171 | + |
| 172 | + // Binding related messages |
| 173 | + // builder.AddFilter("Microsoft.UI.Xaml.Data", LogLevel.Debug ); |
| 174 | + // builder.AddFilter("Microsoft.UI.Xaml.Data", LogLevel.Debug ); |
| 175 | + |
| 176 | + // Binder memory references tracking |
| 177 | + // builder.AddFilter("Uno.UI.DataBinding.BinderReferenceHolder", LogLevel.Debug ); |
| 178 | + |
| 179 | + // DevServer and HotReload related |
| 180 | + // builder.AddFilter("Uno.UI.RemoteControl", LogLevel.Information); |
| 181 | + |
| 182 | + // Debug JS interop |
| 183 | + // builder.AddFilter("Uno.Foundation.WebAssemblyRuntime", LogLevel.Debug ); |
| 184 | + }); |
| 185 | + |
| 186 | + global::Uno.Extensions.LogExtensionPoint.AmbientLoggerFactory = factory; |
| 187 | + |
| 188 | +#if HAS_UNO |
| 189 | + global::Uno.UI.Adapter.Microsoft.Extensions.Logging.LoggingAdapter.Initialize(); |
| 190 | +#endif |
| 191 | +#endif |
| 192 | + } |
| 193 | +} |
0 commit comments