2015-09-18 78 views
0

我可以導航到DetailPage.xaml以顯示更多信息,但無法將對象傳遞給DetailVieModel,並且當我按下應用程序關閉按鈕時。使用MVVM Light在WP 8.1中導航

這裏是我的代碼:

在ViewModelLocator

public ViewModelLocator() 
    { 
     ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default); 

     var navigationService = this.CreateNavigationService(); 
     SimpleIoc.Default.Register<INavigationService>(() => navigationService); 

     SimpleIoc.Default.Register<MainViewModel>(); 
     SimpleIoc.Default.Register<DataService>(); 
     SimpleIoc.Default.Register<DetailViewModel>(true); 
    } 

的導航,我只有一個就目前而言,這似乎工作becouse我可以導航到DetailPage.xaml。

private INavigationService CreateNavigationService() 
    { 
     var navigationService = new NavigationService(); 
     navigationService.Configure("DetailPage", typeof(DetailPage)); 

     return navigationService; 
    } 

這是我在MainViewModel做:

public RelayCommand ShowDetailPage 
    { 
     get 
     { 
      return new RelayCommand(() => 
      { 
       _INavigationService.NavigateTo("DetailPage"); 
      }); 
     } 
    } 

這是我想傳遞給DetailViewModel對象

private Info _SelectedObject; 
    public Info SelectedObject 
    { 
     get 
     { 
      return _SelectedObject; 
     } 
     set 
     { 
      _SelectedObject = value; 
      if (value != null) 
      { 
       Messenger.Default.Send<Info>(value); 
       _SelectedObject = null; 
      } 
      RaisePropertyChanged("SelectedObject"); 
     } 
    } 

這是我做的觸發命令on MainPage.xaml

<ListView Grid.Row="1" 
           ItemsSource="{Binding ObjectsList}"> 
         <i:Interaction.Behaviors> 
          <core:EventTriggerBehavior EventName="Tapped"> 
           <core:InvokeCommandAction Command="{Binding ShowDetailPage}" 
                  CommandParameter="{Binding SelectedObject}" /> 
          </core:EventTriggerBehavior> 
         </i:Interaction.Behaviors> 

而finaly在DetailViewModel我要求與Messenger.Register的SelectedObject ...

Messenger.Default.Register<Info>(this, (selectedinfo) => 
     { 
      _SelectedInfo = selectedinfo; 
     }); 

回答

1

要傳遞的對象,您可以使用:

_navigationService.NavigateTo("DetailPage", _SelectedInfo); 

要處理後退導航,只需在RootFrame_FirstNavigated以下事件處理器。

#if WINDOWS_PHONE_APP 
      Windows.Phone.UI.Input.HardwareButtons.BackPressed += (s, args) => 
      { 
       if (!rootFrame.CanGoBack) 
       { 
        return; 
       } 

       // Allow back navigation using Back button 
       args.Handled = true; 
       rootFrame.GoBack(); 
      };  
#endif 
+0

對象傳遞不起作用:/ – Bayern

+0

如何檢索另一頁中傳遞的參數? –

+0

啊嘿,我忘了註冊我的DetailViewModel ...現在一切正常,謝謝你! – Bayern