object reference compiling error
private static void runantc_OutputDataReceived(object sendingProcess, DataReceivedEventArgs outLine)
{
// Collect the sort command output.
if (!String.IsNullOrEmpty(outLine.Data))
{
ProcoutputTextBlock.Dispatcher.BeginInvoke(new Action(() => { ProcoutputTextBlock.Text += outLine.Data; }, null));
}
}
Hi, in the above code i am having errors @ProcoutputTextBlock
Error 1 An object reference is required for the non-static field, method, or property 'WpfApplication1.Window1.ProcoutputTextBlock'
and @ (开发者_运维问答) => { ProcoutputTextBlock.Text += outLine.Data; }, null
Error 2 Method name expected
can anybody enlighten me?
Your function is static
, which means that it requires a reference to an instance of the class in order to access any instance members. Since I'm assuming that ProcoutputTextBlock
is a TextBlock
on your window, it needs an instance in order to access it.
The other option is making the function non-static, but since you don't show how this event handler is being attached, I don't know if that's a viable option for you.
ProcoutputTextBlock
is an instance property. You are accessing instance property from static method runantc_OutputDataReceived
.
To resolve this remove static
from runantc_OutputDataReceived
declaration.
To fix the second issue, remove the second argument null
. Like this:
ProcoutputTextBlock.Dispatcher.BeginInvoke(new Action(() => { ProcoutputTextBlock.Text += outLine.Data; }));
or
ProcoutputTextBlock.Dispatcher.BeginInvoke((Action)(() => { ProcoutputTextBlock.Text += outLine.Data; }));
精彩评论