Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.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# 单元测试Windows 8应用商店应用程序UI(Xaml控件)_C#_Testing_Windows 8_Windows Runtime_Microsoft Metro - Fatal编程技术网

C# 单元测试Windows 8应用商店应用程序UI(Xaml控件)

C# 单元测试Windows 8应用商店应用程序UI(Xaml控件),c#,testing,windows-8,windows-runtime,microsoft-metro,C#,Testing,Windows 8,Windows Runtime,Microsoft Metro,我一直在创建一个Windows应用商店应用程序,但在测试一个创建网格的方法(这是一个XAML控件)时遇到了线程问题。 我尝试使用NUnit和MSTest进行测试 试验方法为: [TestMethod] public void CreateThumbnail_EmptyLayout_ReturnsEmptyGrid() { Layout l = new Layout(); ThumbnailCreator creator = new ThumbnailCreator();

我一直在创建一个Windows应用商店应用程序,但在测试一个创建网格的方法(这是一个XAML控件)时遇到了线程问题。 我尝试使用NUnit和MSTest进行测试

试验方法为:

[TestMethod]
public void CreateThumbnail_EmptyLayout_ReturnsEmptyGrid()
{
    Layout l = new Layout();
    ThumbnailCreator creator = new ThumbnailCreator();
    Grid grid = creator.CreateThumbnail(l, 192, 120);

    int count = grid.Children.Count;
    Assert.AreEqual(count, 0);
}  
和creator.create缩略图(引发错误的方法):

当我运行此测试时,它抛出以下错误:

System.Exception: The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))

与控件相关的代码需要在UI线程上运行。尝试:

[TestMethod]
async public Task CreateThumbnail_EmptyLayout_ReturnsEmptyGrid()
{
    int count = 0;
    await ExecuteOnUIThread(() =>
    {
        Layout l = new Layout();
        ThumbnailCreator creator = new ThumbnailCreator();
        Grid grid = creator.CreateThumbnail(l, 192, 120);
        count = grid.Children.Count;
    });

    Assert.AreEqual(count, 0);
}

public static IAsyncAction ExecuteOnUIThread(Windows.UI.Core.DispatchedHandler action)
{
    return Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, action);
}

以上内容应适用于MS测试。我不知道NUnit的情况。

非常感谢。它在MS测试中工作。在努尼特,它不起作用。
[TestMethod]
async public Task CreateThumbnail_EmptyLayout_ReturnsEmptyGrid()
{
    int count = 0;
    await ExecuteOnUIThread(() =>
    {
        Layout l = new Layout();
        ThumbnailCreator creator = new ThumbnailCreator();
        Grid grid = creator.CreateThumbnail(l, 192, 120);
        count = grid.Children.Count;
    });

    Assert.AreEqual(count, 0);
}

public static IAsyncAction ExecuteOnUIThread(Windows.UI.Core.DispatchedHandler action)
{
    return Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, action);
}