forked from dotnet/maui
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dotnet:main' into lytico/gtk-ongoing
- Loading branch information
Showing
7 changed files
with
145 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/Controls/samples/Controls.Sample/Pages/Core/DropFileToMauiApp.xaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
109 changes: 109 additions & 0 deletions
109
src/Controls/samples/Controls.Sample/Pages/Core/DropFileToMauiApp.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
if (items.Any()) | ||
{ | ||
foreach (var item in items) | ||
{ | ||
if (item is StorageFile file) | ||
{ | ||
filePaths.Add(item.Path); | ||
} | ||
} | ||
|
||
} | ||
} | ||
#elif MACCATALYST | ||
|
||
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}"); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters