Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why usercontrols loaded event is not fired

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

like image 927
Nasenbaer Avatar asked Jul 08 '11 08:07

Nasenbaer


2 Answers

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.
like image 199
Seth Avatar answered Nov 07 '22 21:11

Seth


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.

like image 22
ColinE Avatar answered Nov 07 '22 21:11

ColinE