using System; using System.Threading.Tasks; using H.NotifyIcon; using Microsoft.Extensions.DependencyInjection; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Input; using Windows.Storage; using Wino.Core; using Wino.Core.Domain.Interfaces; using Wino.Core.Services; using Wino.Core.UWP.Services; using Wino.Services; namespace Wino.Server.NET8 { public partial class ServerApp : Application { public new static ServerApp Current => (ServerApp)Application.Current; private const string WinoServerAppName = "Wino.Server"; public TaskbarIcon TrayIcon { get; private set; } public bool HandleClosedEvents { get; set; } = true; public IServiceProvider Services { get; private set; } public ServerApp() { InitializeComponent(); } private IServiceProvider ConfigureServices() { var services = new ServiceCollection(); services.AddTransient(); services.AddTransient(); services.RegisterCoreServices(); // Below services belongs to UWP.Core package and some APIs are not available for WPF. // We register them here to avoid compilation errors. services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); return services.BuildServiceProvider(); } protected override async void OnLaunched(LaunchActivatedEventArgs args) { Services = ConfigureServices(); await InitializeNewServerAsync(); InitializeTrayIcon(); } private async Task InitializeNewServerAsync() { // TODO: Error handling. var databaseService = Services.GetService(); var applicationFolderConfiguration = Services.GetService(); applicationFolderConfiguration.ApplicationDataFolderPath = ApplicationData.Current.LocalFolder.Path; applicationFolderConfiguration.PublisherSharedFolderPath = ApplicationData.Current.GetPublisherCacheFolder(ApplicationConfiguration.SharedFolderName).Path; await databaseService.InitializeAsync(); var serverViewModel = Services.GetRequiredService(); await serverViewModel.InitializeAsync(); return serverViewModel; } private void InitializeTrayIcon() { var viewModel = Services.GetService(); var launchCommand = (XamlUICommand)Resources["LaunchCommand"]; launchCommand.Command = viewModel.LaunchWinoCommand; var exitApplicationCommand = (XamlUICommand)Resources["TerminateCommand"]; exitApplicationCommand.Command = viewModel.ExitApplicationCommand; TrayIcon = (TaskbarIcon)Resources["TrayIcon"]; TrayIcon.ForceCreate(); } } }