通过windows phone 8.1运行时组件访问CoreWindow

Accessing CoreWindow from a windows phone 8.1 runtime component

本文关键字:组件 访问 CoreWindow 运行时 windows phone 通过      更新时间:2023-10-16

我试图从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在silverlight应用程序中返回null。是否有一种方法可以做到这一点,以便它在silverlight以及windows商店应用程序中工作?

该对象仅在通用应用程序中可用(如文档所示)。

你需要将编译条件化,或者考虑切换到桌面和手机的通用应用程序。