我使用下面的代码,但它在最后一行抛出错误 [英] I am using below code but it's throw an error on last line
问题描述
protected override void Append(LoggingEvent loggingEvent)
{
if (loggingEvent.Level.Name == "INFO")
{
FIXOUTUI ui = FIXOUTUI.GetInstance();
Action action = () =>
{
ui.Listbox1.Items.Add(loggingEvent.TimeStamp.ToString("HH:mm:ss") + " " + loggingEvent.RenderedMessage);
ui.Listbox1.SelectedItem = ui.Listbox1.Items[ui.Listbox1.Items.Count - 1];
ui.Listbox1.UpdateLayout();
var listBoxItem = (ListBoxItem)ui.Listbox1
.ItemContainerGenerator
.ContainerFromItem(ui.Listbox1.SelectedItem);
listBoxItem.Focus(); this line throw an error
};
ui.Dispatcher.BeginInvoke(action);
}
}
错误:未处理异常System.NullReferenceException:
对象引用未设置为对象的实例。
at MyApp.Delogs。<> c__DisplayClass3.b__0()
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback,Object args,Int32 numArgs)
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source,Delegate method,Object args,Int32 numArgs,Delegate catchHandler)
ERROR: Unhandled Exception System.NullReferenceException:
Object reference not set to an instance of an object.
at MyApp.Delogs.<>c__DisplayClass3.b__0()
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
推荐答案
看起来你的ListBox(ListBox1)没有包含数据。调试你的代码。
It looks like your ListBox (ListBox1) is not pouplated with data . debug your code .
这篇关于我使用下面的代码,但它在最后一行抛出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!