0

I have a problem while navigating pages in my WP8.1 Silverlight application. When I was using standard CodeBehind approach everything was simple, because implementing OnNavigatedTo was enough.

Now I want to handle OnNavigatedTo and OnNavigatedFrom events in ViewModel. I started with standard RelayCommand approach.

ViewModel code:

private RelayCommand initializeVideoRecorderCommand;
public RelayCommand InitializeVideoRecorderCommand
{
    get
    {
        if (initializeVideoRecorderCommand == null)
        {
            initializeVideoRecorderCommand = new RelayCommand(InitializeVideoRecorder);
        }
        return initializeVideoRecorderCommand;
    }
}
private void InitializeVideoRecorder()
{
    //Initialization code goes here
}

InitializeVideoRecorderCommand should now be bind to OnNavigatedTo event. I managed to do this with help from Vovich (I only had to change xmlns:cmd namespace, because Visual coudn't find GalaSoft.MvvmLight.Extras assembly).

XAML code:

<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 function is triggered when I navigate to CameraView.

But now I don't know how to override OnNavigatedFrom. I thought that little change to XAML code would do the trick.

XAML code:

<i:EventTrigger EventName="OnNavigatedTo">

But this doesn't trigger event at all.

How can I use properly navigation events with this approach?

Community
  • 1
  • 1
Sebastian Budka
  • 396
  • 1
  • 8
  • 19

0 Answers0