user5773230
user5773230

Reputation:

UWP Back Button and Event Handler - C++

I am new to Windows 10 UWP apps and currently I am trying out various navigation techniques in apps. I have searched for the back button functionality in UWP apps but cant seem to understand that where do I have to put the "provided code" and how to execute it. I have also tried to do it on my own but it throws various errors in Visual Studio about identifier not found.

The first code is:

    Windows::UI::Core::SystemNavigationManager::GetForCurrentView()->
    BackRequested += ref new Windows::Foundation::EventHandler<
    Windows::UI::Core::BackRequestedEventArgs^>(
    this, &App::App_BackRequested);

The second piece of code is:

    void App::App_BackRequested(
    Platform::Object^ sender, 
    Windows::UI::Core::BackRequestedEventArgs^ e)
    {
    Frame^ rootFrame = dynamic_cast<Frame^>(Window::Current->Content);
    if (rootFrame == nullptr)
    return;

    // Navigate back if possible, and if the event has not
    // already been handled.
    if (rootFrame->CanGoBack && e->Handled == false)
        {
            e->Handled = true;
            rootFrame->GoBack();
        }
    }

Please help me in this matter and tell me how do I implement this code in my UWP App. Thanks

Source: https://msdn.microsoft.com/en-us/library/windows/apps/mt465734.aspx

Upvotes: 0

Views: 1358

Answers (2)

Daniel Krzyczkowski
Daniel Krzyczkowski

Reputation: 3167

It should be like below:

This is OnLaunched method in App.xaml.cpp class:

Just before its end you should paste first fragment:

 void App::OnLaunched(Windows::ApplicationModel::Activation::LaunchActivatedEventArgs^ e)
{
#if _DEBUG
// Show graphics profiling information while debugging.
if (IsDebuggerPresent())
{
    // Display the current frame rate counters
     DebugSettings->EnableFrameRateCounter = true;
}
#endif
auto rootFrame = dynamic_cast<Frame^>(Window::Current->Content);

// Do not repeat app initialization when the Window already has content,
// just ensure that the window is active
if (rootFrame == nullptr)
{
    // Create a Frame to act as the navigation context and associate it with
    // a SuspensionManager key
    rootFrame = ref new Frame();

    rootFrame->NavigationFailed += ref new Windows::UI::Xaml::Navigation::NavigationFailedEventHandler(this, &App::OnNavigationFailed);

    if (e->PreviousExecutionState == ApplicationExecutionState::Terminated)
    {
        // TODO: Restore the saved session state only when appropriate, scheduling the
        // final launch steps after the restore is complete

    }


    if (e->PrelaunchActivated == false)
    {
        if (rootFrame->Content == nullptr)
        {
            // When the navigation stack isn't restored navigate to the first page,
            // configuring the new page by passing required information as a navigation
            // parameter
            rootFrame->Navigate(TypeName(MainPage::typeid), e->Arguments);
        }
        // Place the frame in the current Window
        Window::Current->Content = rootFrame;
        // Ensure the current window is active
        Window::Current->Activate();
    }
}
else
{
    if (e->PrelaunchActivated == false)
    {
        if (rootFrame->Content == nullptr)
        {
            // When the navigation stack isn't restored navigate to the first page,
            // configuring the new page by passing required information as a navigation
            // parameter
            rootFrame->Navigate(TypeName(MainPage::typeid), e->Arguments);
        }
        // Ensure the current window is active
        Window::Current->Activate();
    }
}

Windows::UI::Core::SystemNavigationManager::GetForCurrentView()->
    BackRequested += ref new Windows::Foundation::EventHandler<
    Windows::UI::Core::BackRequestedEventArgs^>(
        this, &App::App_BackRequested);
 }

Now under OnLaunched method please paste this handler:

void App::App_BackRequested(
Platform::Object^ sender,
Windows::UI::Core::BackRequestedEventArgs^ e)
{
Frame^ rootFrame = dynamic_cast<Frame^>(Window::Current->Content);
if (rootFrame == nullptr)
    return;

// Navigate back if possible, and if the event has not
// already been handled.
if (rootFrame->CanGoBack && e->Handled == false)
  {
    e->Handled = true;
    rootFrame->GoBack();
  }
}

You need to also add below code to App.xaml.h:

ref class App sealed (Visual Studio can add it automatically): { protected: virtual void OnLaunched(Windows::ApplicationModel::Activation::LaunchActivatedEventArgs^ e) override;

    void App_BackRequested(Platform::Object ^ sender, Windows::UI::Core::BackRequestedEventArgs ^ e);

internal:
    App();

private:
    void OnSuspending(Platform::Object^ sender, Windows::ApplicationModel::SuspendingEventArgs^ e);
    void OnNavigationFailed(Platform::Object ^sender, Windows::UI::Xaml::Navigation::NavigationFailedEventArgs ^e);
};

Hope this will help you.

Upvotes: 1

user5773230
user5773230

Reputation:

I managed to get it right somehow, the first code was put in App.xaml code-behind file under the "OnLaunched" event. The second code was put in the last of the above file and it's forward declaration was put in the header file.

Upvotes: 0

Related Questions