使用 MVVM Light 在 WP 8.1 中导航
Navigation in WP 8.1 with MVVM Light
我可以导航到 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
我可以导航到 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