|
@@ -1,4 +1,5 @@
|
|
|
-using AvaloniaApplication2.Models;
|
|
|
+using Avalonia.Controls.ApplicationLifetimes;
|
|
|
+using AvaloniaApplication2.Models;
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
using System;
|
|
@@ -6,6 +7,10 @@ using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
using System.Text;
|
|
|
using System.Threading.Tasks;
|
|
|
+using Avalonia;
|
|
|
+using Avalonia.Platform.Storage;
|
|
|
+using Avalonia.Media.Imaging;
|
|
|
+using System.IO;
|
|
|
|
|
|
namespace AvaloniaApplication2.ViewModels
|
|
|
{
|
|
@@ -13,10 +18,13 @@ namespace AvaloniaApplication2.ViewModels
|
|
|
{
|
|
|
[ObservableProperty] User editedUser;
|
|
|
[ObservableProperty] List<Gender> gendersList;
|
|
|
+ [ObservableProperty] Bitmap imageUser;
|
|
|
+
|
|
|
public Page2ViewModel(int id)
|
|
|
{
|
|
|
GendersList = db.Genders.ToList();
|
|
|
- editedUser = db.Users.Include(x=>x.IdGenderNavigation).FirstOrDefault(x=>x.IdUser == id);
|
|
|
+ editedUser = db.Users.Include(x=>x.IdGenderNavigation).FirstOrDefault(x=>x.IdUser == id);
|
|
|
+ imageUser = EditedUser.Image != null ? new Bitmap(new MemoryStream(editedUser.Image)) : new Bitmap("Assets/заглушка.jpg");
|
|
|
}
|
|
|
public DateTimeOffset DateTimeOffset
|
|
|
{
|
|
@@ -27,5 +35,29 @@ namespace AvaloniaApplication2.ViewModels
|
|
|
public void GoBack() => MainWindowViewModel.Self.Page = new Page1();
|
|
|
|
|
|
public void Save() => db.SaveChanges();
|
|
|
+ public async Task Image()
|
|
|
+ {
|
|
|
+ if (Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop ||
|
|
|
+ desktop.MainWindow?.StorageProvider is not { } provider)
|
|
|
+ throw new NullReferenceException("Missing StorageProvider instance.");
|
|
|
+
|
|
|
+ //инициализация в явном виде. менее трушный путь
|
|
|
+ //IClassicDesktopStyleApplicationLifetime desktop = (IClassicDesktopStyleApplicationLifetime)Application.Current?.ApplicationLifetime;
|
|
|
+ // IStorageProvider provider = desktop.MainWindow?.StorageProvider;
|
|
|
+ var files = await provider.OpenFilePickerAsync(new FilePickerOpenOptions()
|
|
|
+ {
|
|
|
+ Title = "Выберите файл с изображением пользователя",
|
|
|
+ AllowMultiple = false
|
|
|
+ });
|
|
|
+ await using var readStream = await files[0].OpenReadAsync();
|
|
|
+ byte[] buffer = new byte[10000000];
|
|
|
+ var bytes = readStream.ReadAtLeast(buffer, 1);
|
|
|
+ Array.Resize(ref buffer,bytes);
|
|
|
+ editedUser.Image = buffer;
|
|
|
+ ImageUser = new Bitmap(new MemoryStream(editedUser.Image));
|
|
|
+ db.SaveChanges();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
}
|