如何在 WP7 中使用 MVVM 检测透视视图?
基本上我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
MVVMLight 的标准方法是将视图模型拆分为数据和命令。大多数你使用的东西都是与数据绑定相关的、属性等,但命令实际上是做一些事情的。
在这种情况下,您所说的“Fire Method 1”是一种普通方法,为了符合您必须转换为命令的模式。如果您已经有命令,您就知道我在说什么。
SelectionChanged
之类的事件的粘合剂是EventToCommand
,您可以将其与 MVVMLight 中的代码隐藏连接起来,它是一个 XAML 片段,您可以将其与数据透视项一起放入 XAML 中事件处理程序中的。所以这就是模式:
EventToCommand
是将 XAML 事件连接到视图模型命令的关键,没有任何代码隐藏。最好的办法是使用 MVVMLight 示例来了解EventToCommand
的工作原理,因为有很多方法可以使用它。但这是最基本的版本:
为了使这项工作有效,
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 isEventToCommand
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 howEventToCommand
works because there are lots of ways to use it.But here is the bare-bones version:
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.这可以通过以下方式解决
然后在您的视图模型中添加此
您将不会获得您想要的 PivotItem!而不是你要离开的那个人。
This can be solved in the following way
Then in your viewmodel you add this
You will not get the PivotItem that you are going to! and not the one you are leaving.
我没有直接使用 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.
我喜欢在这种情况下保持简单,其中视图需要通知 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.
我得到了我要离开的数据透视项的索引,而不是我要离开的数据透视项的索引! 。
使用这个:
I get the index for the pivotItem that I'm leaving, not the PivotItem that I'm going to! .
Using this: