feat: 初始化NewLife Studio项目,完成基础框架与数据管理模块
何炳宏 authored at 2026-05-26 12:09:09
1.42 KiB
NewLife.Studio
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();
    }
}