2016-06-14 10 views
0

私はWindows 10 UWPアプリを初めて使用しています。現在、私はアプリでさまざまなナビゲーション技術を試しています。私はUWPアプリケーションのバックボタン機能を探しましたが、「提供されたコード」をどこに置いて実行する必要があるのか​​わからないようです。私自身もそれをやろうとしましたが、識別子が見つからないというVisual Studioのさまざまなエラーが発生します。UWPバックボタンとイベントハンドラ - C++

最初のコードは次のとおりです。

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

コードの2作品は次のとおりです。

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(); 
     } 
    } 

この問題で私を助けて、どのように私は私のUWPのアプリケーションでこのコードを実装してください教えてください。おかげ

出典:私は右何とかそれを得るために管理https://msdn.microsoft.com/en-us/library/windows/apps/mt465734.aspx

+0

アプリが「ビルド」されているときにセットアップ中に最初のビットを実行する必要があり、バックボタンが実際にクリックされたときに2番目のビットを実行する必要があります。 – ocket8888

答えて

0

それは以下のようにする必要があります:

これはApp.xaml.cppクラスのOnLaunchedメソッドです:OnLaunched下

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); 
} 

今:

ちょうどその終了前に、あなたが最初のフラグメントを貼り付ける必要がありますこのハンドラを貼り付けてください:

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(); 
    } 
} 

あなたも追加する必要がありますApp.xaml.hの低いコード:

refのクラス密封されたアプリケーション(Visual Studioが自動的にそれを追加することができます): { が保護:OnLaunched 仮想のボイド(Windowsの:: ApplicationModel ::アクティベーション:: LaunchActivatedEventArgs^e)のオーバーライド;

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); 
}; 

希望すると、これが役立ちます。

+0

はい、これは既に行っています。 –

0

、最初のコードは「OnLaunched」イベントの下App.xamlコードビハインドファイルに入れました。 2番目のコードは上記のファイルの最後に置かれており、前方宣言はヘッダーファイルに入れられています。

関連する問題