当从 ShareTarget 合同创建的第二个窗口调用时,我很难处理一些代码,这些代码显然很困难(当您向应用程序共享某些内容时,它会在一个小的独立窗口中打开)。
到目前为止,这是我的代码:
// Blur and resize the image to get the average HSL color
// Assume that stream is an IRandomAccessStream pointing to valid image data
HslColor hslMean;
using (RandomAccessStreamImageSource imageProvider = new RandomAccessStreamImageSource(stream))
using (BlurEffect blurEffect = new BlurEffect(imageProvider) { KernelSize = 256 })
{
Color mean = await DispatcherHelper.GetFromUIThreadAsync(async () =>
{
WriteableBitmap
blurred = new WriteableBitmap((int)decoder.PixelWidth, (int)decoder.PixelHeight),
result = await blurEffect.GetBitmapAsync(blurred, OutputOption.Stretch),
resized = result.Resize(1, 1, WriteableBitmapExtensions.Interpolation.Bilinear);
return resized.GetPixel(0, 0);
});
hslMean = mean.ToHsl();
}
注意:该DispatcherHelper.GetFromUIThreadAsync
方法只检查线程对 UI 线程的访问,如果需要,它会将代码调度到CoreDispatcher
使用CoreApplication.MainView.CoreWindow.Dispatcher
.
问题:如果我的应用程序已经打开,则此代码可以 100% 正常工作,因为此时该
CoreDispatcher
对象已经由之前对该DispatcherHelper
类的调用创建,因此该方法仅使用存储的调度程序来安排工作并且工作正常。但是,如果在打开 ShareTarget 窗口时关闭了应用程序(因此DispatcherHelper
必须第一次创建调度程序),则该CoreApplication.MainView.CoreWindow
行将引发异常。一个非常奇怪的:
COMException
: 对 ASTA 的 COM 调用被阻止,因为调用链源自或通过另一个 ASTA。这种呼叫模式容易发生死锁,并且被公寓呼叫控制所禁止。对 ASTA(线程 10276)的 COM 调用(IID:{638BB2DB-451D-4661-B099-414F34FFB9F1},方法索引:6)被阻止,因为调用链源自或通过另一个 ASTA(线程 4112)。这种呼叫模式容易发生死锁,并且被公寓呼叫控制所禁止。
因此,即使从不同的窗口调用,我也需要一种方法来使该方法可靠。我尝试了不同的选择:
#1 : 只调用该代码而不分派到不同的线程,因为理论上我此时应该在 UI 线程上 ---> FAIL(应用程序调用了一个为不同线程编组的接口。(来自 HRESULT 的异常: 0x8001010E (RPC_E_WRONG_THREAD)))
#2 : 手动调用CoreApplication.MainView.CoreWindow.Dispatcher
以调度该代码块 ---> FAIL (我得到了上面提到的那个奇怪COMException
的)
#3:手动使用CoreApplication.MainView.Dispatcher
来调度代码块(因为它是.CoreWindow
产生异常的部分)---> FAIL(COMException
:未找到项目)
#4 : 使用CoreApplication.GetCurrentView().CoreWindow.Dispatcher
, CoreApplication.GetCurrentView().Dispatcher
,Window.Current.CoreWindow.Dispatcher
和Window.Current.Content.Dispatcher
来调度该代码---> FAIL(又是错误的线程,我得到了通常的编组异常)
所有这些编组异常都被抛出result = await blurEffect.GetBitmapAsync(blurred, OutputOption.Stretch)
,所以我怀疑它可能与 Lumia Imaging SDK 有关。我的意思是,我很确定我实际上是在 UI 线程上,否则我将无法创建WriteableBitmap
该类的实例,对吗?
为什么我可以创建
WriteableBitmap
对象(据我所知,它们需要在 UI 线程上创建),但GetBitmapAsync
Lumia SDK 中的该方法总是抛出该编组异常?我在我的应用程序的任何地方都使用它,没有任何问题,为什么它不能从 ShareTarget 窗口工作?有什么我需要做的吗?
谢谢你的帮助!