在 Windows 10 中,当我点击例如SHIFT + WIN + S 我可以截图我的屏幕。 在其他情况下,我有可以拍照的网络摄像头,等等。 所有这些场景都具有将捕获的图像“共享”到另一个应用程序的功能,例如邮件、OneNote 等
我想注册我自己的 UWP 应用程序成为此类共享的接收者,以便用户可以在我的 UWP 应用程序中操作捕获的图像。
有没有办法配置我的 UWP 应用来执行此操作?
最佳答案
Is there a way to configure my UWP app to do this?
是的,当您想从其他应用分享一些内容时,您可以让您的 UWP 应用成为接收器。
将您的应用声明为共享目标。打开 list 文件。找到 Declarations 选项卡,然后从 Available Declarations 列表中选择 Share Target,然后选择 Add。
根据您在声明中的要求设置文件类型和格式。例如,如果您需要接收屏幕截图,则需要在Data format 中添加
Bitmap
。通过处理 Application.OnShareTargetActivated event 在 App.Xaml.cs 中处理共享激活.
我对此做了简单的测试,你可以引用一下。有关这方面的更多信息,您还可以查看此文档: Receive data
list 文件:
App.xaml.cs:
protected override async void OnShareTargetActivated(ShareTargetActivatedEventArgs args)
{
ShareOperation shareOperation = args.ShareOperation;
if (shareOperation.Data.Contains(StandardDataFormats.Bitmap))
{
var imageStream = await shareOperation.Data.GetBitmapAsync();
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame == null)
{
// Create a Frame to act as the navigation context and navigate to the first page
rootFrame = new Frame();
Window.Current.Content = rootFrame;
}
rootFrame.Navigate(typeof(ShareImagePage), imageStream);
Window.Current.Activate();
}
}
ShareImagePage:
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
if (e.Content != null)
{
IRandomAccessStreamReference imageReceived = null;
imageReceived = e.Parameter as IRandomAccessStreamReference;
using (var imageStream = await imageReceived.OpenReadAsync())
{
var bitmapImage = new BitmapImage();
bitmapImage.SetSource(imageStream);
imgImage.Source = bitmapImage;
}
}
}
关于c# - 如何将 UWP 应用程序注册为相机快照的共享接收者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73780274/