Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 正在尝试在TestCase中使用Application.Current,但无法从System.Windows导入应用程序_C#_Unit Testing - Fatal编程技术网

C# 正在尝试在TestCase中使用Application.Current,但无法从System.Windows导入应用程序

C# 正在尝试在TestCase中使用Application.Current,但无法从System.Windows导入应用程序,c#,unit-testing,C#,Unit Testing,我尝试对使用 Application.Current.Dispatcher.Invoke()并希望在类似线程中使用@informatorius提供的解决方案。代码如下所示 我遇到的问题是,应用程序没有得到解决,即使我使用System.Windows添加了。有什么特殊的机制可以解决吗 应用程序来自定义测试用例的类库? 我安装了MSTest.TestFramework和MSTest.TestAdapter软件包 using System; using System.Collections.Gener

我尝试对使用
Application.Current.Dispatcher.Invoke()
并希望在类似线程中使用@informatorius提供的解决方案。代码如下所示

我遇到的问题是,
应用程序
没有得到解决,即使我使用System.Windows添加了
。有什么特殊的机制可以解决吗
应用程序
来自定义测试用例的类库? 我安装了
MSTest.TestFramework
MSTest.TestAdapter
软件包

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass] 
public class ApplicationInitializer
{
    [AssemblyInitialize]
    public static void AssemblyInitialize(TestContext context)
    {
        var waitForApplicationRun = new TaskCompletionSource<bool>();
        Task.Run(() =>
        {
            var application = new Application();
            application.Startup += (s, e) => { waitForApplicationRun.SetResult(true); };
            application.Run();
        });
        waitForApplicationRun.Task.Wait();
    }
    [AssemblyCleanup]
    public static void AssemblyCleanup()
    {
        Application.Current.Dispatcher.Invoke(Application.Current.Shutdown);
    }
}

[TestClass]
public class MyTestClass
{
    [TestMethod]
    public void MyTestMethod()
    {
        // implementation can access Application.Current.Dispatcher
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows;
使用System.Threading.Tasks;
使用Microsoft.VisualStudio.TestTools.UnitTesting;
[测试类]
公共类应用程序初始化器
{
[汇编初始化]
公共静态void AssemblyInitialize(TestContext上下文)
{
var waitForApplicationRun=new TaskCompletionSource();
Task.Run(()=>
{
var application=新应用程序();
application.Startup+=(s,e)=>{waitForApplicationRun.SetResult(true);};
application.Run();
});
waitForApplicationRun.Task.Wait();
}
[组装清理]
公共静态void AssemblyCleanup()
{
Application.Current.Dispatcher.Invoke(Application.Current.Shutdown);
}
}
[测试类]
公共类MyTestClass
{
[测试方法]
公共void MyTestMethod()
{
//实现可以访问Application.Current.Dispatcher
}
}
为我指明了正确的方向:
使用System.Windows
是不够的,我还需要在项目中添加对
PresentationFramework
的引用。我真的不明白那背后的汽车魔力