Asked by:

Adding XAML controls to a game.

General discussion

I'm in the midst of creating a 3D game. The game is focused inside a structure with several rooms. The player is allowed to move on a fixed track to view into the different rooms, from a "hallway". If the player chooses to, he can enter one
of these rooms from the "hallway." Here, if the player wants to go back into the "hallway", I would like a small back button to appear (very consistent) so that the player would be teleported back into the hallway. Here's my issue:

Where do I instantiate this XAML control? Should it be apart of the screen that contains the swapchainpanel?

How do I respond to the touch event of this XAML button? The XAML button would be registered to an event in it's code behind, how could I make this effect my game?

Generally, I'm a little confused about mixing XAML and how the flow for the game should be handled, thanks.

> Where do I instantiate this XAML control? Should it be apart of the screen that contains the swapchainpanel?

The simplest way to do it would be to add the button to XAML Page that contains the SwapChainBackgroundPanel. Assign the Button control an x:Name so that you can reference it in code behind, e.g. SomeBackButton. Set its initial Visibility to Collapsed.

You likely have an App class that creates a new instance of that page (typically called DirectXPage) in its OnLaunched method and assigns it to be the current window's content. In my App.xaml.h I have a standalone function like this:

// Get's the Windows::UI::Xaml::Window::Current->Content UIElement^ as a DirectXPage^. This uses dynamic_cast and so
// the return value will be null if there is Window::Current->Content is null or if it is not a DirectXPage.
inline WindowsStoreDirectXGame::DirectXPage^ GetDirectXPageForApp()
{
return dynamic_cast<WindowsStoreDirectXGame::DirectXPage^>(Windows::UI::Xaml::Window::Current->Content);
}

The namespace in this case is WindowsStoreDirectXGame; you would want to change it to match your game's namespace (and change DirectXPage to whatever is the name of your swap chain panel page).

Then you'd add an accessor to your DirectXPage to get the button, e.g.:

It doesn't need to be a separate function; any where you have (or can generate) a Visibility value to assign, you can use the code in that function to change the value.

> How do I respond to the touch event of this XAML button? The XAML button would be registered to an event in it's code behind, how could I make this effect my game?

Assuming your DirectXPage has a reference to the game class, create a public member function of the game class which would allow you to affect the change you want. Then you can call it from your event handler (along with calling the code to set the right
visibility of the back button).