显示自测试单元WPF窗口 [英] Show WPF window from test unit

查看:109
本文介绍了显示自测试单元WPF窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行测试单元(和学习它们)。很简单,我单位将创建一个列表,并将其传递给我的主窗口。

I am running a test unit (and learning about them). Quite simply, my unit creates a List and passes it to my MainWindow.

我的问题是在我显示()主窗口单位方法结束。我想让装置没有完成,直到我关闭主窗口。这是我做了什么(见下文) - 这显然是行不通的,感觉就像我在错误的道路上这里。我怎样才能做到这正常吗?

The issue I have is after I show() the main window the unit method ends. I want the unit to not finish until I close the MainWindow. This is what I've done (see below) - it obviously doesn't work and feels like I'm on the wrong path here. How can I do this properly?

    [TestClass]
    public class Logging
    {
        bool continueOn = true;
        [TestMethod]
        public void ShowLogs()
        {
            ShowResults(createLogList());
        }

        private void ShowResults(List<Log> logList)
        {
            MainWindow mw = new MainWindow(logList);
            mw.Closed += mw_Closed;  
            mw.Show();

            while (continueOn)
            { }
        }

        void mw_Closed(object sender, EventArgs e)
        {
            this.continueOn = false;
        }

        private List<Log> createLogList()
        {
            List<Log> listLog = new List<Log>();
            //logic 
            return listLog;            
        }



也许我得把这个到后台辅助线程和监视 - 到说实话我不知道之前我浪费时间,我会很感激一些指导。

Maybe I have to put this onto a background worker thread and monitor that - to be honest I've no idea and before I waste hours, I'd appreciate some guidance.

推荐答案

WPF窗口必须是创建和支持WPF窗口的基础设施(消息泵送)一个线程中。

The WPF Window must be created and shown on a thread which supports the WPF window infrastructure (message pumping).

[TestMethod]
    public void TestMethod1()
    {
        MainWindow window = null;

        // The dispatcher thread
        var t = new Thread(() =>
        {
            window = new MainWindow();

            // Initiates the dispatcher thread shutdown when the window closes
            window.Closed += (s, e) => window.Dispatcher.InvokeShutdown();

            window.Show();

            // Makes the thread support message pumping
            System.Windows.Threading.Dispatcher.Run();
        });

        // Configure the thread
        t.SetApartmentState(ApartmentState.STA);
        t.Start();
        t.Join();
    }



需要注意的是:

Note that:


  • 必须创建新的线程中所示的窗口。

  • 您必须启动一个调度器(System.Windows.Threading.Dispatcher.Run())在的ThreadStart返回之前,否则窗口将显示,不久后死去。

  • 线程必须配置为在STA公寓运行。

有关更多信息,请访问的此链接

For more information, visit this link.

这篇关于显示自测试单元WPF窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆