3

I'm working on a new c# MVVM light modern ui WPF application and I ran into some weird trigger behavior. So to describe the situation, here is the following:

I have an XML view with triggers. (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)

    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). When I navigate to an other page inside the application, then both the first and the new view LoadedCommand's are triggered. 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:

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. I also started a ticket on github to find out if this is a problem/bug or not. (no reply yet)

1
  • You say you use Loaded as an example, have you tried with other events? Because i know that loaded is triggered twice
    – Moddaman
    Apr 20, 2015 at 14:48

2 Answers 2

2

I would recommend avoiding the Loaded event in WPF for code that is only supposed to run once.

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.

If code is meant to only run once, I typically will either put it in the Constructor or lazy-load data in the 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.

1
  • Ah ok thanks. I'm still confused why this is also happening in the OnNavigate methods as well, but I will try to work around it. Now I know it's not a bug in my application, thanks! (and thx to all the other answers) Apr 20, 2015 at 14:59
2

The loaded event is triggered twice.

Read this blog post explaining it: 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

1
  • 1
    That SO question link is a good one for a simple explanation on why the Loaded event is fired twice :)
    – Rachel
    Apr 20, 2015 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.