locked
How to execute window loaded event with no xaml code? RRS feed

  • Question

  • Hi,

    I have a requirement to start a timer in my user control when it loads. I implemented this by using Interaction.Triggers in xaml and binding a WindowLoaded command in my viewmodel.

    Is there any other way to achieve this functionality with zero xaml code (without using Interaction.Triggers)

    Thursday, August 20, 2015 1:54 PM

Answers

  • If the view instantiates the viewmodel, you could just start the timer in the constructor.

        <Window.DataContext>
            <local:MainWindowViewModel/>
        </Window.DataContext>

    and

            public MainWindowViewModel()
            {
                // Start timer
            }

    So long as your constructor doesn't take forever and your timer doesn't fire very quickly then it's irrelevant whether you start stuff off in the constructor or window loaded.

    If it did matter you can defer using:

    Application.Current.Dispatcher.InvokeAsync(new Action(() => 
    {  
       // Start timer
    
    }), DispatcherPriority.ContextIdle);



    • Edited by Andy ONeill Thursday, August 20, 2015 2:12 PM
    • Marked as answer by ykbharat Thursday, August 20, 2015 5:33 PM
    Thursday, August 20, 2015 2:12 PM

All replies

  • If the view instantiates the viewmodel, you could just start the timer in the constructor.

        <Window.DataContext>
            <local:MainWindowViewModel/>
        </Window.DataContext>

    and

            public MainWindowViewModel()
            {
                // Start timer
            }

    So long as your constructor doesn't take forever and your timer doesn't fire very quickly then it's irrelevant whether you start stuff off in the constructor or window loaded.

    If it did matter you can defer using:

    Application.Current.Dispatcher.InvokeAsync(new Action(() => 
    {  
       // Start timer
    
    }), DispatcherPriority.ContextIdle);



    • Edited by Andy ONeill Thursday, August 20, 2015 2:12 PM
    • Marked as answer by ykbharat Thursday, August 20, 2015 5:33 PM
    Thursday, August 20, 2015 2:12 PM
  • You could hook up an event handler in the code-behind of the view:

        public partial class UserControl1 : UserControl
        {
            public UserControl1()
            {
                InitializeComponent();
         this.DataCOntext = new SomeViewModel();
                this.Loaded += UserControl1_Loaded;
            }
    
            private void UserControl1_Loaded(object sender, RoutedEventArgs e)
            {
                SomeViewModel VM = (SomeViewModel)this.DataContext;
                VM.StartTimer();
    
            }
    

    This requires no changes to the XAML markup.

    But the view model doesn't know, and it is not supposed to know either for that matter, when the user control or window has been loaded, laid out and is ready for interaction if that's what you are asking. So you need to invoke a command or call some method of the view model from the view once the Loaded event occurs one way or another, for example using an interaction trigger in the XAML markup or by handling the event in the code-behind of the view.

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Thursday, August 20, 2015 2:53 PM