locked
Problem with Button.Click events RRS feed

  • Question

  • Hi all,

    A while ago I was looking to learn how to create a Button template with animations b/c I was unable to implement a "click" event in a UserControl. I've created the template in App.xaml so now it's global, animations are working - and basically, I'm happy - with the exception of the click events.

     

     If I add the "Click="" " in markup,

    Page.xaml : 

    <Button  x:Name="SignUp_Button" Margin="0,2,0,2" Style="{StaticResource GlassButton}" Content="SignUp" Width="100" Height="35" Click="SignUp_Button_Click"/> 

    Visual studio will create the method for me. All is fine and it works.

     

    If I'd decide  to implement the event in the code behind file

    Page.xaml.cd :

    public Page()
            {
                this.Loaded += new RoutedEventHandler(Page_Loaded);
                this.SignUp_Button.Click += new RoutedEventHandler(SignUp_Button_Click);
                   InitializeComponent();
            }

     

    The application goes blank before reaching the loading screen.

     

    Can someone explain to me what is happening exactly ?

    Thank you. 

    Wednesday, May 14, 2008 6:42 PM

Answers

  • just put this line 

        this.SignUp_Button.Click += new RoutedEventHandler(SignUp_Button_Click);

    into Page_Loaded

    Wednesday, May 14, 2008 7:27 PM
  •  You could move your code for attaching handler to Click event to Page_Loaded method, as suggested in previous reply. Or you could just move your code below InitializeComponent.

    What is happening exactly? SignUp_Button is actually initialized in InitializeComponent call, so currently you are trying to access null object by trying to subscribe to Click event. This probably causes System.NullReferenceException, this exception is unhandled, and apparently you are not handling it in Application.UnhandledException handler, so your app is shut down and onError handler (javascript) is invoked in HTML host page. But that handler also ignores all types of errors, except InitializationErrors by default. So you blank screen (the application is actually loaded, but then it tries to execute your code, encounters an unhandled exception and shuts down, and this happens very quickly, so you percieve it as blank page).

    Wednesday, May 14, 2008 8:19 PM

All replies

  • just put this line 

        this.SignUp_Button.Click += new RoutedEventHandler(SignUp_Button_Click);

    into Page_Loaded

    Wednesday, May 14, 2008 7:27 PM
  •  You could move your code for attaching handler to Click event to Page_Loaded method, as suggested in previous reply. Or you could just move your code below InitializeComponent.

    What is happening exactly? SignUp_Button is actually initialized in InitializeComponent call, so currently you are trying to access null object by trying to subscribe to Click event. This probably causes System.NullReferenceException, this exception is unhandled, and apparently you are not handling it in Application.UnhandledException handler, so your app is shut down and onError handler (javascript) is invoked in HTML host page. But that handler also ignores all types of errors, except InitializationErrors by default. So you blank screen (the application is actually loaded, but then it tries to execute your code, encounters an unhandled exception and shuts down, and this happens very quickly, so you percieve it as blank page).

    Wednesday, May 14, 2008 8:19 PM