locked
How to start a Metro App using SwapChainBackgroundPanel

    Question

  • I'm attempting to build a C++ Metro app that uses SwapChainBackgroundPanel.  I want to use Blend for XAML overlays

    The only example I have found so far that actually uses Blend is Simple3DGameXaml (the shoot at the rectangle targets app.)

    Looking at MainPage.xaml for the sample, it has a Page element, a bottom and top AppBar (not really relevant to the question), and a SwapChainBackgoundPanel.  The panel is placed as a child of the panel, just like a grid would be in a "normal WPF" app.

    Now, I try it... I make a new blank Metro app, remove the old grid, and replace it with a SwapChainBackgroundPanel.  When I run, I get an exception which tells me that the SwapChainBackgroundPanel must be the root object in the xaml file (of which it is NOT in the Simple3DGameXaml sample, which both compiles and runs fine.)

    Add to this, when in Blend, if I attempt to delete the Page in order to make a SwapChainBackgroundPanel the root object, it will not allow me to delete the Page.

    I admit I have gotten no further... I have not done any code behind for the SwapChainBackgroundPanel... just added it and compiled to see where it would get me.

    So... what are the steps to start up a new app that:

    1. Is Metro, C++, and uses SwapChainBackgroundPanel.

    2. Has the ability to be edited in Blend (for the children of the panel of course.)

    I am not new to Blend... it would not surprise me if at this point I have to hand-edit the file to get it going, which is my next try... but it's bugging me that the sample actually has a page as the root, which means I'm missing something.

    Thanks,

    Jim


    Jim Tomasko

    Tuesday, June 5, 2012 3:41 AM

Answers

  • Hi Jim,

    There's one other required step which may not currently be obvious.  The Frame navigation model can't be used with a SwapChainBackgroundPanel-based app, so when starting from a blank C++ XAML app you need to directly set the Window::Content property to an instance of your MainPage rather than using the Frame::Navigate method.

    I.e. in the OnLaunched method in app.xaml.cpp, change this:

    auto rootFrame = ref new Frame();
    if (!rootFrame->Navigate(TypeName(MainPage::typeid)))
    {
    	throw ref new FailureException("Failed to create initial page");
    }
    Window::Current->Content = rootFrame;
    Window::Current->Activate();

    to something this:

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

    You should then be able to structure your MainPage.xaml markup to be similar to the game sample, e.g.:

    <Page   .... >
       <SwapChainBackgroundPanel>
          <!-- XAML children here -->
       </SwapChainBackgroundPanel>
       <!-- Page.BottomAppBar/TopAppBar here if applicable -->
    </Page>


    Tuesday, June 5, 2012 8:07 PM

All replies

  • Hi Jim,

    There's one other required step which may not currently be obvious.  The Frame navigation model can't be used with a SwapChainBackgroundPanel-based app, so when starting from a blank C++ XAML app you need to directly set the Window::Content property to an instance of your MainPage rather than using the Frame::Navigate method.

    I.e. in the OnLaunched method in app.xaml.cpp, change this:

    auto rootFrame = ref new Frame();
    if (!rootFrame->Navigate(TypeName(MainPage::typeid)))
    {
    	throw ref new FailureException("Failed to create initial page");
    }
    Window::Current->Content = rootFrame;
    Window::Current->Activate();

    to something this:

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

    You should then be able to structure your MainPage.xaml markup to be similar to the game sample, e.g.:

    <Page   .... >
       <SwapChainBackgroundPanel>
          <!-- XAML children here -->
       </SwapChainBackgroundPanel>
       <!-- Page.BottomAppBar/TopAppBar here if applicable -->
    </Page>


    Tuesday, June 5, 2012 8:07 PM
  • Thanks Jesse,

    My early opinion is "epic job Microsoft!".  Right now I'm lost, confused, and bewildered, but finding things like the Platform::winmd namespace feels all warm and XAMLly.  Finding that a button has a DependencyObject (as an example) in it's hierarchy is a pleasant and unexpected surprise to.

    Are we now "Engineers with Hats"?

    Jim


    Jim Tomasko

    Wednesday, June 6, 2012 11:49 PM
  • Hi Jesse,

    this won't work either. In the MSDN (http://msdn.microsoft.com/library/windows/apps/Hh702626) you can read, that the SwapChainBackgroundPanel is used to be the root element and should be set directly into the Window.

    _dxPage = ref new DirectXPage(); //DirectXPage == SwapChainBackgroundPanel
    
    Window::Current->Content = _dxPage;
    Window::Current->Activate();

    and the according XAML or DirectXPage:

    <SwapChainBackgroundPanel 
        x:Class="DirectXPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
        <Frame x:Name="_frame" Navigated="FrameOnNavigatedTo" NavigationFailed="FrameNavigationFailed">
    <!-- add some pages here -->
        </Frame>
    
    </SwapChainBackgroundPanel>

    @Jim: In the current Version the VS Templates just works quite well.  (see C++ -> Direct2D template)

    -Robert


    • Edited by RoXXX_DE Thursday, June 21, 2012 2:28 PM
    Thursday, June 21, 2012 2:27 PM
  • in C# , how to program ? natigate to other pages
    Tuesday, November 27, 2012 10:19 AM