123456789101112131415161718192021222324252627282930313233343536373839404142 |
- using Avalonia.Controls.ApplicationLifetimes;
- using Avalonia.Controls;
- using Avalonia.Platform.Storage;
- using System;
- using System.Threading.Tasks;
- using Avalonia;
- using Avalonia.VisualTree;
- namespace VeloeAvaloniaKemonoPartyApp.Services
- {
- public class StorageService
- {
- protected virtual IStorageProvider Storage => _storage ??= GetTopLevel()?.StorageProvider ?? throw new NullReferenceException("No StorageProvider found.");
- private IStorageProvider? _storage;
- public async Task<string?> GetDocumentsFolderAsync()
- {
- var result = await Storage.TryGetWellKnownFolderAsync(WellKnownFolder.Documents);
- return result.TryGetLocalPath();
- }
- public async Task<string?> GetPicturesFolderAsync()
- {
- var result = await Storage.TryGetWellKnownFolderAsync(WellKnownFolder.Pictures);
- return result.TryGetLocalPath();
- }
- private TopLevel? GetTopLevel()
- {
- if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
- {
- return desktop.MainWindow;
- }
- if (Application.Current?.ApplicationLifetime is ISingleViewApplicationLifetime viewApp)
- {
- var visualRoot = viewApp.MainView?.GetVisualRoot();
- return visualRoot as TopLevel;
- }
- return null;
- }
- }
- }
|