Why usercontrols loaded event is not fired

10,824

Solution 1

This is because an exception is being thrown in the 'Loaded' eventhandler. The exception may be occurring as a result of a mixed mode assembly or some other exception that is "user handled", and the WPF framework is catching it (unknown to the debugger). This causes the debugger not to break when a breakpoint is set within the Loaded method.

To make sure you can see exactly what error is occurring:

  1. In VS2010 go to Debug | Exceptions.
  2. Tick "Thrown" exception radio boxes for the exceptions that may be applicable in your case.
  3. Re-run the app and VS2010 should break on the exception that is being thrown in the event handler.
  4. Debug according to a now known exception.

Solution 2

Does your UserControl constructor still make a call InitializeComponent(), without this, it will not build up its visuals and the Loaded event may not fire.

Share:
10,824

Related videos on Youtube

Nasenbaer
Author by

Nasenbaer

Have a family, sunny weather and on evening a lot of ideas for programming... Public profile is also available on http://www.tb-software.ch

Updated on June 01, 2022

Comments

  • Nasenbaer
    Nasenbaer about 2 years

    I have a user control. I had this situations again some times but could always fix it by using the "New() contructor". But I still wonder what I am doing wrong because the load event has to be fired if control was loaded!

    Here is some code:

    <Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:my="clr-namespace:OUTPUT___VideoContent"
        Title="OUTPUT - VideoContent" Height="350" Width="525" Icon="/OUTPUT%20-%20VideoContent;component/Images/VideoContent.png">
        <Grid x:Name="LayoutRoot">
            <Grid x:Name="VideoGrid">
                <my:ucVideoPresenter x:Name="VideoPresenter1"/>
                <TextBlock x:Name="txtInfo" Visibility="Collapsed" />
            </Grid>
        </Grid>
    </Window>
    

    and in the usercontrol, the load event is declared on WPF or codebehing without any success! Usercontrol wpf

    Usercontrol codebehind

  • EpiGen
    EpiGen almost 9 years
    Do you know how to add event handler "not firing" exception? "Add" option does not give any ready bounded to events or there is no anything close to "System.Windows.Input".
  • Nasenbaer
    Nasenbaer over 3 years
    InitializeComponent() is completed correctly. Usercontrol has no Exceptions while loading.