【问题标题】:Bind mvvm light Relaycommand to page loading event将 mvvm light Relay 命令绑定到页面加载事件
【发布时间】:2014-10-16 19:53:35
【问题描述】:
我正在使用 Window phone 8.1。
我有一个 RelayCommand 命令,它执行一些异步方法。
我想知道如何将页面加载事件从页面绑定到视图模型中的 RelayCommand?
我看到的所有示例都是将 RelayCommand 绑定到按钮。
如何将其绑定到页面加载事件?我看到一些示例使用 EventToCommand。但是我使用的是 Window phone 8.1,我不认为我有我看到的一些文章的行为。
【问题讨论】:
标签:
mvvm
windows-phone-8.1
mvvm-light
【解决方案1】:
确保将行为扩展添加到您的参考文献中
然后定义事件触发器调用命令:
<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。