Repository: microsoft/WindowsAppSDK

Issue #1063: Simplify using UWP file pickers from desktop apps
Created: 2021-07-15T04:19:49Z
To use UWP file pickers such as file open picker or file save picker in desktop apps, you need to cast the object reference to IInitializeWithWindow and invoke its Initialize method. Although one can use Win32 file pickers, those lack some features like the ability to use picker contract apps. To simplify usage, I would suggest adding an overload for the constructor of FileOpenPicker and FileSavePicker to take a window handle. Since HWND can't be represented with the Windows Runtime type system, it can take an int64_t or the new WindowId structure instead.

Duplicate issue #3777: Please simply wrap the DataTransferManager interop class
Created: 2023-08-02T04:49:40Z
Describe the bug
Please simply wrap the DataTransferManager interop class
At present in desktop applications, if we want to invoke DataTransferManager class methods, the need to import IDataTransferManagerInterop interface, and implement the corresponding operation in accordance with the corresponding methods, it is a bit too cumbersome. Hope future versions can be a simple packaging IDataTransferManagerInterop interface, to call DataTransferManager method in a class.
目前在桌面应用中，如果我们想要调用DataTransferManager类的方法，需要自己导入IDataTransferManagerInterop接口，并按照相应的方法实现对应的操作，这有点太繁琐了。希望未来的版本中可以简单包装一下IDataTransferManagerInterop接口，能更快的调用DataTransferManager类中的方法。
This is the code in the official example.
这是官方示例中的代码。
// MainWindow.xaml.cs
...
public sealed partial class MainWindow : Window
{
...
[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);
}
static readonly Guid _dtm_iid = 
    new Guid(0xa5caee9b, 0x8708, 0x49d1, 0x8d, 0x36, 0x67, 0xd2, 0x5a, 0x8d, 0xa0, 0x0c);

private void myButton_Click(object sender, RoutedEventArgs e)
{
    // Retrieve the window handle (HWND) of the current WinUI 3 window.
    var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);

    IDataTransferManagerInterop interop =
    Windows.ApplicationModel.DataTransfer.DataTransferManager.As
        <IDataTransferManagerInterop>();

    IntPtr result = interop.GetForWindow(hWnd, _dtm_iid);
    var dataTransferManager = WinRT.MarshalInterface
        <Windows.ApplicationModel.DataTransfer.DataTransferManager>.FromAbi(result);

    dataTransferManager.DataRequested += (sender, args) =>
    {
        args.Request.Data.Properties.Title = "In a desktop app...";
        args.Request.Data.SetText("...display WinRT UI objects that depend on CoreWindow.");
        args.Request.Data.RequestedOperation = 
            Windows.ApplicationModel.DataTransfer.DataPackageOperation.Copy;
    };

    // Show the Share UI
    interop.ShowShareUIForWindow(hWnd);
}

}
...

Steps to reproduce the bug
None
Expected behavior
Please provide a DataTransferManagerInterop static class, the class has achieved IDataTransferManagerInterop interface, and provides the corresponding interactions. The following code
请提供一个DataTransferManagerInterop静态类，这个类内部已经实现了IDataTransferManagerInterop接口，并提供了相应的交互操作。如下面的代码
public static class DataTransferManagerInterop
{
    private static readonly Guid riid = new Guid(0xa5caee9b, 0x8708, 0x49d1, 0x8d, 0x36, 0x67, 0xd2, 0x5a, 0x8d, 0xa0, 0x0c);

    private static IDataTransferManagerInterop dataTransferManagerInterop = DataTransferManager.As<IDataTransferManagerInterop>();

    public static DataTransferManager GetForWindow(IntPtr window)
    {
        return MarshalInterface<DataTransferManager>.FromAbi(dataTransferManagerInterop.GetForWindow(window, riid));
    }

    public static void ShowShareUI(IntPtr window)
    {
        dataTransferManagerInterop.ShowShareUIForWindow(window);
    }
}

In this wrapped class, if we need to use the methods provided in the DataTransferManager class, we only need one line of code:
在这个包装好的类中，如果我们需要使用DataTransferManager类中提供的方法，只需要一行代码：
DataTransferManager dataTransferManager = DataTransferManagerInterop.GetForWindow(/need to input window handle/);
To display the shared window, just:
想要显示共享窗口，只需要：
DataTransferManagerInterop.ShowShareUI(/need to input window handle/);
It can be called quickly.
即可快速调用。
Screenshots
Defined static class
定义的静态类

快速调用示例
Quick call example

The definition of the DataTransferManagerInterop static class reference is DisplayInformationInterop static class definition
这个DataTransferManagerInterop静态类的定义参考的是DisplayInformationInterop静态类的定义
NuGet package version
Windows App SDK 1.4 Preview 1: 1.4.230628000-preview1
Packaging type
Packaged (MSIX)
Windows version
Windows 11 version 22H2 (22621, 2022 Update)
IDE
Visual Studio 2022
Additional context
None

Closing comment (2023-08-09T13:52:58Z): Related to #1063