c# - 如何将 UWP 应用程序注册为相机快照的共享接收者

标签 c# uwp share screen-capture

在 Windows 10 中,当我点击例如SHIFT + WIN + S 我可以截图我的屏幕。 在其他情况下,我有可以拍照的网络摄像头,等等。 所有这些场景都具有将捕获的图像“共享”到另一个应用程序的功能,例如邮件、OneNote 等

我想注册我自己的 UWP 应用程序成为此类共享的接收者,以便用户可以在我的 UWP 应用程序中操作捕获的图像。

有没有办法配置我的 UWP 应用来执行此操作?

最佳答案

Is there a way to configure my UWP app to do this?

是的,当您想从其他应用分享一些内容时,您可以让您的 UWP 应用成为接收器。

  1. 将您的应用声明为共享目标。打开 list 文件。找到 Declarations 选项卡,然后从 Available Declarations 列表中选择 Share Target,然后选择 Add。

  2. 根据您在声明中的要求设置文件类型和格式。例如,如果您需要接收屏幕截图,则需要在Data format 中添加Bitmap

  3. 通过处理 Application.OnShareTargetActivated event 在 App.Xaml.cs 中处理共享激活.

我对此做了简单的测试,你可以引用一下。有关这方面的更多信息,您还可以查看此文档: Receive data

list 文件:

enter image description here

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/

相关文章:

c# - C# 中的并行管道

c# - twoWay 绑定(bind)在以 ObservableCollection 作为源的 ToggleButton 中不起作用

visual-studio - 没有开发者模式/开发者许可证,无法旁加载 Win10 UWP 应用程序

Android 分享 Intent - facebook

c# - 我应该如何为我的 C# Windows 窗体应用程序编写我的事件处理程序?

c# - 如何对高于阈值的序列元素进行分组?

c# - 身份用户管理器 DeleteAsync DbUpdateConcurrencyException

c# - UWP Storyboard 和 CallMethodAction 的仅发布异常(exception)

java - 我通过 Intent 上传的文本和网址对于 Twitter 来说太长

android - 检查 SD 卡上是否存在图像 - Android