using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Controls; using Avalonia.Platform.Storage; using CommunityToolkit.Mvvm.Input; using Shaker.Models; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Input; using Avalonia; using Avalonia.Data.Converters; namespace ShakerApp.ViewModels { internal class SelectTestDataViewModel:DisplayViewModelBase { public override string OKContent => "Confirm"; public override bool CanResize => false; public override double Height => 320; public override double Width => 620; private string selectedFile = string.Empty; private SelectTestDataViewModel() { Content = typeof(Views.LoadTestDataView); } static SelectTestDataViewModel() { } public string SelectedFile { get => selectedFile; set => SetProperty(ref selectedFile, value); } public override void InitData() { base.InitData(); SelectedFile = string.Empty; } protected override async void Save() { base.Save(); if(!string.IsNullOrEmpty(SelectedFile) && System.IO.File.Exists(SelectedFile)) { await Task.Delay(1); if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) { DataReviewViewModel.Instance.InitData(); DataReviewViewModel.Instance.Title = "DataReview"; BaseDialogWindow window = new BaseDialogWindow(); window.DataContext = DataReviewViewModel.Instance; DeviceMangerViewModel.Instance.CloseWindowAction = () => window?.Close(); DataReviewViewModel.Instance.OpenFile(SelectedFile); await window.ShowDialog(desktop.MainWindow!); } } } public ICommand SelectFileCommand => new RelayCommand(SelectFile); private async void SelectFile(string? p) { if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) { var folder = await TopLevel.GetTopLevel(desktop.MainWindow)!.StorageProvider.OpenFilePickerAsync(new Avalonia.Platform.Storage.FilePickerOpenOptions() { AllowMultiple = false, Title = p == null ? "" : App.Current?.FindResource(p) + "", FileTypeFilter = new FilePickerFileType[] { new FilePickerFileType($"{(p ==null ?"": App.Current?.FindResource(p) + "")}"){ Patterns = new []{"*.tdms" } } } }); if (folder != null && folder.Count > 0) { SelectedFile = folder.First().Path.LocalPath; } } } public static SelectTestDataViewModel Instance { get; } = new SelectTestDataViewModel(); } }