CsWinRT
CsWinRT copied to clipboard
Please simply wrap the DataTransferManager interop class
Proposal: [your title here]
Please simply wrap the DataTransferManager interop class
Summary
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);
}
} ...
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静态类的定义
Rationale
- {First reason for why we should consider this proposal}
- {Second reason for why we should consider this proposal}
- {etc}