locked
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.

    Saturday, May 18, 2013 7:56 PM

All replies


  • Hello,

    I would like to change this thread type as discussion, because I think there may be no exact answer for this question.

    Thanks for your understanding,

    Best regards,
    Jesse


    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, May 20, 2013 4:19 AM
    Moderator
  • > 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.:

    Windows::UI::Xaml::Controls::Button^ DirectXPage::GetSomeBackButton()
    {
    	return SomeBackButton;
    }
    

    Given all of that, you can always run the following code to set the state of the button's visibility:

    void SetButtonVisibility(Windows::UI::Xaml::Visibility visibility)
    {
    	auto page = GetDirectXPageForApp();
    	// Ensure that the UI state change runs on the main thread.
    	page->Dispatcher->RunAsync(
    		Windows::UI::Core::CoreDispatcherPriority::Normal,
    		ref new Windows::UI::Core::DispatchedHandler([page, visibility]()
    	{
    		page->GetSomeBackButton()->Visibility = visibility;
    	}));
    }
    
    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).


    Visual C++ MVP | Website | Blog | @mikebmcl | Windows Store DirectX Game Template

    Monday, May 20, 2013 11:31 PM
  • Yep, that was it. Thanks.
    Friday, May 24, 2013 8:14 PM