Window Phone 7和MVVM,页面的Loaded事件



嗨,在以这种方式实例化视图模型后,我在使用MVVM模式时遇到了问题:

<phone:PhoneApplicationPage.Resources>
    <local:DetailVM x:Key="DetailVM"/>
</phone:PhoneApplicationPage.Resources>

如何知道此页面何时加载?

您可以使用Blend SDK并添加一个事件触发器,该触发器可以在视图模型中触发命令。

包括

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

在xmlns includes中,然后为Loaded事件添加一个触发器。

<i:Interaction.Triggers>
    <i:EventTrigger EventName="Loaded">
        <i:InvokeCommandAction Command="{Binding LoadCommand}" />
    </i:EventTrigger>
</i:Interaction.Triggers>

LoadCommand只是一个返回ICommand的属性。当然,您应该将页面的DataContext设置为视图模型,或者将绑定的源设置为资源中的源。

最新更新