MVVM Light - 使用 ViewModelLocator - 属性多次命中
我正在使用 MVVM Light ViewModelLocator。我有一个名为 GlobalViewModelLocator 的类,它连接在 App.Xaml 的资源中。此类有一个名为 Main 的静态属性,它返回 MainViewModel 的实例。
然后在 MainView.Xaml 中,我设置用户控件的数据上下文以绑定到此 MainViewModel 的路径。这工作正常 - 我在 MainViewModel 构造函数上放置了一个断点,并且它被击中一次。但是,由于 MainViewModel 内的控件上的事件触发器而设置的 ViewModel 中的所有属性都被命中了 3 次。有谁知道为什么会发生这种情况?
下面是 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 次。知道为什么吗?
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.
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?
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>
...
In this case, the MediaOpenedCommand is being hit three times. Any idea why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现它被点击 3 次的原因是因为该特定视图在不同的 XAML 页面中被引用了 3 次。
谢谢
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.
Thanks