PersonalVM.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using Avalonia;
  2. using Avalonia.Controls.ApplicationLifetimes;
  3. using Avalonia.Platform.Storage;
  4. using AvaloniaApplication2.Models;
  5. using ReactiveUI;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace AvaloniaApplication2.ViewModels
  12. {
  13. internal class PersonalVM : ViewModelBase
  14. {
  15. User _user;
  16. public User User { get => _user; set => this.RaiseAndSetIfChanged(ref _user, value); }
  17. public PersonalVM(User user)
  18. {
  19. User = user;
  20. }
  21. public void Vixod()
  22. {
  23. MainWindowViewModel.Instance.Page = new AuthAndReg();
  24. }
  25. public async Task Image()
  26. {
  27. if (Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desctop || desctop.MainWindow?.StorageProvider is not { } provider) throw new NullReferenceException("провайдер отсутствует");
  28. var file = await provider.OpenFilePickerAsync(
  29. new FilePickerOpenOptions()
  30. {
  31. Title = "Выберите изображение",
  32. AllowMultiple = false,
  33. FileTypeFilter = [FilePickerFileTypes.All, FilePickerFileTypes.ImageAll]
  34. }
  35. );
  36. if (file != null)
  37. {
  38. await using var readStream = await file[0].OpenReadAsync();
  39. byte[] buffer = new byte[readStream.Length];
  40. readStream.ReadAtLeast(buffer, 1);
  41. User.Image = buffer;
  42. MainWindowViewModel.myconnection.Users.Update(User);
  43. MainWindowViewModel.myconnection.SaveChanges();
  44. MainWindowViewModel.Instance.Page = new Personal(User);
  45. }
  46. }
  47. }
  48. }