Skip to content

Fix: Fixed exception that would sometimes occur when sharing items #14298

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

Merged
merged 2 commits into from
Dec 27, 2023
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
4 changes: 1 addition & 3 deletions src/Files.App/Actions/Content/Share/ShareItemAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ public ShareItemAction()

public Task ExecuteAsync()
{
ShareItemHelpers.ShareItems(context.SelectedItems);

return Task.CompletedTask;
return ShareItemHelpers.ShareItemsAsync(context.SelectedItems);
}

private bool IsContextPageTypeAdaptedToCommand()
Expand Down
23 changes: 21 additions & 2 deletions src/Files.App/Helpers/ShareItemHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@

using Files.App.Extensions;
using Files.App.Utils;
using Microsoft.UI.Xaml.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using Windows.ApplicationModel.DataTransfer;
using Windows.Foundation;
using Windows.Foundation.Metadata;
using Windows.Storage;

namespace Files.App.Helpers
Expand All @@ -19,15 +21,32 @@ public static bool IsItemShareable(ListedItem item)
(!item.IsShortcut || item.IsLinkItem) &&
(item.PrimaryItemAttribute != StorageItemTypes.Folder || item.IsArchive);

public static void ShareItems(IEnumerable<ListedItem> itemsToShare)
public static async Task ShareItemsAsync(IEnumerable<ListedItem> itemsToShare)
{
var interop = DataTransferManager.As<IDataTransferManagerInterop>();
IntPtr result = interop.GetForWindow(MainWindow.Instance.WindowHandle, InteropHelpers.DataTransferManagerInteropIID);

var manager = WinRT.MarshalInterface<DataTransferManager>.FromAbi(result);
manager.DataRequested += new TypedEventHandler<DataTransferManager, DataRequestedEventArgs>(Manager_DataRequested);

interop.ShowShareUIForWindow(MainWindow.Instance.WindowHandle);
try
{
interop.ShowShareUIForWindow(MainWindow.Instance.WindowHandle);
}
catch (Exception ex)
{
var errorDialog = new ContentDialog()
{
Title = "FaildToShareItems".GetLocalizedResource(),
Content = ex.Message,
PrimaryButtonText = "OK".GetLocalizedResource(),
};

if (ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 8))
errorDialog.XamlRoot = MainWindow.Instance.Content.XamlRoot;

await errorDialog.TryShowAsync();
}

async void Manager_DataRequested(DataTransferManager sender, DataRequestedEventArgs args)
{
Expand Down
3 changes: 3 additions & 0 deletions src/Files.App/Strings/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -3662,4 +3662,7 @@
<data name="FailedToRotateImage" xml:space="preserve">
<value>Failed to rotate the image</value>
</data>
<data name="FaildToShareItems" xml:space="preserve">
<value>Failed to share items</value>
</data>
</root>