Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Controls/samples/Controls.Sample.Sandbox/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ public static class MauiProgram
public static MauiApp CreateMauiApp() =>
MauiApp
.CreateBuilder()
#if __ANDROID__ || __IOS__
.UseMauiMaps()
#endif
Comment thread
mattleibow marked this conversation as resolved.
.UseMauiApp<App>()
.Build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8" ?>
<views:BasePage
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Controls.Sample.Pages.DropFileToMauiApp"
xmlns:views="clr-namespace:Maui.Controls.Sample.Pages.Base">
<views:BasePage.Resources>
</views:BasePage.Resources>
<Grid x:Name="myLayout">
<Grid.GestureRecognizers>
<DropGestureRecognizer AllowDrop="True" DragOver="DropGestureDragOver" Drop="DropGestureDrop" DragLeave="DropGestureDragLeave"/>
</Grid.GestureRecognizers>
<Label x:Name="lblPath" HorizontalOptions="Center" VerticalOptions="Center" Text="Drag a file here like a .txt" />
</Grid>
</views:BasePage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Maui;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Graphics;

#if WINDOWS
using Windows.ApplicationModel.DataTransfer;
using Windows.Storage;
#endif

#if IOS || MACCATALYST
using UIKit;
using Foundation;
#endif

namespace Maui.Controls.Sample.Pages
{
public partial class DropFileToMauiApp
{

public DropFileToMauiApp()
{
InitializeComponent();
}

void DropGestureDragLeave(object? sender, DragEventArgs e)
{

}

async void DropGestureDrop(object? sender, DropEventArgs e)
{
var filePaths = new List<string>();

#if WINDOWS
if (e.PlatformArgs is not null && e.PlatformArgs.DragEventArgs.DataView.Contains(StandardDataFormats.StorageItems))
{
var items = await e.PlatformArgs.DragEventArgs.DataView.GetStorageItemsAsync();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doe this work in unpackaged? Not sure how all the storage APIs work.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me test and see..

if (items.Any())
{
foreach (var item in items)
{
if (item is StorageFile file)
{
filePaths.Add(item.Path);
}
}

}
}
#elif MACCATALYST
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works with iOS, so you can enable it here.


var session = e.PlatformArgs?.DropSession;
if (session == null)
{
return;
}
foreach (UIDragItem item in session.Items)
{
var result = await LoadItemAsync(item.ItemProvider, item.ItemProvider.RegisteredTypeIdentifiers.ToList());
if (result is not null)
{
filePaths.Add(result.FileUrl?.Path!);
}
}
foreach (var item in filePaths)
{
Debug.WriteLine($"Path: {item}");
}

static async Task<LoadInPlaceResult?> LoadItemAsync(NSItemProvider itemProvider, List<string> typeIdentifiers)
{
if (typeIdentifiers is null || typeIdentifiers.Count == 0)
{
return null;
}

var typeIdent = typeIdentifiers.First();

if (itemProvider.HasItemConformingTo(typeIdent))
{
return await itemProvider.LoadInPlaceFileRepresentationAsync(typeIdent);
}

typeIdentifiers.Remove(typeIdent);

return await LoadItemAsync(itemProvider, typeIdentifiers);
}
#else
await Task.CompletedTask;
#endif

lblPath.Text = filePaths.FirstOrDefault();
}

void DropGestureDragOver(object? sender, DragEventArgs e)
{
Debug.WriteLine($"Dragging {e.Data?.Text}, {e.Data?.Image}");
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ protected override IEnumerable<SectionModel> CreateItems() => new[]
{
new SectionModel(typeof(DragAndDropBetweenLayouts), "Drag And Drop",
"Drag and Drop Views."),
new SectionModel(typeof(DropFileToMauiApp), "Drag and Drop file from OS",
"Drop File to App"),
new SectionModel(typeof(PanGestureGallery), "Pan Gesture",
"Pan Gesture."),
new SectionModel(typeof(PinchGestureTestPage), "Pinch Gesture",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,7 @@ void HandleDrop(object sender, Microsoft.UI.Xaml.DragEventArgs e)
element = ve;
}

if (datapackage is null)
return;

var args = new DropEventArgs(datapackage.View, (relativeTo) => GetPosition(relativeTo, e), new PlatformDropEventArgs(sender as UIElement, e));
var args = new DropEventArgs(datapackage?.View, (relativeTo) => GetPosition(relativeTo, e), new PlatformDropEventArgs(sender as UIElement, e));
SendEventArgs<DropGestureRecognizer>(async rec =>
{
if (!rec.AllowDrop)
Expand Down
13 changes: 6 additions & 7 deletions src/Controls/src/Core/Platform/iOS/DragAndDropDelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,14 @@ public UIDropProposal SessionDidUpdate(UIDropInteraction interaction, IUIDropSes
{
UIDropOperation operation = UIDropOperation.Cancel;

if (session.LocalDragSession == null)
return new UIDropProposal(operation);

DataPackage package = null;

if (session.LocalDragSession.Items.Length > 0 &&
session.LocalDragSession.Items[0].LocalObject is CustomLocalStateData cdi)
if (session.LocalDragSession != null)
{
package = cdi.DataPackage;
if (session.LocalDragSession.Items.Length > 0 &&
session.LocalDragSession.Items[0].LocalObject is CustomLocalStateData cdi)
{
package = cdi.DataPackage;
}
}

var platformArgs = new PlatformDragEventArgs(_viewHandler.PlatformView, interaction, session);
Expand Down