How to create CoreApplicationView (C++/CX)?

0

Im trying to create a second window on UWP, but nothing works out for me. Unfortunately, I`m not well in UWP and there are few guides for C++/CX. My IFrameworkViewSource:

IFrameworkView^ ApplicationSource::CreateView()
{
    return ref new test();
}

maximally simple implementation of IFrameworkView:

test::test() 
{
}    

void test::Initialize(CoreApplicationView^ applicationView)
{
    applicationView->Activated +=
        ref new TypedEventHandler<CoreApplicationView^, IActivatedEventArgs^>(this, &test::OnActivated);
}

void test::SetWindow(Windows::UI::Core::CoreWindow^ window) {

}    

void test::Load(Platform::String^ entryPoint)
{
}

void test::Run()
{
}

void test::Uninitialize()
{
}

void test::OnActivated(CoreApplicationView^ applicationView, IActivatedEventArgs^ args)
{
    CoreWindow::GetForCurrentThread()->Activate();
}

The trying to create a view:

auto applicationSource = ref new ApplicationSource();
CoreApplication::CreateNewView(applicationSource);

Sometimes application is running, sometimes it return HR 0x8000000e . I`m using a classic template in microsoft visual studio 2019 "Application DirectX11(UWP - C++/CX)

uwp
c++-cx
asked on Stack Overflow Feb 15, 2020 by wirkl • edited Feb 15, 2020 by drescherjm

1 Answer

0

You can first call CoreApplication.CreateNewView to create a new window and thread for the view content. Then Track the Id of the new view. You use this to show the view later. After that, active window and show the new view. You could try to use the following code and about more details, you can refer to this document which is using c #.

auto newView = CoreApplication::CreateNewView(ref new ApplicationSource());
newView->Dispatcher->RunAsync(CoreDispatcherPriority::Normal, ref new DispatchedHandler([this]()
{
    auto viewId = Windows::UI::ViewManagement::ApplicationView::GetForCurrentView()->Id;
    CoreWindow::GetForCurrentThread()->Activate();
    CoreApplication::MainView->Dispatcher->RunAsync(CoreDispatcherPriority::Normal, ref new DispatchedHandler([viewId]()
    {
        auto asyncAction = Windows::UI::ViewManagement::ApplicationViewSwitcher::SwitchAsync(viewId, Windows::UI::ViewManagement::ApplicationView::GetForCurrentView()->Id);
    }));
}));
answered on Stack Overflow Feb 17, 2020 by Faywang - MSFT

User contributions licensed under CC BY-SA 3.0