简体   繁体   English

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

[英]How to detect Pivot View using MVVM in WP7?

basically I have a pivot control in my WP7 app that contains 3 views. 基本上我在我的WP7应用程序中有一个包含3个视图的数据透视控件。 On each view I'm calling 1 of my 3 different web services that I run. 在每个视图中,我正在调用我运行的3种不同Web服务中的1种。 What I'm trying to do is call the service only when they navigate to that particular view. 我想要做的只是在导航到特定视图时调用服务。

It's pretty simple using the code behind because all you do is use selected index with a switch statement and you can fire certain methods accordingly. 使用后面的代码非常简单,因为您所做的只是使用带有switch语句的选定索引,您可以相应地触发某些方法。 Any idea on how to accomplish this from a view model? 有关如何从视图模型中实现此目的的任何想法?

NOTE: I'm using MVVM Light. 注意:我正在使用MVVM Light。

UPDATE: Here's my code that I would normally use: 更新:这是我通常使用的代码:

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

The standard approach with MVVMLight is the split your view-model into data and commands. MVVMLight的标准方法是将视图模型拆分为数据和命令。 Most things you use databinding related, properties, etc. but commands actually do something. 大多数事情你使用数据绑定相关,属性等,但命令实际上做了一些事情。

In this case what you are calling "Fire Method 1" is an ordinary method that to conform to the pattern you have to convert to a command. 在这种情况下,您所谓的“Fire方法1”是一种普通方法,它符合您必须转换为命令的模式。 If you already have commands you know what I am talking about. 如果你已经有命令,你知道我在说什么。

The glue for events like SelectionChanged that you would have wired up with code-behind in MVVMLight is EventToCommand which is a XAML fragment that you put in the XAML with the pivot item instead of in the event hander. 您将在MVVMLight中使用代码隐藏连接的SelectionChanged事件的粘合剂是EventToCommand ,它是一个XAML片段,您使用透视项而不是在事件处理程序中放入XAML。

So this is the pattern: EventToCommand is your key to hooking up XAML events to view-model commands without any code-behind. 所以这就是模式: EventToCommand是将XAML事件连接到视图模型命令而不需要任何代码隐藏的关键。 The best thing to do is use the MVVMLight samples to see how EventToCommand works because there are lots of ways to use it. 最好的办法是使用MVVMLight示例来查看EventToCommand工作原理,因为有很多方法可以使用它。

But here is the bare-bones version: 但这是一个简单的版本:

<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>

and to make this work the SelectServiceCommand has to actually exist in the view-model and it has to take a parameter and do the right thing for 0, 1, 2, 3, etc. 为了使这项工作, SelectServiceCommand必须实际存在于视图模型中,它必须采用参数并为0,1,2,3等做正确的事情。

This can be solved in the following way 这可以通过以下方式解决

<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>

Then in your viewmodel you add this 然后在你的viewmodel中添加它

      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);
                                                       }
          );

You will not get the PivotItem that you are going to! 你不会得到你要去的PivotItem! and not the one you are leaving. 而不是你要离开的人。

I haven't used MVVM Light directly, but you should be able to bind the selected index / item to a property on the view model. 我没有直接使用MVVM Light,但您应该能够将选定的索引/项绑定到视图模型上的属性。 When that property is changed you could do your switch. 当该属性发生变化时,您可以进行切换。

I like to keep things simple in situations like these where the View needs to notify the ViewModel that something that is so trivial changed (for example: A trivial combobox selection change that really has nothing to do with the view state (ie ViewModel)). 我喜欢在这样的情况下保持简单,其中View需要通知ViewModel一些如此微不足道的变化(例如:一个简单的组合框选择更改,它实际上与视图状态无关(即ViewModel))。

For your specific case, in your switch statement, just call a public method in your ViewModel. 对于您的特定情况,在switch语句中,只需在ViewModel中调用公共方法即可。 How to get the viewmodel reference? 如何获取viewmodel引用? You can obtain that by the view's DataContext. 您可以通过视图的DataContext获取它。 So now your views can call public methods (and properties) within your viewModel. 所以现在你的视图可以调用viewModel中的公共方法(和属性)。

For significant things stick with DataBinding. 对于重要的事情坚持使用DataBinding。 otherwise, just call directly. 否则,直接打电话。 Saves so much time and hassle. 节省了这么多时间和麻烦。

I get the index for the pivotItem that I'm leaving, not the PivotItem that I'm going to! 我得到了我要离开的pivotItem的索引,而不是我要去的PivotItem! .

Using this: 使用这个:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM