using Avalonia; using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Platform.Storage; using AvaloniaApplication2.Models; using ReactiveUI; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AvaloniaApplication2.ViewModels { internal class PersonalVM : ViewModelBase { User _user; public User User { get => _user; set => this.RaiseAndSetIfChanged(ref _user, value); } public PersonalVM(User user) { User = user; } public void Vixod() { MainWindowViewModel.Instance.Page = new AuthAndReg(); } public async Task Image() { if (Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desctop || desctop.MainWindow?.StorageProvider is not { } provider) throw new NullReferenceException("провайдер отсутствует"); var file = await provider.OpenFilePickerAsync( new FilePickerOpenOptions() { Title = "Выберите изображение", AllowMultiple = false, FileTypeFilter = [FilePickerFileTypes.All, FilePickerFileTypes.ImageAll] } ); if (file != null) { await using var readStream = await file[0].OpenReadAsync(); byte[] buffer = new byte[readStream.Length]; readStream.ReadAtLeast(buffer, 1); User.Image = buffer; MainWindowViewModel.myconnection.Users.Update(User); MainWindowViewModel.myconnection.SaveChanges(); MainWindowViewModel.Instance.Page = new Personal(User); } } } }