Search code examples
c#c++silverlightwindows-runtimewindows-store-apps

Accessing CoreWindow from a windows phone 8.1 runtime component


I'm trying to access the CoreWindow from a windows phone 8.1 C++ runtime component. The component needs to react to some events fired by CoreWindow. I have the following code.

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;
        }
    ));
}

This works fine, when the application using the component is a universal app, but fails in a silverlight application with an access violation exception.

0xC0000005: Access violation reading location 0x00000000.

Apparently Windows::Current returns null in a silverlight app. Is there a way to do it so that it works in silverlight as well as a windows store app?


Solution

  • That object is only available in a Universal application (as documented).

    You would need to either conditionalize your compile or consider switching to a universal app for both desktop and phone.