using Avalonia; using Avalonia.Controls; using Avalonia.Controls.Primitives; using Avalonia.Platform.Storage; namespace TheXamlGuy.UI.Avalonia.Controls { public class FilePickerFilePickedEventArgs : EventArgs { } public class FilePicker : TemplatedControl { public static readonly StyledProperty AllowMultipleProperty = AvaloniaProperty.Register(nameof(AllowMultiple)); public static readonly StyledProperty?> FileTypeFilterProperty = AvaloniaProperty.Register?>(nameof(FileTypeFilter)); public static readonly StyledProperty SuggestedStartLocationProperty = AvaloniaProperty.Register(nameof(SuggestedStartLocation)); public static readonly StyledProperty TitleProperty = AvaloniaProperty.Register(nameof(Title)); public bool AllowMultiple { get { return GetValue(AllowMultipleProperty); } set { SetValue(AllowMultipleProperty, value); } } public IReadOnlyList? FileTypeFilter { get { return GetValue(FileTypeFilterProperty); } set { SetValue(FileTypeFilterProperty, value); } } public IStorageFolder? SuggestedStartLocation { get { return GetValue(SuggestedStartLocationProperty); } set { SetValue(SuggestedStartLocationProperty, value); } } public string? Title { get { return GetValue(TitleProperty); } set { SetValue(TitleProperty, value); } } public event TypedEventHandler? FilePicked; public async void Open() { if (VisualRoot is Window window) { IReadOnlyList files = await window.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions { AllowMultiple = AllowMultiple, FileTypeFilter = FileTypeFilter, SuggestedStartLocation = SuggestedStartLocation, Title = Title }); } } } }