HWA:实例化RenderTargetBitmap即使在UI线程

时间:2018-05-14 15:32:39

标签: c# uwp rendertargetbitmap

我尝试使用RenderTargetBitmap获取Windows托管Web应用程序(HWA)窗口的屏幕截图,但在处理来自Javascript Web的请求时,我似乎无法实例化它应用程序在HWA应用程序上运行:

public async Task<IBuffer> CaptureScreenshotToFileAsync(uint thumbnailWidth, uint thumbnailHeight, string fileName)
{
    // Throws RPC_E_WRONG_THREAD
    RenderTargetBitmap screenshotBitmap = new RenderTargetBitmap();

    // Assumed that this was a background thread, but...
    await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
    {
        // Surprisingly same thread as above, naturally also throws RPC_E_WRONG_THREAD
        RenderTargetBitmap screenshotBitmap = new RenderTargetBitmap();            
    });
}

在这两种情况下,都会抛出:

System.Exception: The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E    (RPC_E_WRONG_THREAD))
       at Windows.UI.Xaml.Media.Imaging.RenderTargetBitmap..ctor()

顺便说一下,如果我从标准UWP应用程序中的后台线程执行以下操作,它会按预期工作:

public MainPage()
{
    // UI thread

    Task.Run(() => {
        // Background thread
        await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
        {
            // Back in UI thread.
            RenderTargetBitmap targetBitmap = new RenderTargetBitmap(); // doesn't throw.
        }
    });
}

任何人都可以解释:

  • 为什么RenderTargetBitmap的实例化在&#34; UI&#34; HWA应用程序中的线程?
  • 为什么来自Javascript的Web请求由&#34; UI&#34;处理。 HWA应用程序的线程(奖金)?

0 个答案:

没有答案