2014-10-16 43 views
1

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

我看到的所有例子都绑定了一个按钮的RelayCommand。

如何将它绑定到页面加载事件?我看到一些使用EventToCommand的例子。但我正在使用Window phone 8.1,我不认为自己有某些文章的行为。

回答

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是在你的虚拟机的命令,和该页面的DataContext设置为您的虚拟机。