0

I am working on a custom keycasting program for tut videos and I am using MouseKeyHook and I am using the example code found here: https://github.com/gmamaladze/globalmousekeyhook/blob/vNext/Demo/Main.cs to get the basic construction working.

As the example was intended for win forms I am having trouble with one line in particular. I have made everything work by omitting - if (IsDisposed) return; line 176.

How do i replicate this code for wpf?

 private void Log(string text)
    {
       if (IsDisposed) return;
        textBoxLog.AppendText(text);
        textBoxLog.ScrollToLine(textBoxLog.LineCount - 1);
    }

EDIT: This was not related to garbage collection it is because if the form is disposed textBoxLog will throw a ObjectDisposedException.

4

1 回答 1

1

这不是用于垃圾收集,这是因为如果您尝试调用或在表单已被处置并且 Log 在事后被调用,则如果表单被处置textBoxLog,则会抛出 a 。ObjectDisposedExceptionAppendTextScrollToLine

WPF 窗口和控件不像 winforms 那样是一次性的,但是如果您想重新创建行为,只需覆盖该OnClosed方法并设置一个标志。

private bool _isClosed = false;   

protected override void OnClosed(EventArgs e)
{
    _isClosed = true;
    base.OnClosed(e);     
}

private void Log(string text)
{
   if (_isClosed) return;
    textBoxLog.AppendText(text);
    textBoxLog.ScrollToLine(textBoxLog.LineCount - 1);
}
于 2016-09-05T16:31:04.663 回答