1

ページが読み込まれるたびに (ユーザーが [戻る] ボタンを押したときも)、新しいページ インスタンスを再作成する必要があります。だから私はメソッドをオーバーライドしましたOnBackKeyPress

protected override void OnBackKeyPress(CancelEventArgs e)
{
    base.OnBackKeyPress(e);
    if (NavigationService.CanGoBack) {
        e.Cancel = true;
        var j = NavigationService.RemoveBackEntry();
        NavigationService.Navigate(j.Source);
        NavigationService.RemoveBackEntry();
    }
}

CustomMessageBox問題は、ユーザーが [戻る] ボタンを押してダイアログを閉じるときに大文字と小文字を区別できないことです。どうすれば確認できますか?または、履歴状態に戻るときにページ インスタンスを強制的に再作成する方法はありますか?

4

2 に答える 2

0

Ha, in the near thread, i have opposite question :)

What about MessageBox - it depends, which one are you using. It can be custom message box, for example. Anyway, try to check MessageBox.IsOpened (or alternative for your MessageBox) in your OnBackKeyPress().

Another solution is to use OnNavigatedTo() of the page you want to be new each time.

Third solution: in case you works with Mvvm Light, add some unique id in ViewModel getter, like

public MyViewModel MyViewModel
    {
        get
        {
            return ServiceLocator.Current.GetInstance<MyViewModel>((++Uid).ToString());
        }
    }

This would force to recreate new ViewModel each time, so you'd have different instance of VM, so you would have another data on the View.

于 2013-10-25T00:08:19.140 に答える
0

ページ インスタンスを再作成する必要があるのはなぜですか? 表示するデータを単純に再読み込みしようとしている場合は、データ読み込みロジックを OnNavigatedTo() に入れてみませんか?

それがあなたが実際に達成しようとしているものであると仮定して、このようなことを試してください...

public partial class MainPage : PhoneApplicationPage
{
    // Constructor
    public MainPage()
    {
        InitializeComponent();
        // don't do your data loading here.  This will only be called on page creation.
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        LoadData();
        base.OnNavigatedTo(e);
    }

    MyViewModel model;

    async void LoadData()
    {
        model = new MyViewModel();
        await model.LoadDataAsync();
    }
}

ページの最初の構築時と戻るキー ナビゲーションで実行する必要がある特定のロジックもある場合は、OnNavigatedTo に渡される NavigationEventArgs オブジェクトの NavigationMode プロパティを確認します。

if(e.NavigationMode == NavigationMode.New)
{
    //do what you need to do specifically for a new page instance
}
if (e.NavigationMode == NavigationMode.Back)
{
    // do anything specific for back navigation here.
}
于 2013-10-25T08:08:10.247 に答える