winrt: Try to acquire CoreDispatcher from multiple sources

When the system launches the application via different activation mode
(eg. app registered for sharing) no main window will be created. Hence
accessing the core window will return null and event dispatcher
initialization will fail.

In that case iterate through all available views and try to get access
to their dispatcher to be able to invoke code on the xaml thread.

Task-number: QTBUG-49276
Change-Id: I8c78baa27747a0465ff7a1b2ead6c9e03f0e05a8
Reviewed-by: Oliver Wolff <oliver.wolff@theqtcompany.com>
bb10
Maurice Kalinowski 2016-04-08 07:39:41 +02:00
parent 0f9ca217d0
commit 3b98467ebe
1 changed files with 29 additions and 2 deletions

View File

@ -50,6 +50,7 @@ using namespace Microsoft::WRL;
using namespace Microsoft::WRL::Wrappers;
using namespace ABI::Windows::System::Threading;
using namespace ABI::Windows::Foundation;
using namespace ABI::Windows::Foundation::Collections;
using namespace ABI::Windows::UI::Core;
using namespace ABI::Windows::ApplicationModel::Core;
@ -179,8 +180,34 @@ HRESULT QEventDispatcherWinRT::runOnXamlThread(const std::function<HRESULT ()> &
ComPtr<ICoreWindow> window;
hr = view->get_CoreWindow(&window);
Q_ASSERT_SUCCEEDED(hr);
hr = window->get_Dispatcher(&dispatcher);
Q_ASSERT_SUCCEEDED(hr);
if (!window) {
// In case the application is launched via activation
// there might not be a main view (eg ShareTarget).
// Hence iterate through the available views and try to find
// a dispatcher in there
ComPtr<IVectorView<CoreApplicationView*>> appViews;
hr = application->get_Views(&appViews);
Q_ASSERT_SUCCEEDED(hr);
quint32 count;
hr = appViews->get_Size(&count);
Q_ASSERT_SUCCEEDED(hr);
for (quint32 i = 0; i < count; ++i) {
hr = appViews->GetAt(i, &view);
Q_ASSERT_SUCCEEDED(hr);
hr = view->get_CoreWindow(&window);
Q_ASSERT_SUCCEEDED(hr);
if (window) {
hr = window->get_Dispatcher(&dispatcher);
Q_ASSERT_SUCCEEDED(hr);
if (dispatcher)
break;
}
}
Q_ASSERT(dispatcher);
} else {
hr = window->get_Dispatcher(&dispatcher);
Q_ASSERT_SUCCEEDED(hr);
}
}
HRESULT hr;