Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6468423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T05:53:18+00:00 2026-05-25T05:53:18+00:00

I have a UserControl which publishes an EventAggregator message in its Loaded event. In

  • 0

I have a UserControl which publishes an EventAggregator message in its Loaded event. In order to test this (and get the Loaded event raised) I am currently creating a window and adding the control to it, then waiting for the Loaded event to be raised.

Is there any way to setup a test so that the Loaded event fires without having to create and add the control to a window?

For example:

[Test, RequiresSTA]
public void active_thingy_message_is_published_on_loaded()
{
    const string TestMsg = "Active thingy changed";

    using (AutoResetEvent loadedEvent = new AutoResetEvent(false))
    {
        DummyEventService eventService = new DummyEventService();                
        DummyControl control = new DummyControl(eventService, TestMsg);
        control.Loaded += delegate { loadedEvent.Set(); };

        Assert.That(eventService.Message, Is.Null, "Before.");
        Window window = new Window { Content = control };
        window.Show();                
        loadedEvent.WaitOne();
        window.Dispatcher.InvokeShutdown();
        Assert.That(eventService.Message, Is.EqualTo(TestMsg), "After.");
    }
}

private class DummyControl : UserControl
{
    public DummyControl(DummyEventService eventService, string testMsg)
    {
        Loaded += delegate { eventService.Publish(testMsg); };
    }
}

private class DummyEventService
{
    public string Message { get; private set; }
    public void Publish(string msg) { Message = msg; }
}

Update

I’ve changed the title from “Unit Testing…” to “Testing…”, and replaced the tag “unit-testing” with “testing”.

I would prefer not to split hairs over exactly what class of test this is, as it is not constructive. Yes it could be argued that this is not a “Unit Test”, but that’s not helpful. I want to test an issue that is dependent on the control’s life-cycle and this involves the Loaded event. It’s an important regression test, as 3rd party components I have no control over depend on the message being raised at Loaded.

Can the Loaded event be raised without adding the control to a window?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T05:53:19+00:00Added an answer on May 25, 2026 at 5:53 am

    If you are just interested in firing the Loaded event of the target control, then Reflection should do the trick.

    public static void RaiseLoadedEvent(FrameworkElement element)
    {
        MethodInfo eventMethod = typeof(FrameworkElement).GetMethod("OnLoaded",
            BindingFlags.Instance | BindingFlags.NonPublic);
    
        RoutedEventArgs args = new RoutedEventArgs(FrameworkElement.LoadedEvent);
    
        eventMethod.Invoke(element, new object[] { args });
    }
    

    This literally fires the OnLoaded method that is present in each FrameworkElement, so if your test requires Application state, this won’t work.

    Also, there is no relationship between the Loaded event of a parent and it’s children. If a test requires the child elements to fire their Loaded events, then the helper method will need to manually walk the child controls and fire those as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a UserControl at which a message is being appeared gradually and after
I have a usercontrol which overrides the property Text. But this property is not
I have a usercontrol which has a combobox inside it. This UserControl is the
I have a UserControl which contains 3 labels. I want to add an event
I have a UserControl which rather than putting tags into its ascx markup file,
I have an UserControl which has listbox. I put this UserControl on the Mainpage.xaml.
Just trying to get my head round WPF. I have a usercontrol which I'm
I have a UserControl which has a quadratic Image as a Child. This Image
I have a UserControl which contains the following XAML: <GroupBox> <Grid> <Button x:Name=btn Content=Test/>
I have a UserControl which basically wraps a ListBox like this - <ListBox x:Name=lb

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.