我们都知道我们都知道WPF是一个单UI线程的应用模型
但是实际上我们可以通过在后台线程跑一个 Dispatcher 来运行UI元素
Dispatcher
详情可以参见这个大大的博客 Launching a WPF Window in a Separate Thread, Part 1 : Reed Copsey, Jr.
这种场景用来加载一个欢迎界面还是很nice的
但是显然WPF的团队没有充分考虑过这种场景,因为我们遇到了如下的异常“集合已经修改”
System.Windows.Markup.XamlParseException: 集合已修改;可能无法执行枚举操作。 ---> System.InvalidOperationException: 集合已修改;可能无法执行枚举操作。 在 System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource) 在 System.Collections.Generic.List`1.Enumerator.MoveNextRare() 在 System.Collections.Generic.List`1.Enumerator.MoveNext() 在 System.Windows.Baml2006.WpfSharedBamlSchemaContext.GetKnownXamlType(Type type) 在 System.Windows.Baml2006.WpfSharedBamlSchemaContext.GetXamlType(Type type) 在 System.Windows.Baml2006.Baml2006SchemaContext.GetXamlType(Type type) 在 System.Xaml.XamlObjectWriter.GetXamlType(Type clrType) 在 System.Xaml.XamlObjectWriter.WriteEndMember() 在 System.Xaml.XamlWriter.WriteNode(XamlReader reader) 在 System.Windows.Markup.WpfXamlLoader.TransformNodes(XamlReader xamlReader, XamlObjectWriter xamlWriter, Boolean onlyLoadOneNode, Boolean skipJournaledProperties, Boolean shouldPassLineNumberInfo, IXamlLineInfo xamlLineInfo, IXamlLineInfoConsumer xamlLineInfoConsumer, XamlContextStack`1 stack, IStyleConnector styleConnector) 在 System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)