我在WP8.1 Silverlight应用程序中导航页面时遇到问题。当我使用标准的CodeBehind方法时,一切都很简单,因为实现OnNavigatedTo
就足够了。
现在我想在ViewModel中处理OnNavigatedTo
和OnNavigatedFrom
个事件。我从标准RelayCommand
方法开始。
ViewModel代码:
private RelayCommand initializeVideoRecorderCommand;
public RelayCommand InitializeVideoRecorderCommand
{
get
{
if (initializeVideoRecorderCommand == null)
{
initializeVideoRecorderCommand = new RelayCommand(InitializeVideoRecorder);
}
return initializeVideoRecorderCommand;
}
}
private void InitializeVideoRecorder()
{
//Initialization code goes here
}
InitializeVideoRecorderCommand
现在应该绑定到OnNavigatedTo
事件。我设法在Vovich的帮助下完成了这项工作(我只需要更改xmlns:cmd
命名空间,因为Visual coudn找不到GalaSoft.MvvmLight.Extras
程序集。
XAML代码:
<phone:PhoneApplicationPage
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Platform"
DataContext="{Binding Camera, Source={StaticResource Locator}}">
<i:Interaction.Triggers>
<i:EventTrigger>
<cmd:EventToCommand Command="{Binding InitializeVideoRecorderCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<!-- Rest of the UI -->
</phone:PhoneApplicationPage>
导航到InitializeVideoRecorder
时会触发 CameraView
功能。
但现在我不知道如何覆盖OnNavigatedFrom
。我认为对XAML代码的一点改动就可以解决这个问题。
XAML代码:
<i:EventTrigger EventName="OnNavigatedTo">
但这根本不会触发事件。
如何使用此方法正确使用导航事件?