July 2012 1 post

Setting up SwapChainBackgroundPanel in a XAML app

Monday, July 30, 2012

After creating a XAML app for Windows 8 using the Blank XAML app template, I changed the body of MainPage.xaml from this:

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
to this:

While this compiles without warnings, it crashes on launch. With a C++/XAML app, a console message appears:

First-chance exception at 0x754F7945 (KernelBase.dll) in HelloWorld.exe: 0x40080201: WinRT originate error (parameters: 0x80004005, 0x0000005E, 0x031FDCE4).

In both C++ and C#, the exception message is as follows:

Page element must be root of visual tree when its content is SwapChainBackgroundPanel element.

This is because the default XAML template creates a Frame that hosts your app's pages, and it, not the Page object, is the root. To remove it, edit App.xaml.cpp's OnLaunched() function and replace this:

// Create a Frame to act navigation context and navigate to the first page
auto rootFrame = ref new Frame();
if (!rootFrame->Navigate(TypeName(MainPage::typeid)))
    throw ref new FailureException("Failed to create initial page");

// Place the frame in the current Window and ensure that it is active
Window::Current->Content = rootFrame;

with this:

Window::Current->Content = ref new MainPage();

If you're using C# and not C++, it's pretty much identical. Just omit the ref before new MainPage().

If you want to learn more about Direct3D and XAML, there is a good article on MSDN about the interop options available.

Tags: directx, direct3d, xaml, winrt, metro | Posted at 15:48 | Comments (10)