从 windows phone 8.1 运行时组件访问 CoreWindow

Accessing CoreWindow from a windows phone 8.1 runtime component

我正在尝试从 windows phone 8.1 C++ 运行时组件访问 CoreWindow。该组件需要对 CoreWindow 触发的一些事件做出反应。我有以下代码。

IAsyncAction^ MyClass::RegisterCoreWindowVisibilityChanged()
{
    return CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(CoreDispatcherPriority::High,
        ref new DispatchedHandler(
        [this]
        {
            auto eventHandler = ref new TypedEventHandler<CoreWindow^, VisibilityChangedEventArgs^>(this, &MyClass::OnCoreWindowVisibilityChanged);
            Window::Current->CoreWindow->VisibilityChanged += eventHandler;
        }
    ));
}

当使用该组件的应用程序是一个通用应用程序时,这工作正常,但在 silverlight 应用程序中失败并出现访问冲突异常。

0xC0000005: Access violation reading location 0x00000000.

显然 Windows::Current returns 在 silverlight 应用程序中为空。有没有办法让它在 silverlight 和 windows 商店应用程序中工作?

该对象仅在通用应用程序中可用(如 documented)。

您需要对编译进行条件化或考虑切换到适用于桌面和 phone 的通用应用程序。