PersonalPageVM.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using Avalonia;
  2. using Avalonia.Controls.ApplicationLifetimes;
  3. using Avalonia.Platform.Storage;
  4. using AvaloniaApplication4.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 AvaloniaApplication4.ViewModels
  12. {
  13. internal class PersonalPageVM : ViewModelBase
  14. {
  15. User _user;
  16. public User User { get => _user; set => this.RaiseAndSetIfChanged(ref _user, value); }
  17. public PersonalPageVM(User user)
  18. {
  19. User = user;
  20. }
  21. public PersonalPageVM()
  22. {
  23. User = new User();
  24. }
  25. public void ToPageMain()
  26. {
  27. MainWindowViewModel.Instance.Uc = new MainPage();
  28. }
  29. public async Task AddOnePhoto()
  30. {
  31. if (Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desctop || desctop.MainWindow?.StorageProvider is not { } provider) throw new NullReferenceException("провайдер отсутствует");
  32. var file = await provider.OpenFilePickerAsync(
  33. new FilePickerOpenOptions()
  34. {
  35. Title = "Выберите изображение",
  36. AllowMultiple = false,
  37. FileTypeFilter = [FilePickerFileTypes.All, FilePickerFileTypes.ImageAll]
  38. }
  39. );
  40. if (file != null)
  41. {
  42. await using var readStream = await file[0].OpenReadAsync();
  43. byte[] buffer = new byte[readStream.Length];
  44. readStream.ReadAtLeast(buffer, 1);
  45. User.Image = buffer;
  46. MainWindowViewModel.myConnection.SaveChanges();
  47. MainWindowViewModel.Instance.Uc = new PesonalPage(_user);
  48. }
  49. }
  50. }
  51. }