question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DataTransferManager.GetForCurrentView() in WinUI 3

See original GitHub issue

Describe the bug Calling DataTransferManager.GetForCurrentView() fails with a System.Runtime.InteropServices.COMException: 'Element not found'. This is somewhat expected, given the ‘GetForCurrentView’ name.

However, I have not found any mention of this particular class. Especially not in the WinRT API changes for Desktop apps.

Is cleaning this up already somewhere on your radar? Can I work around this to make a share dialog work in WinUI 3?

Steps to reproduce the bug

var dataTransferManager = DataTransferManager.GetForCurrentView();
dataTransferManager.DataRequested += DataTransferManager_DataRequested;

Version Info

NuGet package version: WinUI 3 - Project Reunion 0.5: 0.5.5

Windows app type:

UWP Win32
Yes
Windows 10 version Saw the problem?
Insider Build (xxxxx)
October 2020 Update (19042) Yes
May 2020 Update (19041)
November 2019 Update (18363)
May 2019 Update (18362)
October 2018 Update (17763)
April 2018 Update (17134)
Fall Creators Update (16299)
Creators Update (15063)
Device form factor Saw the problem?
Desktop Yes
Xbox
Surface Hub
IoT

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
shuicecommented, Nov 19, 2021

IDataTransferManagerInterop is exactly what I was looking for.

I’m using CsWin32, and with that the code to get the DataTransferManager for the current view is:

using WinRT;

var guid = Guid.Parse("a5caee9b-8708-49d1-8d36-67d25a8da00c");
var iop = DataTransferManager.As<IDataTransferManagerInterop>();
var transferManager = IntPtr.Zero;
iop.GetForWindow(PInvoke.GetActiveWindow(), guid, (void**)&transferManager);
var realManager = DataTransferManager.FromAbi(transferManager);

I filed an issue over at CSWin32 to see if that can be simplified in the future, but it works for now.

Thanks!

Maybe a comment could be added to the DataTransferManager docs or this document?

I use the following on WinUI3, It’s works, a little different on function GetForWindow, and different to page migrate-to-windows-app-sdk/guides/winui3

private void http_server_demo_share_send_uri(string uri)
{
        IntPtr windowHandle = WinRT.Interop.WindowNative.GetWindowHandle(this);
        IDataTransferManagerInterop interop = Windows.ApplicationModel.DataTransfer.DataTransferManager.As<IDataTransferManagerInterop>();

        var guid = Guid.Parse("a5caee9b-8708-49d1-8d36-67d25a8da00c");
        var iop = DataTransferManager.As<IDataTransferManagerInterop>();
        var transferManager = DataTransferManager.FromAbi(iop.GetForWindow(windowHandle, guid));
        transferManager.DataRequested += (DataTransferManager dtm, DataRequestedEventArgs args) =>
        {
                DataPackage dataPackage = new DataPackage();
                dataPackage.SetWebLink(new Uri(uri));
                dataPackage.Properties.Title = "Share uri \"" + uri + "\"";
                args.Request.Data = dataPackage;
        };

        interop.ShowShareUIForWindow(windowHandle);
}


[System.Runtime.InteropServices.ComImport, System.Runtime.InteropServices.Guid("3A3DCD6C-3EAB-43DC-BCDE-45671CE800C8")]
[System.Runtime.InteropServices.InterfaceType(System.Runtime.InteropServices.ComInterfaceType.InterfaceIsIUnknown)]
interface IDataTransferManagerInterop
{
        IntPtr GetForWindow([System.Runtime.InteropServices.In] IntPtr appWindow, [System.Runtime.InteropServices.In] ref Guid riid);
        void ShowShareUIForWindow(IntPtr appWindow);
}
1reaction
anawishnoffcommented, Apr 27, 2021

Thanks! We’re planning on publishing this doc to our official docs site soon with some changes and this API should be included.

Read more comments on GitHub >

github_iconTop Results From Across the Web

DataTransferManager.GetForCurrentView Method
This example calls the GetForCurrentView method to get the DataTransferManager object associated with the active window for the app.
Read more >
DataTransferManager Class - Windows
To use the class, first call the GetForCurrentView method. This method returns the DataTransferManager object that is specific to the active window. Next,...
Read more >
Building a Windows 8 Metro App, Part 3: Listing 8
Click += new RoutedEventHandler(FetchButton_Click); _dataTransferManager = DataTransferManager.GetForCurrentView(); _dataTransferManager.
Read more >
c# - Why is this Element not Found?
In your case SettingsPane.GetForCurrentView() is throwing an exception because you're calling it too early. You are configuring the settings ...
Read more >
CaptureElement, Windows.UI.Xaml.Controls ...
GetForCurrentView().BackRequested +=BackRequested; DataTransferManager ShareManager = DataTransferManager.GetForCurrentView(); ShareManager.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found