|
|
@@ -1,27 +1,29 @@
|
|
|
using System;
|
|
|
+using System.IO;
|
|
|
using System.Linq;
|
|
|
using System.Reflection;
|
|
|
using Avalonia.Controls;
|
|
|
using Avalonia.Input;
|
|
|
using Avalonia.Markup.Xaml;
|
|
|
+using Avalonia.Platform.Storage;
|
|
|
|
|
|
namespace ControlCatalog.Pages
|
|
|
{
|
|
|
public class DragAndDropPage : UserControl
|
|
|
{
|
|
|
- TextBlock _DropState;
|
|
|
+ private readonly TextBlock _dropState;
|
|
|
private const string CustomFormat = "application/xxx-avalonia-controlcatalog-custom";
|
|
|
public DragAndDropPage()
|
|
|
{
|
|
|
this.InitializeComponent();
|
|
|
- _DropState = this.Get<TextBlock>("DropState");
|
|
|
+ _dropState = this.Get<TextBlock>("DropState");
|
|
|
|
|
|
int textCount = 0;
|
|
|
SetupDnd("Text", d => d.Set(DataFormats.Text,
|
|
|
$"Text was dragged {++textCount} times"), DragDropEffects.Copy | DragDropEffects.Move | DragDropEffects.Link);
|
|
|
|
|
|
SetupDnd("Custom", d => d.Set(CustomFormat, "Test123"), DragDropEffects.Move);
|
|
|
- SetupDnd("Files", d => d.Set(DataFormats.FileNames, new[] { Assembly.GetEntryAssembly()?.GetModules().FirstOrDefault()?.FullyQualifiedName }), DragDropEffects.Copy);
|
|
|
+ SetupDnd("Files", d => d.Set(DataFormats.Files, new[] { Assembly.GetEntryAssembly()?.GetModules().FirstOrDefault()?.FullyQualifiedName }), DragDropEffects.Copy);
|
|
|
}
|
|
|
|
|
|
void SetupDnd(string suffix, Action<DataObject> factory, DragDropEffects effects)
|
|
|
@@ -68,12 +70,12 @@ namespace ControlCatalog.Pages
|
|
|
|
|
|
// Only allow if the dragged data contains text or filenames.
|
|
|
if (!e.Data.Contains(DataFormats.Text)
|
|
|
- && !e.Data.Contains(DataFormats.FileNames)
|
|
|
+ && !e.Data.Contains(DataFormats.Files)
|
|
|
&& !e.Data.Contains(CustomFormat))
|
|
|
e.DragEffects = DragDropEffects.None;
|
|
|
}
|
|
|
|
|
|
- void Drop(object? sender, DragEventArgs e)
|
|
|
+ async void Drop(object? sender, DragEventArgs e)
|
|
|
{
|
|
|
if (e.Source is Control c && c.Name == "MoveTarget")
|
|
|
{
|
|
|
@@ -85,11 +87,41 @@ namespace ControlCatalog.Pages
|
|
|
}
|
|
|
|
|
|
if (e.Data.Contains(DataFormats.Text))
|
|
|
- _DropState.Text = e.Data.GetText();
|
|
|
+ {
|
|
|
+ _dropState.Text = e.Data.GetText();
|
|
|
+ }
|
|
|
+ else if (e.Data.Contains(DataFormats.Files))
|
|
|
+ {
|
|
|
+ var files = e.Data.GetFiles() ?? Array.Empty<IStorageItem>();
|
|
|
+ var contentStr = "";
|
|
|
+
|
|
|
+ foreach (var item in files)
|
|
|
+ {
|
|
|
+ if (item is IStorageFile file)
|
|
|
+ {
|
|
|
+ var content = await DialogsPage.ReadTextFromFile(file, 1000);
|
|
|
+ contentStr += $"File {item.Name}:{Environment.NewLine}{content}{Environment.NewLine}{Environment.NewLine}";
|
|
|
+ }
|
|
|
+ else if (item is IStorageFolder folder)
|
|
|
+ {
|
|
|
+ var items = await folder.GetItemsAsync();
|
|
|
+ contentStr += $"Folder {item.Name}: items {items.Count}{Environment.NewLine}{Environment.NewLine}";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ _dropState.Text = contentStr;
|
|
|
+ }
|
|
|
+#pragma warning disable CS0618 // Type or member is obsolete
|
|
|
else if (e.Data.Contains(DataFormats.FileNames))
|
|
|
- _DropState.Text = string.Join(Environment.NewLine, e.Data.GetFileNames() ?? Array.Empty<string>());
|
|
|
+ {
|
|
|
+ var files = e.Data.GetFileNames();
|
|
|
+ _dropState.Text = string.Join(Environment.NewLine, files ?? Array.Empty<string>());
|
|
|
+ }
|
|
|
+#pragma warning restore CS0618 // Type or member is obsolete
|
|
|
else if (e.Data.Contains(CustomFormat))
|
|
|
- _DropState.Text = "Custom: " + e.Data.Get(CustomFormat);
|
|
|
+ {
|
|
|
+ _dropState.Text = "Custom: " + e.Data.Get(CustomFormat);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
dragMe.PointerPressed += DoDrag;
|