我正在使用MVVM灯光视图模型定位器。我有一个名为globalview modellocator的类,它连接在app.xaml中的资源中。此类具有名为main的静态属性,该属性返回MainViewModel的实例。
然后在mainview.xaml中,我设置了usercontrol的dataContext以绑定到此mainviewModel的路径。这很好——我在MainViewModel构造函数上放置了一个断点,它被命中一次。但是,由于MainViewModel中控件的事件触发器而设置的ViewModel中的所有属性都会被命中三次。有人知道为什么会这样吗?
以下是主视图中的代码示例。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被点击三次。你知道为什么吗?