错误: Object reference not set to an instance of an object.
下面的算法有效.我尝试了,然后我将Winform项目删除到另一个目录,SynchronizationContext.Current是null.为什么?
SynchronizationContext uiCtx = SynchronizationContext.Current;
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
int[] makeSelfMoves = new int[4];
lock (replay)
{
// count should be more than 2
foreach (KeyValuePair<int, int[]> item in replay)
{
makeSelfMoves = replay[item.Key];
codeFile.ExecuteAll(makeSelfMoves[0],
makeSelfMoves[1], makeSelfMoves[2], makeSelfMoves[3]);
// i get the error here. uictx is null
uiCtx.Post(o =>
{
PrintPieces(codeFile.PieceState());
}, null);
System.Threading.Thread.Sleep(1000);
}
}
}
Run Code Online (Sandbox Code Playgroud) 我正在运行一个测试单元(并了解它们).很简单,我的单位创建一个List并将其传递给我的MainWindow.
我的问题是在show()主窗口之后单元方法结束.在关闭MainWindow之前,我希望装置不能完成.这就是我所做的(见下文) - 它显然不起作用,感觉我在这里走错了路.我该怎么做呢?
[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;
}
Run Code Online (Sandbox Code Playgroud)
也许我必须把它放到后台工作者的线程并监控 - 说实话我不知道,在我浪费时间之前,我会感激一些指导.
我有一个 VSTO Microsoft Word 加载项。当用户单击功能区按钮时,我希望显示一个 WPF 窗口。我一直认为执行此操作的唯一方法是将 ElementHost 控件添加到 Winforms 表单中,并在顶部添加 WPF 控件。但我通过here发现我也可以通过将WPF UserControl转换为WPF Window并使用control.Show()来显示它来完成同样的事情。这似乎消除了使用 ElementHost 的需要。在我走这条路之前,我有什么理由不应该这样做?这样做有什么技术差异或速度差异吗?