MainWindowViewModel.cs
Код: Выделить всё
using Avalonia.Controls;
using Avalonia.Media.Imaging;
using Avalonia.Platform.Storage;
using ReactiveUI;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Reactive;
using System.Threading.Tasks;
using MemoryLane.ImageHelpers;
using System;
namespace MemoryLane.ViewModels
{
public partial class MainWindowViewModel : ReactiveObject
{
// Observable collection of Bitmaps
private ObservableCollection _images = new();
public ObservableCollection Images
{
get => _images;
set => this.RaiseAndSetIfChanged(ref _images, value);
}
// Commands
public ReactiveCommand HomeButtonCommand { get; }
public ReactiveCommand GalleryButtonCommand { get; }
public ReactiveCommand SettingsButtonCommand { get; }
public ReactiveCommand UploadImageCommand { get; }
public MainWindowViewModel()
{
HomeButtonCommand = ReactiveCommand.Create(() => Debug.WriteLine("Home clicked"));
GalleryButtonCommand = ReactiveCommand.Create(() => Debug.WriteLine("Gallery clicked"));
SettingsButtonCommand = ReactiveCommand.Create(() => Debug.WriteLine("Settings clicked"));
UploadImageCommand = ReactiveCommand.CreateFromTask(LoadSelectedImages);
}
// File picker + bitmap loader (memory-safe)
public async Task LoadSelectedImages()
{
var topLevel = Avalonia.Controls.TopLevel.GetTopLevel(App.Current.ApplicationLifetime is Avalonia.Controls.ApplicationLifetimes.ClassicDesktopStyleApplicationLifetime desktop ? desktop.MainWindow : null);
if (topLevel == null) return;
var files = await topLevel.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions
{
Title = "Select Images",
AllowMultiple = true,
FileTypeFilter = [FilePickerFileTypes.ImageAll]
});
foreach (var file in files)
{
await using var stream = await file.OpenReadAsync();
try {
Images.Add(new Bitmap(files[0].Path.LocalPath));
//Images[0] = bitmap; // Trigger UI update
Debug.WriteLine($"Loaded image: {file.Name}");
Debug.WriteLine("Loaded image: " + file.Name);
}
catch (Exception ex)
{
Debug.WriteLine($"Failed to load image {file.Name}: {ex.Message}");
}
}
}
}
}
Код: Выделить всё
Подробнее здесь: https://stackoverflow.com/questions/798 ... le-dialogs
Мобильная версия