有一个包含CustomRichTextBox的视图。
public static readonly DependencyProperty NewOutputLogItemProperty = DependencyProperty.Register("NewOutputLogItem",
typeof(OutputLog ), typeof(CustomRichTextBox), new FrameworkPropertyMetadata(null, OnNewOutputLogAdded));
public OutputLog NewOutputLogItem
{
get { return (OutputLog )GetValue(NewOutputLogItemProperty ); }
set { SetValue(NewOutputLogItemProperty , value); }
}
private static void OnNewOutputLogAdded(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
var richBox = (CustomRichTextBox)obj;
var outputLog = (OutputLog )args.NewValue;
richBox.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
{
var mainParagraph = richBox.Document.Blocks.FirstBlock as Paragraph;
if (mainParagraph != null)
{
mainParagraph.Inlines.AddRange(outputLog.GetInlines());
}
}));
}
还有ViewModel,它通过MVVM light Messenger接收消息并设置View绑定的属性。
public void ProcessNotificationMessage(OutputLog message)
{
NewOutputLog = message;
}
不幸的是,我发现如果ProcessNotificationMessage工作得太快而且没有描述某些消息,那么View会错过单个消息。
您是否知道如何确保其正常工作?