detect/detect.gui/Views/WebViewService.cs

88 lines
3.1 KiB
C#
Raw Normal View History

2025-04-23 15:55:51 +08:00
using System.Threading.Tasks;
2024-11-21 16:28:36 +08:00
using Avalonia.Controls;
2024-11-26 15:44:31 +08:00
using Avalonia.Controls.Notifications;
2024-11-27 15:20:42 +08:00
using Avalonia.Threading;
2024-11-26 15:44:31 +08:00
using detect.gui.VWMS;
2024-11-27 15:20:42 +08:00
using detect.gui.VWS;
2024-11-26 15:44:31 +08:00
using Splat;
2024-11-27 15:20:42 +08:00
using WebViewControl;
2024-11-21 16:28:36 +08:00
namespace detect.gui.Views;
2024-12-27 13:49:37 +08:00
public class WebViewService(MainWindow? w)
2024-11-21 16:28:36 +08:00
{
2024-12-27 13:49:37 +08:00
public MainWindow? Self { get; set; } = w;
2024-11-21 16:28:36 +08:00
2025-04-23 15:55:51 +08:00
// [Obsolete("过时的")]
// public async Task<string?> ImportExcel()
// {
// var dialog = new OpenFileDialog
// {
// Title = "打开文件",
// AllowMultiple = false,
// Filters =
// [
// new FileDialogFilter { Name = "Excel Files", Extensions = { "*.xls; *.xlsx" } }
// ]
// };
// var result = await dialog.ShowAsync(Self!);
// if (result is not { Length: 1 }) return null;
// try
// {
// return ConvertExcelToJson(result[0]);
// }
// catch
// {
// return null;
// }
// }
2024-11-21 16:28:36 +08:00
2025-04-23 15:55:51 +08:00
// public static string ConvertExcelToJson(string excelFilePath)
// {
// ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
// var fileInfo = new FileInfo(excelFilePath);
// using var package = new ExcelPackage(fileInfo);
// var worksheet = package.Workbook.Worksheets[0];
// var rowCount = worksheet.Dimension.Rows;
// var colCount = worksheet.Dimension.Columns;
//
// var data = new List<Dictionary<string, object>>();
// var headers = Enumerable.Range(1, colCount)
// .Select(col => worksheet.Cells[1, col].Text)
// .ToList();
// for (var row = 2; row <= rowCount; row++)
// {
// var rowData = new Dictionary<string, object>();
// for (var col = 1; col <= colCount; col++)
// {
// rowData[headers[col - 1]] = worksheet.Cells[row, col].Text;
// }
//
// data.Add(rowData);
// }
// return JsonSerializer.Serialize(data, new JsonSerializerOptions { WriteIndented = true, Encoder = JavaScriptEncoder.Create(UnicodeRanges.All) });
// }
2024-11-26 15:44:31 +08:00
public Task SetIsLoading(bool isLoading)
{
Locator.Current.GetService<MainWindowModel>()!.IsLoading = isLoading;
2024-11-27 15:20:42 +08:00
Dispatcher.UIThread.InvokeAsync(() =>
{
var webView = Locator.Current.GetService<MainWindow>()!.Get<WebView>("WebView");
if (!webView.IsVisible) webView.IsVisible = Locator.Current.GetService<MainWindowModel>()!.CurrentUser != null;
});
return Task.FromResult(default(object));
2024-11-26 15:44:31 +08:00
}
public Task SetMessage(string messageText, string messageType)
{
Locator.Current.GetService<MainWindowModel>()!.Message = messageType switch
{
"success" => new MessageItem(messageText, NotificationType.Success),
"information" => new MessageItem(messageText, NotificationType.Information),
"error" => new MessageItem(messageText, NotificationType.Error),
_ => new MessageItem(messageText, NotificationType.Warning)
};
return Task.CompletedTask;
}
2024-11-21 16:28:36 +08:00
}