2011-05-17 52 views
7

基本上我有一个包含3个视图的WP7应用程序中的透视控件。在每个视图中,我都会调用我运行的3个不同Web服务中的1个。我试图做的只是当他们导航到该特定视图时才调用该服务。如何在WP7中使用MVVM检测数据透视视图?

使用后面的代码非常简单,因为您所做的只是使用选择的索引和switch语句,并且可以相应地触发某些方法。有关如何从视图模型完成此任何想法?

注意:我正在使用MVVM Light。

UPDATE:这里是我的代码,我通常会使用:

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; 
     } 
    } 
+0

显示您试图转换的代码。 – 2011-05-17 02:52:39

+0

@Rick我添加了我通常会使用的代码。 – loyalpenguin 2011-05-17 02:55:53

回答

5

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

在这种情况下,你所称的“火法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> 

,并使这项工作SelectServiceCommand有实际存在的视图模型,它必须采取一个参数,并做正确的事情为0 ,1,2,3等

2

我没有使用MVVM光直接,但你应该能够选择的指标/项目结合于一个属性查看模型。当这个属性改变时,你可以做你的开关。

0

我喜欢在像这样的情况下让事情变得简单,在这些情况下,View需要通知ViewModel某些如此微不足道的事情发生了变化(例如:一个简单的combobox选择更改与视图状态无关(即ViewModel))。

对于您的具体情况,在您的switch语句中,只需在您的ViewModel中调用公共方法即可。 如何获取视图模型参考?你可以通过视图的DataContext获取。所以现在你的视图可以调用viewModel中的公共方法(和属性)。

对于重要的事情坚持DataBinding。否则,只需直接拨打。节省了很多时间和麻烦。

0

我得到了我离开的pivotItem的索引,而不是我要去的PivotItem! 。

使用此:

<controls:Pivot x:Name="pivMain" Title="{Binding AppName}" > 
     <Custom:Interaction.Triggers> 
      <Custom:EventTrigger EventName="SelectionChanged"> 
        <cmd:EventToCommand Command="{Binding SelectServiceCommand}"   
        CommandParameter="{Binding ElementName=pivMain, Path=SelectedIndex}"/> 
      </Custom:EventTrigger> 
     </Custom:Interaction.Triggers> 
4

这可以通过以下方式在您的视图模型来解决

<controls:Pivot x:Name="PivotControl" FontSize="18" > 
     <Custom:Interaction.Triggers> 
      <Custom:EventTrigger EventName="SelectionChanged"> 
       <GalaSoft_MvvmLight_Command:EventToCommand x:Name="VideoPivotClicked" 
                  Command="{Binding VideoPivotClicked, Mode=OneWay}" PassEventArgsToCommand="True" /> 
      </Custom:EventTrigger> 
     </Custom:Interaction.Triggers> 

然后添加这个

 public RelayCommand<SelectionChangedEventArgs> VideoPivotClicked 
    { 
     get; 
     private set; 
    } 

VideoPivotClicked = new RelayCommand<SelectionChangedEventArgs>(arg => 
                 { 
                  PivotItem pivotItem = arg.AddedItems[0] as PivotItem; 
                  Pivot pivot = pivotItem.Parent as Pivot; 
                  Debug.WriteLine(pivot.SelectedIndex); 
                 } 
     ); 

你不会得到PivotItem是你会!而不是你要离开的人。