Subscribing for events from code

Hello, I'm after installing Jan CTP. It appears to be no means for generating event handlers from within IDE, or at least I couldn't find them. Normally double click or a button in property bag's header DID that.

Anyway - I was trying to type something like:

button1.Click += new WhateverEventHandler(...);

from within InitializeComponent routine. button1 is null, therefore my code got failed. I found a place (some Connect proc with the switch within, which consequently sets my form's vars to something meaningful) -I'd be curious if it's the only place I can do my stuff from. The question is which place can I safely subscribe for my events from

Thanks, Alex



Answer this question

Subscribing for events from code

  • CccF

    it looks weird, why not just retain the old style..double click control and automatically creates the events..and the same time create in XAML file!


  • Roman Wienicke

    In your XAML you would do:

    <Window Loaded="window_Loaded" ...>

    And then in code behind just declare the method window_Loaded with the correct signature like so:

    private void window_Loaded(object sender, RoutedEventArgs args)
    {

    }

    And then compile time the correct code is emitted to hook that up.

    HTH,
    Drew


  • Yashman

    Try to hook up the handlers in the Window Loaded event.


  • igkmahesh

    Thanks Valentin, is that the right thing to do though ;)

    What I mean is would it not affect the likfe cycle of my page, being putted into unsual place

    There always used to be a logical separation between loading and initialization (unless I'm a real dude here) - I would have though about setting event handlers as of part of initialization, rather than loading.

    Anyway, it's good for me as long as it works.

    Slan, Alex


  • Luke Hoban

    Thanks Valentin, Drew. It's actually lol, but where do I subscribe for Window_Loaded from

    Thanks, Alex


  • Kah Geh

    Valentin's suggestion is 100% correct. The Loaded event is the only safe place you can count on all of the windows child elements being completely initialized and availble for event connection. Therefore it is the "right thing to do". ;)

    Cheers,
    Drew


  • Roman Benko.

    Well I think you're talking about design time features of "Cider" at that point. I'm sure they just haven't implemented that yet. There's nothing intrinsic to WPF that would prevent IDEs from creating events like that.

    Cheers,
    Drew


  • Subscribing for events from code