-
Notifications
You must be signed in to change notification settings - Fork 2k
[Catalyst,Windows] Allow drag item from outside the app #21684
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
21dc29d
1c7f2a1
af0697d
3b6816d
2fc491e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}"); | ||
| } | ||
|
|
||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.