using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Platform.Storage;
using NewLife.Studio.Modules.DataStudio.ViewModels;
namespace NewLife.Studio.Modules.DataStudio.Views;
public partial class ResultGridView : UserControl
{
private ResultGridViewModel? _vm;
public ResultGridView()
{
InitializeComponent();
ExportCsvButton.Click += async (_, _) =>
{
if (_vm == null || !_vm.HasRows) return;
var filePath = await GetSavePathAsync("查询结果.csv");
if (filePath != null)
await _vm.ExportCsvAsync(filePath);
};
ExportJsonButton.Click += async (_, _) =>
{
if (_vm == null || !_vm.HasRows) return;
var filePath = await GetSavePathAsync("查询结果.json");
if (filePath != null)
await _vm.ExportJsonAsync(filePath);
};
DataContextChanged += (_, _) =>
{
_vm = DataContext as ResultGridViewModel;
};
}
private async Task<string?> GetSavePathAsync(string defaultName)
{
var topLevel = TopLevel.GetTopLevel(this);
if (topLevel == null) return null;
var file = await topLevel.StorageProvider.SaveFilePickerAsync(new FilePickerSaveOptions
{
Title = "导出文件",
SuggestedFileName = defaultName
});
return file?.TryGetLocalPath();
}
}
|