MVVM Light-使用ViewModelLocator-属性多次击中 [英] MVVM Light - using ViewModelLocator - properties hit multiple times

查看:200
本文介绍了MVVM Light-使用ViewModelLocator-属性多次击中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MVVM Light ViewModelLocator.我有一个名为GlobalViewModelLocator的类,该类与App.Xaml中的资源连接在一起.此类具有一个称为Main的静态属性,该属性返回MainViewModel的实例.

I'm making use of the MVVM Light ViewModelLocator. I have a class called GlobalViewModelLocator which is hooked up in the resources in the App.Xaml. This class has a static property called Main which returns an instance of the MainViewModel.

然后在MainView.Xaml中,将usercontrol的datacontext设置为绑定到此MainViewModel的路径.效果很好-我在MainViewModel构造函数上设置了一个断点,并且它被命中了一次.但是,由于在MainViewModel中控件上的事件触发而在ViewModel中设置的所有属性都被击中了3次.有谁知道为什么会这样?

Then in the MainView.Xaml, I set the datacontext of the usercontrol to bind to the path of this MainViewModel. This works fine - I put a breakpoint on the MainViewModel constructor and it is being hit once. However, all the properties in the ViewModel which are set as a result of event triggers on controls within the MainViewModel are being hit three times. Does anyone know why this could be happening?

以下是MainView.Xaml中的代码示例:

Here is a sample of the code in the MainView.Xaml:

<UserControl.DataContext>
    <Binding Path="Main" Source="{StaticResource Locator}"/>
</UserControl.DataContext>

<Grid x:Name="LayoutRoot" Background="#FF292929">
...
<MediaElement Stretch="Fill" AutoPlay="False" Name="mediaElement">
<MediaElement.Style>
 <Style TargetType="MediaElement">
  <Setter Property="OpacityMask" Value="Black"/>
 </Style>
</MediaElement.Style>
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Loaded">
                    <i:InvokeCommandAction Command="{Binding MediaOpenedCommand}" CommandParameter="{Binding ElementName=mediaElement, Mode=OneWay}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </MediaElement>
...

在这种情况下,MediaOpenedCommand被击中了3次.知道为什么吗?

In this case, the MediaOpenedCommand is being hit three times. Any idea why?

推荐答案

我发现它被命中三次的原因是,该特定视图在不同的XAML页面中被引用了三次.

I found the reason that it is getting hit three times is because that particular view is being referenced three times from within different XAML pages.

谢谢

这篇关于MVVM Light-使用ViewModelLocator-属性多次击中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆