导航在WP 8.1与MVVM光
本文关键字:MVVM WP 导航 | 更新日期: 2023-09-27 18:13:22
我可以导航到DetailPage。xaml来显示更多的信息,但是我不能将对象传递给DetailVieModel,当我按回应用程序关闭。
下面是我的代码:在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);
}
导航,我现在只有一个,这似乎可以工作,因为我可以导航到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");
}
}
下面是我在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>
最后在DetailViewModel上,我用Messenger.Register询问SelectedObject…
Messenger.Default.Register<Info>(this, (selectedinfo) =>
{
_SelectedInfo = selectedinfo;
});
传递对象可以使用:
_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