locked
How to invoke WPF Dispatcher in unit testing. RRS feed

  • Question


  • I want to test an application which renders a text block with a data field value. I would like to get the actual width and actual height, once the rendering completes. Everything works fine. The problem came first, when I tried to test the application. I'm unable to invoke the dispatcher from the test project.

    Following is the code.

    this.Loaded += (s, e) =>
    {
        TextBlock textBlock1 = new TextBlock();

        //// Text block value is assigned from data base field.
        textBlock1.Text = strValueFromDataBaseField;        
        //// Setting the wrap behavior.
        textBlock1.TextWrapping = TextWrapping.WrapWithOverflow;
        //// Adding the text block to the layout canvas.
        this.layoutCanvas.Children.Add(textBlock1);

        this.Dispatcher.BeginInvoke(DispatcherPriority.Background,
            (Action)(() =>
                {
                    //// After rendering the text block with the data base field value. Measuring the actual width and height.
                   this.TextBlockActualWidth = textBlock1.ActualWidth;
                   this.TextBlockActualHeight = textBlock1.ActualHeight;

                   //// Other calculations based on the actual widht and actual height.
                }
            ));
    };

    Thanks
    Tuesday, March 16, 2010 1:21 PM

All replies

  • Hi all, 

    please tell me if we have workaround to achieve this.

     

    Thanks

    Thursday, April 29, 2010 5:09 AM