所以我有主格式和第二格式ListView
:
public partial class LoggerForm : MetroWindow
{
public LoggerForm()
{
InitializeComponent();
}
}
XAML:
<ListView Name="lvLogger"
ItemsSource="{Binding Path=(list:LogHelper.LogEntries)}"
Background="#181818"
Margin="0,0,0,0">
<ListView.View>
<GridView>
<GridViewColumn Width="Auto" Header="Time" DisplayMemberBinding="{Binding DateTime}"/>
<GridViewColumn Width="Auto" Header="Index" DisplayMemberBinding="{Binding Index}"/>
<GridViewColumn Width="Auto" Header="Level" DisplayMemberBinding="{Binding Level}"/>
<GridViewColumn Width="Auto" Header="Source" DisplayMemberBinding="{Binding Source}"/>
<GridViewColumn Width="Auto" Header="Message" DisplayMemberBinding="{Binding Message}"/>
</GridView>
</ListView.View>
</ListView>
日志对象:
public class LogEntry
{
public string DateTime { get; set; }
public int Index { get; set; }
public string Source{ get; set; }
public Level Level { get; set; }
public string Message { get; set; }
}
我的Log对象列表:
public static class LogHelper
{
public static ObservableCollection<LogEntry> LogEntries { get; set; }
public static void AddLog(Level level, string message, string source)
{
LogEntry logEntry = new LogEntry()
{
DateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss,fff"),
Index = LogEntries.Count + 1,
Level = level,
Source = source,
Message = message
};
Application.Current.Dispatcher.Invoke(new Action(() =>
{
LogEntries.Add(logEntry);
}));
}
}
这就是我创建新Log
的方式:
LogHelper.AddLog(Level.Info, "My message", $"{ GetType().Name }\\{ MethodBase.GetCurrentMethod().Name }")
;
所以我有另一个class
执行work
并使用Task
使用不同的thread
,在这种情况下,我无法在ListView
中看到其消息虽然我尝试使用这种方式:
Application.Current.Dispatcher.Invoke(new Action(() =>
{
LogEntries.Add(logEntry);
}));