MVVM Light - 使用 ViewModelLocator - 属性多次命中

发布于 2024-09-25 16:34:28 字数 1242 浏览 0 评论 0原文

我正在使用 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 技术交流群。

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

发布评论

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

评论(1

放飞的风筝 2024-10-02 16:34:28

我发现它被点击 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

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