hap497
hap497

Reputation: 163025

Bind mvvm light Relaycommand to page loading event

I am working on Window phone 8.1. I have a RelayCommand command which executes some method async. I want to know how can I bind a page load event from a page to the RelayCommand in the view model?

All the example I see are binding RelayCommand to a Button.

How can I bind that to page load event? I see some example uses EventToCommand. But I am using Window phone 8.1, I dont' think I have the behaviour stuff some article I saw.

Upvotes: 1

Views: 1180

Answers (1)

Igor Ralic
Igor Ralic

Reputation: 15006

Make sure you add Behaviors extension to your References

Behaviors SDK reference

then define that the event trigger invokes a command:

<Page
    x:Class="App31.PivotPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App31"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:data="using:App31.Data"
    xmlns:i="using:Microsoft.Xaml.Interactivity"
    xmlns:core="using:Microsoft.Xaml.Interactions.Core"
    mc:Ignorable="d">
    <i:Interaction.Behaviors>
        <core:EventTriggerBehavior EventName="Loaded">
            <core:InvokeCommandAction Command="{Binding MyCommandInTheViewModel}" />
        </core:EventTriggerBehavior>
    </i:Interaction.Behaviors>
//....
//.. rest of page code

Where MyCommandInTheViewModel is the command in your VM, and the DataContext of the page is set to your VM.

Upvotes: 3

Related Questions