16

Xaml ページにフレームがあります。

フレーム内をナビゲートするための backButton イベントを作成しようとしています。

だから私はこのコードを使用しようとしました

public MainPage(){
    this.InitializeComponent();
    if(Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons")) {
        Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;
    }
}
private void HardwareButtons_BackPressed(object sender,BackPressedEventArgs e) {
    if(insideFrame.CanGoBack())insideFrame.GoBack();
    else  Application.Current.Exit();
}

しかし、電話ではHardwareButtons_BackPressedイベントを行った後、アプリケーションを閉じます。

MainPage でデフォルトの戻るボタンの動作を実行しているようです...

どうすれば修正できますか?また、Windows10 では、戻るナビゲーションを処理するための新しいイベントが追加されますか?


[アップデート]

SystemNavigationManagerではなく、Windows 10 で使用する方がよいことがわかりましたInput.HardwareButtons.BackPressed

SystemNavigationManager currentView = SystemNavigationManager.GetForCurrentView();
4

5 に答える 5

24

Windows 10 (UWP) には、ナビゲーション目的でのみ名前空間にSystemNavigationManagerが含まれています。Windows.UI.Core

SystemNavigationManagerの一部であるためWindows Universal Platform、モバイルや PC を含む、Windows 10 で実行されているすべてのデバイス ファミリでサポートされています。

単一ページの場合


単一ページのナビゲーションを処理したいだけの場合。次の手順に従ってください

ステップ1。名前空間を使用Windows.UI.Core

using Windows.UI.Core;

ステップ 2.現在のビューのバック リクエスト イベントを登録します。これに最適な場所は、 の後のクラスのメイン コンストラクターですInitializeComponent()

public MainPage()
{
    this.InitializeComponent();
    //register back request event for current view
    SystemNavigationManager.GetForCurrentView().BackRequested += MainPage_BackRequested;
}

ステップ 3. BackRequested イベントを処理する

private void Food_BackRequested(object sender, BackRequestedEventArgs e)
{
    if (Frame.CanGoBack)
    {
        Frame.GoBack();
        e.Handled = true;
    }
}

1ヶ所での単発申込の場合rootFrame


すべてのビューのすべての戻るボタンを処理するのに最適な場所はApp.xaml.cs

ステップ1。名前空間を使用Windows.UI.Core

using Windows.UI.Core;

ステップ 2.現在のビューのバック リクエスト イベントを登録します。これに最適な場所はOnLaunched直前ですWindow.Current.Activate

protected override void OnLaunched(LaunchActivatedEventArgs e)
{
    ...
    SystemNavigationManager.GetForCurrentView().BackRequested += OnBackRequested;
    Window.Current.Activate();
}
    

ステップ 3. BackRequested イベントを処理する

private void OnBackRequested(object sender, BackRequestedEventArgs e)
{
    Frame rootFrame = Window.Current.Content as Frame;
    if (rootFrame.CanGoBack)
    {
        rootFrame.GoBack();
        e.Handled = true;
    }
}

参照- UWP で押された [戻る] ボタンを処理する

これが誰かに役立つことを願っています!

于 2016-03-08T19:04:55.343 に答える
0

次の手順を実行します:

  • 以下のように、ページに 2 つのグローバル変数を追加します。

    private NavigationHelper navigationHelper;
    private RelayCommand _GoBackCommand;
    
  • 次に、特定のページのコンストラクターに以下のコードを追加します。

        // below code is to override the back navigation
        // hardware back button press event from navigationHelper
        _GoBackCommand = new RelayCommand
            (
                () => this.CheckGoBack(),
                () => this.CanCheckGoBack()
            );
    
        navigationHelper.GoBackCommand = _GoBackCommand;
        // ---------
    
  • 次に、コンストラクターで宣言したばかりの両方のメソッドを追加します。

    private bool CanCheckGoBack()
    {
        // this should be always true to make sure the app handles back buton manually.
        return true;
    }
    
    private void CheckGoBack()
    {
        // this will be execute when back button will be pressed
    }
    

ps。- このために、新しいページを追加するBasicPage代わりに使用する必要がある場合があります。BlankPage

これが役立つことを願っています..!

于 2015-08-05T12:28:17.940 に答える
-1

これは、app.xaml.cs ではなく、ページに HardwareButtons_BackPressed を追加したためだと思います。

app.xaml.cs で:

public App()
{
    this.InitializeComponent();
    this.Suspending += this.OnSuspending;

    HardwareButtons.BackPressed += HardwareButtons_BackPressed;
}

void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
    Frame rootFrame = Window.Current.Content as Frame;

    if (rootFrame != null && rootFrame.CanGoBack)
    {
        e.Handled = true;
        rootFrame.GoBack();
    }
}

これで、携帯電話の戻るボタンはどのページでも機能します。

そして、任意のページに戻る特定のボタンを追加する場合:

特定のページ (または必要に応じて各ページ) で:

public void btn_return_Tapped(object sender, TappedRoutedEventArgs e)
        {
            Frame rootFrame = Window.Current.Content as Frame;

            if (rootFrame != null && rootFrame.CanGoBack)
            {
                e.Handled = true;
                rootFrame.GoBack();
            }
        }

ソース: http://windowsapptutorials.com/tips/general-tips/handling-the-back-button-in-a-windows-phone-8-1-app/

于 2015-08-05T12:24:32.770 に答える