Skip to content

Commit bf56fbb

Browse files
authored
Merge branch 'dotnet:main' into lytico/merge/main240321
2 parents 3098b37 + 01e72e2 commit bf56fbb

File tree

7 files changed

+145
-11
lines changed

7 files changed

+145
-11
lines changed

.github/policies/resourceManagement.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,5 +579,15 @@ configuration:
579579
- removeLabel:
580580
label: s/try-latest-version
581581
description: Remove 's/try-latest-version' when new reply from author comes in
582+
- if:
583+
- payloadType: Issues
584+
- activitySenderHasPermission:
585+
permission: Write
586+
- isAction:
587+
action: Opened
588+
then:
589+
- addLabel:
590+
label: s/triaged
591+
description: Add 's/triaged' label to issues opened by the core team, we assume these issues do not need triaging
582592
onFailure:
583593
onSuccess:

src/Controls/samples/Controls.Sample.Sandbox/MauiProgram.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ public static class MauiProgram
1010
public static MauiApp CreateMauiApp() =>
1111
MauiApp
1212
.CreateBuilder()
13+
#if __ANDROID__ || __IOS__
1314
.UseMauiMaps()
15+
#endif
1416
.UseMauiApp<App>()
1517
.Build();
1618
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<views:BasePage
3+
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
4+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
5+
x:Class="Maui.Controls.Sample.Pages.DropFileToMauiApp"
6+
xmlns:views="clr-namespace:Maui.Controls.Sample.Pages.Base">
7+
<views:BasePage.Resources>
8+
</views:BasePage.Resources>
9+
<Grid x:Name="myLayout">
10+
<Grid.GestureRecognizers>
11+
<DropGestureRecognizer AllowDrop="True" DragOver="DropGestureDragOver" Drop="DropGestureDrop" DragLeave="DropGestureDragLeave"/>
12+
</Grid.GestureRecognizers>
13+
<Label x:Name="lblPath" HorizontalOptions="Center" VerticalOptions="Center" Text="Drag a file here like a .txt" />
14+
</Grid>
15+
</views:BasePage>
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
4+
using System.Diagnostics;
5+
using System.IO;
6+
using System.Linq;
7+
using System.Threading.Tasks;
8+
using Microsoft.Extensions.DependencyInjection;
9+
using Microsoft.Maui;
10+
using Microsoft.Maui.Controls;
11+
using Microsoft.Maui.Graphics;
12+
13+
#if WINDOWS
14+
using Windows.ApplicationModel.DataTransfer;
15+
using Windows.Storage;
16+
#endif
17+
18+
#if IOS || MACCATALYST
19+
using UIKit;
20+
using Foundation;
21+
#endif
22+
23+
namespace Maui.Controls.Sample.Pages
24+
{
25+
public partial class DropFileToMauiApp
26+
{
27+
28+
public DropFileToMauiApp()
29+
{
30+
InitializeComponent();
31+
}
32+
33+
void DropGestureDragLeave(object? sender, DragEventArgs e)
34+
{
35+
36+
}
37+
38+
async void DropGestureDrop(object? sender, DropEventArgs e)
39+
{
40+
var filePaths = new List<string>();
41+
42+
#if WINDOWS
43+
if (e.PlatformArgs is not null && e.PlatformArgs.DragEventArgs.DataView.Contains(StandardDataFormats.StorageItems))
44+
{
45+
var items = await e.PlatformArgs.DragEventArgs.DataView.GetStorageItemsAsync();
46+
if (items.Any())
47+
{
48+
foreach (var item in items)
49+
{
50+
if (item is StorageFile file)
51+
{
52+
filePaths.Add(item.Path);
53+
}
54+
}
55+
56+
}
57+
}
58+
#elif MACCATALYST
59+
60+
var session = e.PlatformArgs?.DropSession;
61+
if (session == null)
62+
{
63+
return;
64+
}
65+
foreach (UIDragItem item in session.Items)
66+
{
67+
var result = await LoadItemAsync(item.ItemProvider, item.ItemProvider.RegisteredTypeIdentifiers.ToList());
68+
if (result is not null)
69+
{
70+
filePaths.Add(result.FileUrl?.Path!);
71+
}
72+
}
73+
foreach (var item in filePaths)
74+
{
75+
Debug.WriteLine($"Path: {item}");
76+
}
77+
78+
static async Task<LoadInPlaceResult?> LoadItemAsync(NSItemProvider itemProvider, List<string> typeIdentifiers)
79+
{
80+
if (typeIdentifiers is null || typeIdentifiers.Count == 0)
81+
{
82+
return null;
83+
}
84+
85+
var typeIdent = typeIdentifiers.First();
86+
87+
if (itemProvider.HasItemConformingTo(typeIdent))
88+
{
89+
return await itemProvider.LoadInPlaceFileRepresentationAsync(typeIdent);
90+
}
91+
92+
typeIdentifiers.Remove(typeIdent);
93+
94+
return await LoadItemAsync(itemProvider, typeIdentifiers);
95+
}
96+
#else
97+
await Task.CompletedTask;
98+
#endif
99+
100+
lblPath.Text = filePaths.FirstOrDefault();
101+
}
102+
103+
void DropGestureDragOver(object? sender, DragEventArgs e)
104+
{
105+
Debug.WriteLine($"Dragging {e.Data?.Text}, {e.Data?.Image}");
106+
}
107+
108+
}
109+
}

src/Controls/samples/Controls.Sample/ViewModels/GesturesViewModel.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ protected override IEnumerable<SectionModel> CreateItems() => new[]
1111
{
1212
new SectionModel(typeof(DragAndDropBetweenLayouts), "Drag And Drop",
1313
"Drag and Drop Views."),
14+
new SectionModel(typeof(DropFileToMauiApp), "Drag and Drop file from OS",
15+
"Drop File to App"),
1416
new SectionModel(typeof(PanGestureGallery), "Pan Gesture",
1517
"Pan Gesture."),
1618
new SectionModel(typeof(PinchGestureTestPage), "Pinch Gesture",

src/Controls/src/Core/Platform/GestureManager/GesturePlatformManager.Windows.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,7 @@ void HandleDrop(object sender, Microsoft.UI.Xaml.DragEventArgs e)
202202
element = ve;
203203
}
204204

205-
if (datapackage is null)
206-
return;
207-
208-
var args = new DropEventArgs(datapackage.View, (relativeTo) => GetPosition(relativeTo, e), new PlatformDropEventArgs(sender as UIElement, e));
205+
var args = new DropEventArgs(datapackage?.View, (relativeTo) => GetPosition(relativeTo, e), new PlatformDropEventArgs(sender as UIElement, e));
209206
SendEventArgs<DropGestureRecognizer>(async rec =>
210207
{
211208
if (!rec.AllowDrop)

src/Controls/src/Core/Platform/iOS/DragAndDropDelegate.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,14 @@ public UIDropProposal SessionDidUpdate(UIDropInteraction interaction, IUIDropSes
9090
{
9191
UIDropOperation operation = UIDropOperation.Cancel;
9292

93-
if (session.LocalDragSession == null)
94-
return new UIDropProposal(operation);
95-
9693
DataPackage package = null;
97-
98-
if (session.LocalDragSession.Items.Length > 0 &&
99-
session.LocalDragSession.Items[0].LocalObject is CustomLocalStateData cdi)
94+
if (session.LocalDragSession != null)
10095
{
101-
package = cdi.DataPackage;
96+
if (session.LocalDragSession.Items.Length > 0 &&
97+
session.LocalDragSession.Items[0].LocalObject is CustomLocalStateData cdi)
98+
{
99+
package = cdi.DataPackage;
100+
}
102101
}
103102

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

0 commit comments

Comments
 (0)