简体   繁体   English

MVVM指示灯RelayCommand触发两次

[英]MVVM light RelayCommand triggers twice

I'm working on a new c# MVVM light modern ui WPF application and I ran into some weird trigger behavior. 我正在开发新的C#MVVM轻型现代ui WPF应用程序,但遇到了一些奇怪的触发行为。 So to describe the situation, here is the following: 因此,为了描述这种情况,如下所示:

I have an XML view with triggers. 我有一个带有触发器的XML视图。 (Loaded method as example) (以加载方法为例)

<i:Interaction.Triggers>
    <i:EventTrigger EventName="Loaded">
        <i:InvokeCommandAction Command="{Binding LoadedCommand}" />
    </i:EventTrigger>
</i:Interaction.Triggers>

The constructor with the relaycommand (LoadedCommand from abstract model) 具有relaycommand的构造函数(抽象模型中的LoadedCommand)

    public MovieListModel()
    {
        LoadedCommand = new RelayCommand(LoadData);
    }        
    private void LoadData()
    {
        Debug.WriteLine("MovieListModel - LoadData");
    }

Problem: When the application starts the loadedcommand is called 1 time (good). 问题:应用程序启动时,被加载的命令被调用1次(良好)。 When I navigate to an other page inside the application, then both the first and the new view LoadedCommand's are triggered. 当我导航到应用程序内的另一个页面时,将同时触发第一个视图和新视图LoadedCommand。 Other commands are also triggered twice. 其他命令也会触发两次。 I am guessing this will become a problem if I want to put some programming logic inside later on. 我猜想如果以后要在其中加入一些编程逻辑,这将成为一个问题。

The full debug.writeline is as followed: 完整的debug.writeline如下所示:

ModernUserControl - OnNavigatingFrom
MovieListModel - NavigatingFrom
ModernUserControl - OnNavigatingFrom event called
ModernUserControl - OnNavigatedFrom
MovieListModel - NavigatedFrom
ModernUserControl - OnNavigatedFrom event called
ModernUserControl - OnNavigatedTo
MovieListModel - LoadData
SettingsModel - LoadData

Info: I found this problem in the example program found here: https://github.com/saramgsilva/ModernUISamples and in my own application. 信息:我在以下示例程序中发现了此问题: https : //github.com/saramgsilva/ModernUISamples和我自己的应用程序。 I also started a ticket on github to find out if this is a problem/bug or not. 我还在github上开始了一张票,以了解这是否是问题/错误。 (no reply yet) (尚未回复)

I would recommend avoiding the Loaded event in WPF for code that is only supposed to run once. 我建议避免在WPF中避免只应运行一次的代码中的Loaded事件。

WPF unloads controls which are not in use (such as if the application is minimized, or you change tab pages), so the Loaded command will often run more than once. WPF卸载不使用的控件(例如,如果应用程序已最小化,或者您更改了选项卡页),因此“ Loaded命令通常会运行一次以上。

If code is meant to only run once, I typically will either put it in the Constructor or lazy-load data in the getter. 如果代码只打算运行一次,我通常将其放入构造函数中或将其延迟加载到getter中。

And if I really want to use the Loaded event for something that is only meant to run once, I will make sure to detach the event from within the Loaded event handler when it runs the first time. 如果我真的想将Loaded事件用于只可以运行一次的事件,那么我将确保在第一次运行时从Loaded事件处理程序中分离该事件。

The loaded event is triggered twice. 加载的事件触发两次。

Read this blog post explaining it: http://blogs.msdn.com/b/mikehillberg/archive/2006/09/19/loadedvsinitialized.aspx 阅读此博客文章对其进行解释: http : //blogs.msdn.com/b/mikehillberg/archive/2006/09/19/loadedvsinitialized.aspx

Or even better, read this answer from another post: Loaded event of a WPF user control fire two times 甚至更好的是,从另一篇文章中阅读以下答案: WPF用户控件的Loaded事件触发两次

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM