将mvvm light Relay命令绑定到页面加载事件

时间:2014-10-16 19:53:35

标签: mvvm windows-phone-8.1 mvvm-light

我正在使用Window phone 8.1。 我有一个RelayCommand命令执行一些方法异步。 我想知道如何将页面加载事件从页面绑定到视图模型中的RelayCommand?

我看到的所有示例都是将RelayCommand绑定到Button。

如何将其绑定到页面加载事件?我看到一些示例使用EventToCommand。但我正在使用Window phone 8.1,我不会'我觉得我的行为就像我看到的一些文章。

1 个答案:

答案 0 :(得分:3)

确保将行为扩展名添加到参考文献

Behaviors SDK reference

然后定义事件触发器调用命令:

<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

其中MyCommandInTheViewModel是VM中的命令,并且页面的DataContext设置为您的VM。