Add drag/drop handling

This commit is contained in:
dan_clark@outlook.com
2022-03-23 21:19:23 +00:00
parent 2ac0e3ed26
commit 263704a772
22 changed files with 189 additions and 63 deletions
@@ -0,0 +1,9 @@
using Windows.UI.Xaml;
namespace TheXamlGuy.TaskbarGroup.Flyout.Foundation
{
public record class Drop<TTarget>(DragEventArgs DropEventArgs) where TTarget : UIElement
{
public TTarget Target { get; }
}
}
@@ -1,55 +1,43 @@
using System;
using System.Collections.Generic;
using Microsoft.Xaml.Interactivity;
using System;
using TheXamlGuy.TaskbarGroup.Core;
using Windows.ApplicationModel.DataTransfer;
using Windows.Storage;
using Windows.UI.Xaml;
namespace TheXamlGuy.TaskbarGroup.Flyout.Foundation
{
public class DropTarget
public class DropTarget : Behavior<FrameworkElement>
{
public void Initialize(UIElement target)
public static readonly DependencyProperty MediatorProperty =
DependencyProperty.Register(nameof(Mediator),
typeof(IMediator), typeof(DropTarget),
new PropertyMetadata(null));
public IMediator Mediator
{
target.DragOver += OnDragOver;
target.Drop += OnDrop;
get => (IMediator)GetValue(MediatorProperty);
set => SetValue(MediatorProperty, value);
}
private async void OnDrop(object sender, DragEventArgs args)
protected override void OnAttached()
{
if (args.DataView.Contains(StandardDataFormats.StorageItems))
AssociatedObject.DragOver -= OnDragOver;
AssociatedObject.Drop -= OnDrop;
AssociatedObject.DragOver += OnDragOver;
AssociatedObject.Drop += OnDrop;
base.OnAttached();
}
private void OnDrop(object sender, DragEventArgs args)
{
if (Mediator is not null)
{
var items = await args.DataView.GetStorageItemsAsync();
if (items.Count > 0)
{
foreach (var storageItem in items)
{
if (storageItem is StorageFile storageFile)
{
if (storageFile.Path is { Length: > 0 })
{
var dropMessageType = typeof(Drop<>).MakeGenericType(sender.GetType());
var dropMessage = Activator.CreateInstance(dropMessageType, args);
}
else
{
var properties = await storageFile.Properties.RetrievePropertiesAsync(new List<string>
{
"System.AppUserModel.ID"
});
var appUserModelId = properties["System.AppUserModel.ID"];
if (appUserModelId is not null)
{
}
}
}
if (storageItem is StorageFolder storageFolder)
{
}
}
}
Mediator.HandleAsync(dropMessage);
}
}
@@ -0,0 +1,10 @@
using TheXamlGuy.TaskbarGroup.Core;
using Windows.UI.Xaml;
namespace TheXamlGuy.TaskbarGroup.Flyout.Foundation
{
public interface IDropHandler<TTarget> : IAsyncMessageHandler<Drop<TTarget>> where TTarget : UIElement
{
}
}
@@ -12,7 +12,8 @@ namespace TheXamlGuy.TaskbarGroup.Flyout.Foundation
return serviceCollection
.AddSingleton<TemplateSelector>()
.AddSingleton<IDataTemplateCollection>(new DataTemplateCollection(new Dictionary<Type, Type>()))
.AddSingleton<DataTemplateFactory>();
.AddSingleton<DataTemplateFactory>()
.AddSingleton<DropTarget>();
}
}
}
@@ -122,9 +122,11 @@
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
</PropertyGroup>
<ItemGroup>
<Compile Include="DataTemplateFactory.cs" />
<Compile Include="DropTarget.cs" />
<Compile Include="DataTemplateFactory.cs" />
<Compile Include="Drop.cs" />
<Compile Include="IDataTemplateFactory.cs" />
<Compile Include="IDropHandler.cs" />
<Compile Include="IServiceCollectionExtensions.cs" />
<Compile Include="IWindowPrivate.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
@@ -142,6 +144,9 @@
<PackageReference Include="Microsoft.UI.Xaml">
<Version>2.8.0-prerelease.220118001</Version>
</PackageReference>
<PackageReference Include="Microsoft.Xaml.Behaviors.Uwp.Managed">
<Version>2.0.1</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\TheXamlGuy.TaskbarGroup.Core\TheXamlGuy.TaskbarGroup.Core.csproj">