如何在 WP7 中使用 MVVM 检测透视视图?

发布于 2024-11-07 19:46:41 字数 804 浏览 0 评论 0原文

基本上我的 WP7 应用程序中有一个包含 3 个视图的枢轴控件。在每个视图中,我都会调用我运行的 3 个不同 Web 服务中的一个。我想做的是仅当他们导航到该特定视图时才调用该服务。

使用后面的代码非常简单,因为您所做的就是使用带有 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;
        }
    }

basically I have a pivot control in my WP7 app that contains 3 views. On each view I'm calling 1 of my 3 different web services that I run. 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. Any idea on how to accomplish this from a view model?

NOTE: I'm using 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;
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

白衬杉格子梦 2024-11-14 19:46:41

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

在这种情况下,您所说的“Fire Method 1”是一种普通方法,为了符合您必须转换为命令的模式。如果您已经有命令,您就知道我在说什么。

SelectionChanged 之类的事件的粘合剂是 EventToCommand,您可以将其与 MVVMLight 中的代码隐藏连接起来,它是一个 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 等

The standard approach with MVVMLight is the split your view-model into data and commands. 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. 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.

So this is the pattern: EventToCommand is your key to hooking up XAML events to view-model commands without any code-behind. The best thing to do is use the MVVMLight samples to see how EventToCommand works because there are lots of ways to use it.

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.

随心而道 2024-11-14 19:46:41

这可以通过以下方式解决

<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!而不是你要离开的那个人。

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

      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! and not the one you are leaving.

貪欢 2024-11-14 19:46:41

我没有直接使用 MVVM Light,但您应该能够将选定的索引/项目绑定到视图模型上的属性。当该属性更改时,您可以进行切换。

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. When that property is changed you could do your switch.

梦幻的味道 2024-11-14 19:46:41

我喜欢在这种情况下保持简单,其中视图需要通知 ViewModel 一些如此琐碎的更改(例如:与视图状态(即 ViewModel)真正无关的简单组合框选择更改)。

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

对于重要的事情,请坚持使用数据绑定。否则直接打电话就可以了。节省了很多时间和麻烦。

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 (i.e ViewModel)).

For your specific case, in your switch statement, just call a public method in your ViewModel.
How to get the viewmodel reference? You can obtain that by the view's DataContext. So now your views can call public methods (and properties) within your viewModel.

For significant things stick with DataBinding. otherwise, just call directly. Saves so much time and hassle.

单调的奢华 2024-11-14 19:46:41

我得到了我要离开的数据透视项的索引,而不是我要离开的数据透视项的索引! 。

使用这个:

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

I get the index for the pivotItem that I'm leaving, not the PivotItem that I'm going to! .

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文