我一直在创建一个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);
}
Run Code Online (Sandbox Code Playgroud)
和creator.CreateThumbnail(抛出错误的方法):
public Grid CreateThumbnail(Layout l, double totalWidth, double totalHeight)
{
Grid newGrid = new Grid();
newGrid.Width = totalWidth;
newGrid.Height = totalHeight;
SolidColorBrush backGroundBrush = new SolidColorBrush(BackgroundColor);
newGrid.Background = backGroundBrush;
newGrid.Tag = l;
return newGrid;
}
Run Code Online (Sandbox Code Playgroud)
当我运行此测试时,它会抛出此错误:
System.Exception: The application called an interface that was marshalled for a different thread. (Exception …Run Code Online (Sandbox Code Playgroud)