2016-02-26 59 views
1

我已經實現了以下模式與MVVM光: Calling ViewModel methods in response to Page navigation events using MVVM Light in WinRTMVVM後退按鈕:頁面和視圖模型失去命令結合

我已經使用這個具有:How to handle the back button on WP 8.1 using MVVM light?

private static INavigationService CreateNavigationService() 
{ 
    var navigationService = new NavigationService(); 
    navigationService.Configure("Details", typeof(DetailsPage)); 
    navigationService.Configure("Chart", typeof(ChartPage)); 

    // Handle back button 
    HardwareButtons.BackPressed += (sender, args) => { 
     navigationService.GoBack(); 
     args.Handled = true; 
    }; 

    return navigationService; 
} 

Navagtion的詳細信息頁面和/或圖表頁面起作用,後退按鈕也起作用。但之後,我無法再導航到其中一個頁面。

它看起來像MainPage和它的ViewModel不重新加載(緩存?)。所以他們失去了約束力。 RelayCommand沒有設置,所以導航不可能了。

任何人都可以幫助我嗎?

編輯:找到了解決辦法:)

最好的問候,裏克

回答

2

從改變了代碼的視圖模型:

public MainViewModel(INavigationService navigationService) 
{ 
    _navigationService = navigationService; 

    DetailsCommand = new RelayCommand(() => 
    { 
     navigationService.NavigateTo("Details", "My data"); 
    }); 
} 

要:

public MainViewModel(INavigationService navigationService) 
{ 
    _navigationService = navigationService; 

    DetailsCommand = new RelayCommand(() => 
    { 
     _navigationService.NavigateTo("Details", "My data"); 
    }); 
} 

和導航工程現在來回。