From 436d8a5d7cc3e6ac103849eb10867600b2e9b619 Mon Sep 17 00:00:00 2001 From: wanggang <76527413@qq.com> Date: Mon, 13 Dec 2021 12:46:02 +0800 Subject: [PATCH] 0.1.2 --- src/PhotoCollector/MainForm.cs | 11 +- src/PhotoCollector/PhotoCollector.csproj | 9 +- src/PhotoCollector/Program.cs | 120 +++++----- src/PhotoCollector/Startup.cs | 33 +++ src/PhotoCollector/wwwroot/index.html | 265 +++++++++++++++++++++++ 5 files changed, 383 insertions(+), 55 deletions(-) create mode 100644 src/PhotoCollector/Startup.cs create mode 100644 src/PhotoCollector/wwwroot/index.html diff --git a/src/PhotoCollector/MainForm.cs b/src/PhotoCollector/MainForm.cs index 2d81978..32080c5 100644 --- a/src/PhotoCollector/MainForm.cs +++ b/src/PhotoCollector/MainForm.cs @@ -35,7 +35,7 @@ namespace PhotoCollector var userDataFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), Application.ProductName); var env = await CoreWebView2Environment.CreateAsync(browserExecutableFolder, userDataFolder); this.webView21.NavigationCompleted += WebView21_NavigationCompleted; - this.webView21.CoreWebView2InitializationCompleted += WebView21_CoreWebView2InitializationCompleted; + this.webView21.CoreWebView2InitializationCompleted += WebView21_CoreWebView2InitializationCompletedAsync; await this.webView21.EnsureCoreWebView2Async(env); } @@ -47,7 +47,7 @@ namespace PhotoCollector } } - private void WebView21_CoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e) + private async void WebView21_CoreWebView2InitializationCompletedAsync(object sender, CoreWebView2InitializationCompletedEventArgs e) { var webView = sender as WebView2; if (webView != null) @@ -64,10 +64,13 @@ namespace PhotoCollector webView.CoreWebView2.WebMessageReceived += CoreWebView2_WebMessageReceived; AddEventHandler(); webView.CoreWebView2.AddHostObjectToScript("dotnet", new WebView2Interop()); - webView.CoreWebView2.Navigate(AppDbContext.getConfig("url")); + await Program.WebHostStartAsync(); } } - + public void Navigate(string url) + { + this.webView21.CoreWebView2.Navigate(url); + } private void AddEventHandler() { WebView2Interop.FuncList.Add("selectPath", o => diff --git a/src/PhotoCollector/PhotoCollector.csproj b/src/PhotoCollector/PhotoCollector.csproj index 2437476..3fd0a62 100644 --- a/src/PhotoCollector/PhotoCollector.csproj +++ b/src/PhotoCollector/PhotoCollector.csproj @@ -10,7 +10,7 @@ full True zh-Hans - 0.1.1 + 0.1.2 @@ -24,8 +24,10 @@ + - + + @@ -33,6 +35,9 @@ PreserveNewest + + PreserveNewest + diff --git a/src/PhotoCollector/Program.cs b/src/PhotoCollector/Program.cs index fce65c1..48fd307 100644 --- a/src/PhotoCollector/Program.cs +++ b/src/PhotoCollector/Program.cs @@ -1,67 +1,89 @@ +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; using PhotoCollector.Data; using Serilog; using Serilog.Core; using Serilog.Events; using System.Diagnostics; -namespace PhotoCollector +namespace PhotoCollector; + +internal static class Program { - internal static class Program + public static MainForm MainForm; + public static IHost WebHost; + + [STAThread] + private static void Main() { - /// - /// The main entry point for the application. - /// - [STAThread] - private static void Main() + using var mutex = new Mutex(true, Process.GetCurrentProcess().ProcessName, out var result); + if (!result) { - using var mutex = new Mutex(true, Process.GetCurrentProcess().ProcessName, out var result); - if (!result) - { - MessageBox.Show($"当前程序只允许运行一个实例"); - return; - } - Log.Logger = new LoggerConfiguration() - .MinimumLevel.Override("Microsoft", LogEventLevel.Information) - .MinimumLevel.Debug() - .WriteTo.File(Path.Combine(AppContext.BaseDirectory, "logs", "app.log.txt"), shared: true, rollOnFileSizeLimit: true, fileSizeLimitBytes: 1024 * 1024 * 1024, rollingInterval: RollingInterval.Day, retainedFileCountLimit: 7, levelSwitch: new LoggingLevelSwitch(LogEventLevel.Information)) - .WriteTo.File(Path.Combine(AppContext.BaseDirectory, "logs", "app.err.txt"), shared: true, rollOnFileSizeLimit: true, fileSizeLimitBytes: 1024 * 1024 * 1024, rollingInterval: RollingInterval.Day, retainedFileCountLimit: 7, levelSwitch: new LoggingLevelSwitch(LogEventLevel.Error)) - .CreateLogger(); - try - { - Log.Information("application start"); + MessageBox.Show($"当前程序只允许运行一个实例"); + return; + } + Log.Logger = new LoggerConfiguration() + .MinimumLevel.Override("Microsoft", LogEventLevel.Information) + .MinimumLevel.Debug() + .WriteTo.File(Path.Combine(AppContext.BaseDirectory, "logs", "app.log.txt"), shared: true, rollOnFileSizeLimit: true, fileSizeLimitBytes: 1024 * 1024 * 1024, rollingInterval: RollingInterval.Day, retainedFileCountLimit: 7, levelSwitch: new LoggingLevelSwitch(LogEventLevel.Information)) + .WriteTo.File(Path.Combine(AppContext.BaseDirectory, "logs", "app.err.txt"), shared: true, rollOnFileSizeLimit: true, fileSizeLimitBytes: 1024 * 1024 * 1024, rollingInterval: RollingInterval.Day, retainedFileCountLimit: 7, levelSwitch: new LoggingLevelSwitch(LogEventLevel.Error)) + .CreateLogger(); + try + { + Log.Information("application start"); - System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); - AppDbContext.Init(); - Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); - Application.ThreadException += new ThreadExceptionEventHandler((s, e) => - { - Log.Error(e.Exception.ToString()); + System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); + AppDbContext.Init(); + Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); + Application.ThreadException += new ThreadExceptionEventHandler((s, e) => + { + Log.Error(e.Exception.ToString()); #if DEBUG - MessageBox.Show(e.Exception.ToString()); + MessageBox.Show(e.Exception.ToString()); #endif - }); - AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler((s, e) => - { - Log.Error(e.ExceptionObject.ToString()); + }); + AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler((s, e) => + { + Log.Error(e.ExceptionObject.ToString()); #if DEBUG - MessageBox.Show(e.ExceptionObject.ToString()); + MessageBox.Show(e.ExceptionObject.ToString()); #endif - }); - ApplicationConfiguration.Initialize(); - Application.Run(new MainForm()); - Log.Information("application exit"); - } - catch (Exception ex) - { - Log.Error(ex.ToString()); + }); + ApplicationConfiguration.Initialize(); + MainForm = new MainForm(); + Application.Run(MainForm); + Log.Information("application exit"); + } + catch (Exception ex) + { + Log.Error(ex.ToString()); #if DEBUG - MessageBox.Show(ex.Message); + MessageBox.Show(ex.Message); #endif - } - finally - { - Log.CloseAndFlush(); - } } + finally + { + Log.CloseAndFlush(); + } + } + + public static async Task WebHostStartAsync() + { + WebHost = Host.CreateDefaultBuilder() + .ConfigureHostConfiguration(configHost => + { + configHost.SetBasePath(AppContext.BaseDirectory); + }) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.CaptureStartupErrors(true); + webBuilder.UseSetting(WebHostDefaults.DetailedErrorsKey, "true"); + webBuilder.UseContentRoot(AppContext.BaseDirectory); + webBuilder.UseStartup(); + webBuilder.UseUrls("http://localhost:8440"); + }) + .Build(); + await WebHost.StartAsync(); } -} \ No newline at end of file +} diff --git a/src/PhotoCollector/Startup.cs b/src/PhotoCollector/Startup.cs new file mode 100644 index 0000000..f84599e --- /dev/null +++ b/src/PhotoCollector/Startup.cs @@ -0,0 +1,33 @@ +锘 +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting.Server.Features; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using System.Text.Encodings.Web; +using System.Text.Unicode; + +namespace PhotoCollector; +public class Startup +{ + public void ConfigureServices(IServiceCollection services) + { + services.AddSingleton(HtmlEncoder.Create(UnicodeRanges.All)); + } + + public void Configure(IApplicationBuilder app, IHostApplicationLifetime lifetime) + { + lifetime.ApplicationStarted.Register(() => + { + var url = app.ServerFeatures.Get()?.Addresses.FirstOrDefault(); + Program.MainForm.BeginInvoke(new EventHandler(delegate + { + Task.Delay(1000); + Program.MainForm.Navigate(url); + })); + }); + app.UseDeveloperExceptionPage(); + app.UseStaticFiles(); + app.UseDefaultFiles(); + app.UseFileServer(); + } +} \ No newline at end of file diff --git a/src/PhotoCollector/wwwroot/index.html b/src/PhotoCollector/wwwroot/index.html new file mode 100644 index 0000000..b829dd7 --- /dev/null +++ b/src/PhotoCollector/wwwroot/index.html @@ -0,0 +1,265 @@ +锘 + + + + + 妯℃嫙鎺ュ彛 + + + + + + + + + + + + +
+
+ +
+
+
+
+
+ + +
+ + + + + \ No newline at end of file