如何在WP7中使用MVVM检测Pivot View?

Edw*_*ard 7 c# silverlight mvvm windows-phone-7 mvvm-light

基本上我在我的WP7应用程序中有一个包含3个视图的数据透视控件.在每个视图中,我正在调用我运行的3种不同Web服务中的1种.我想要做的只是在导航到特定视图时调用服务.

使用后面的代码非常简单,因为您所做的只是使用带有switch语句的选定索引,您可以相应地触发某些方法.有关如何从视图模型中实现此目的的任何想法?

注意:我正在使用MVVM Light.

更新:这是我通常使用的代码:

private void PivotItem_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        int currentPivot = ResultsPivot.SelectedIndex;
        switch (currentPivot)
        {
            case 0:
                //Fire Method 1
                break;
            case 1:
                //Fire Method 2
                break;
            case 2:
                //Fire Method 3
                break;
            default:
                //Fire default method
                break;
        }
    }
Run Code Online (Sandbox Code Playgroud)

Ric*_*key 5

MVVMLight的标准方法是将视图模型拆分为数据和命令.大多数事情你使用数据绑定相关,属性等,但命令实际上做了一些事情.

在这种情况下,您所谓的"Fire方法1"是一种普通方法,它符合您必须转换为命令的模式.如果你已经有命令,你知道我在说什么.

SelectionChanged你在MVVMLight中使用代码隐藏连接的事件的粘合剂EventToCommand是一个XAML片段,它放在XAML中,与枢轴项而不是事件处理程序.

所以这就是模式:EventToCommand是将XAML事件连接到视图模型命令而不需要任何代码隐藏的关键.最好的办法是使用MVVMLight示例查看EventToCommand工作原理,因为有很多方法可以使用它.

但这是一个简单的版本:

<controls:PivotItem Name="pivotItem">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="SelectionChanged">
            <cmd:EventToCommand Command="{Binding SelectServiceCommand}"
                                CommandParameter="{Binding SelectedIndex, ElementName=pivotItem}"/>
        </i:EventTrigger>
        <!-- other stuff  -->
    </i:Interaction.Triggers>
</controls:PivotItem>
Run Code Online (Sandbox Code Playgroud)

并且要使这项工作SelectServiceCommand必须实际存在于视图模型中,并且它必须采用参数并为0,1,2,3等做正确的事情.