2017-08-30 3 views
0

私は次のセットアップを持っています:MainPage xaml-viewとSettingPage xaml-view。 SettingPage xaml-viewでは、ウィンドウのタイトルバーにあるBackボタンを有効にし、BackRequestedEventArgsを追加しました。 (さらにDX12のxamlページがありますが、ナビゲーションにはまだ関与していませんので、初期化されることはありません)UWP - プラットフォーム:: DisconnectedExceptionページ間を移動中に

私の問題は、MainPageにある設定と呼ばれるflyoutitemをクリックすると、私はSettingPageに移動します。タイトルバーにはバックボタンが表示され、クリックするとMainPageに戻ります。今度はもう一度やります:設定をクリックし、SettingPageにナビゲートします。今、私がバックボタンをクリックするか、ウィンドウを閉じるとアプリケーションがクラッシュし、以下の例外が表示されます。

プラットフォーム:: DisconnectedException^at 0x046BED80。 HRESULT:0x80010108

質問:どうすれば修正できますか?ここで

はそれのために私のコードです:

メインページナビゲーション:

void MainPage::MenuFlyoutItemSettings_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e) 
    { 

    this->Frame->Navigate(Windows::UI::Xaml::Interop::TypeName(SettingsPage::typeid)); 

    } 

SettingsPage:

// in Constructor 
Windows::UI::Core::SystemNavigationManager::GetForCurrentView()->AppViewBackButtonVisibility = Windows::UI::Core::AppViewBackButtonVisibility::Visible; 
Windows::UI::Core::SystemNavigationManager::GetForCurrentView()-> 
    BackRequested += ref new Windows::Foundation::EventHandler< 
    Windows::UI::Core::BackRequestedEventArgs^>(
     this, &SettingsPage::App_BackRequested); 



void SettingsPage::App_BackRequested(
    Platform::Object^ sender, 
    Windows::UI::Core::BackRequestedEventArgs^ e) 
{ 
    Windows::UI::Xaml::Controls::Frame^ rootFrame = dynamic_cast<Windows::UI::Xaml::Controls::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(); 
    } 
} 

さらに、両方の方法は、手動で私が追加したonSuspendingとonResumingハンドラを持っていますが、それらはどちらも空:

//in constructor 

    Application::Current->Suspending += ref new SuspendingEventHandler(this, &SettingsPage::OnSuspending); 
    Application::Current->Resuming += ref new EventHandler<Object^>(this, &SettingsPage::OnResuming); 


void SettingsPage::OnSuspending(Object^ sender, SuspendingEventArgs^ e) { 


} 

void SettingsPage::OnResuming(Object^ sender, Object^ e) { 


} 

注:バックボタンコード全体を削除すると、この例外でアプリケーションがクラッシュすることはないため、このコードではエラーと思われます。

EDIT 2017年9月4日:

Sunteen呉に取り組ん後 - 以下からMSFTの答えは私がすべての戻るボタンコードを削除しても、私はできるだけ早く私が入ると、この例外を取得することを実現しました最初に設定ページを開き、アプリケーションを閉じます。だからここに私が説明した例外を取得しています私の現在のシナリオは次のとおりです。

私は、ナビゲーションのために今持っているだけのコード:(カスタムsettingsbutton中)

メインページ:

this->Frame->Navigate(Windows::UI::Xaml::Interop::TypeName(SettingsPage::typeid)); 

SettingsPage( )カスタム戻るボタンで:

this->Frame->Navigate(Windows::UI::Xaml::Interop::TypeName(MainPage::typeid)); 

だから、最初の時間の後、私は私だけのシャットダウン番目の場合で説明例外を取得settingsbuttonを押してsettingspageに移動しますe app(赤いxをクリックするか、デバッガを止めても同じです)。ナビゲーションはうまくいきますが、私が望む限りページ間を入れ替えることができ、アプリを実行している間は例外が発生しません。

FINAL ANSWER 2017年9月6日: Sunteen呉を組み合わせる

- 上記

Application::Current->Suspending += ref new SuspendingEventHandler(this, &SettingsPage::OnSuspending); 
Application::Current->Resuming += ref new EventHandler<Object^>(this, &SettingsPage::OnResuming); 

ハンドラを言及を削除するとMSFTの答えは私のためのソリューションです。 DisconnectedExceptionはなく、Back-Button-Logicも機能しています。

答えて

0

プラットフォーム:: DisconnectedException^at 0x046BED80。 HRESULT:0x80010108

の問題が実際に発生しています。サイクルの問題と解決方法についてはWeak references and breaking cycles (C++/CX)を参照してください。 BackRequestedイベントハンドルを購読すると、サイクルの問題が発生しました。記事から

WeakReference wr(this); 
Windows::UI::Core::SystemNavigationManager::GetForCurrentView()-> 
    BackRequested += ref new Windows::Foundation::EventHandler< 
    Windows::UI::Core::BackRequestedEventArgs^>([wr](
     Object^ sender, Windows::UI::Core::BackRequestedEventArgs^ e) 
{ 
    SettingsPage^ c = wr.Resolve<SettingsPage>(); 
    if (c != nullptr) 
    { 
     Windows::UI::Xaml::Controls::Frame^ rootFrame = dynamic_cast<Windows::UI::Xaml::Controls::Frame^>(Window::Current->Content); 
     if (rootFrame == nullptr) 
      return; 
     if (rootFrame->CanGoBack && e->Handled == false) 
     { 
      e->Handled = true; 
      rootFrame->GoBack(); 
     } 
    } 
    else 
    { 
     throw ref new DisconnectedException(); 
    } 
}); 

、イベントハンドラはDisconnectedExceptionをスローした場合、それは加入者リストからハンドラを削除するには、イベントが発生します。WeakReferenceを使用すると、問題があります。これを解決するには、バックリクエスト後にサブスクライバリストからイベントハンドルを削除することができます。次のコードスニペットは、削除方法を示しています。

Windows::Foundation::EventRegistrationToken cookie; 
SettingsPage::SettingsPage() 
{ 
    InitializeComponent(); 
    ... 
    cookie = Windows::UI::Core::SystemNavigationManager::GetForCurrentView()-> 
     BackRequested += ref new Windows::Foundation::EventHandler< 
     Windows::UI::Core::BackRequestedEventArgs^>(
      this, &SettingsPage::App_BackRequested);  
} 

void SettingsPage::App_BackRequested(
    Platform::Object^ sender, 
    Windows::UI::Core::BackRequestedEventArgs^ e) 
{ 
    ... 
    Windows::UI::Core::SystemNavigationManager::GetForCurrentView()-> 
     BackRequested -= cookie; 
} 

また、私はあなたがこの問題を回避するためにApp.xaml.cpp内でこのイベントをサブスクライブをお勧めします。

void App::OnLaunched(Windows::ApplicationModel::Activation::LaunchActivatedEventArgs^ e) 
{ 
    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) 
    { 
    ... 
    } 
    else 
    { 
     ... 
    }  
    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) 
{ 
    ... 
} 

詳細あなたがBackButton公式のサンプルを参照することができますを次のようにあなたはOnLaunchedの内側にそれを購読することができます。

+0

ありがとうございます。私が設定した設定にナビゲートすると、そのあとでアプリケーションを終了すると、まだ「Platform :: DisconnectedException^0x047FF328。HRESULT:0x80010108」というメッセージが表示されます。 (ただし、もう2番目のバックの後ではなく、バックナビゲーション後にはもう一度)。私はSettingsPage.xaml.cppであなたの "クッキー"スニペットを使用しました(クッキー= ...を介してハンドラを追加し、AppBackRequestedであなたの - =クッキー行を追加する質問から私のコードを使用しました)、私はアプリケーションで最後のスニペットを使用しました。 xaml.cpp。 – David

+0

@David、 'App.xaml.cpp'でスニペットを試してみませんか?(設定ページ内のコードを削除してください) –

+0

私は設定ページの関連するコードをすべて削除しました.App.xaml.cppのonLaunched()にcookie =(...)を書き、自分の質問に加えてAppBackRequestedメソッドコードを書きました。 - = App.xaml.cppのクッキー。今度は初めてセカンドタイムに戻ることができます。設定ページに移動して、メインページに戻ることはできません。バックボタンをクリックすると何も起こりません。さらに、アプリケーションをシャットダウンした後の例外は、以前の私のコメントのようにまだそこにあります。 – David

関連する問題