“调用线程必须是STA,因为许多UI组件需要这个。” WPF出错? [英] "The calling thread must be STA, because many UI components require this." Error in WPF?

查看:237
本文介绍了“调用线程必须是STA,因为许多UI组件需要这个。” WPF出错?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  Assembly assembly = Assembly.GetExecutingAssembly(); 
//读入嵌入式xpsDocument文件
Stream helpStream = assembly.GetManifestResourceStream(resourceNameOfContext);
if(helpStream!= null)
{
Package package = Package.Open(helpStream);
string inMemoryPackageName =memorystream://+ topicName +.xps;
Uri packageUri = new Uri(inMemoryPackageName);
//将包添加到PackageStore
PackageStore.AddPackage(packageUri,package);
docXps = new XpsDocument(package,CompressionOption.Maximum,inMemoryPackageName);
}
return docXps;

当我试图获取docXps.GetFixedDocumentSequence();
我收到上面的错误。任何人都可以帮忙?



谢谢,

解决方案

关于创建或使用XPS文档的代码。它与您正在运行的线程有任何关系。



您将收到调用线程必须是STA,因为许多UI组件需要这个错误,只要在MTA线程上尝试下列任何一种:




  • 您构造从FrameworkElement派生的任何对象(包括控件和面板)

  • 您构造从BitmapEffect派生的任何对象

  • 您构造从TextComposition派生的任何对象

  • 您构造从HwndSource导出的任何对象
  • 您访问当前的InputManager

  • 您访问主要的KeyboardDevice,StylusDevice或TabletDevice

  • 您尝试将焦点更改为FrameworkContentElement

  • 您可以向接受文本输入的任何控件提供鼠标,键盘或IME输入

  • 您可以使WPF内容可见或更新其布局

  • 您可以以这样的方式操纵可视化树:ca使用重新评估来渲染

  • 其他几个更改,主要与显示和输入有关



例如,我去年收到了这个错误,当我尝试反序列化一些包含WCF服务中的< Button> 和其他WPF对象的XAML。问题很简单:我只是切换到一个STA线程进行处理。



显然,大多数使用XPS文档的操作都会触发上述一个或多个条件。在你的情况下,我怀疑GetFixedDocumentSequence最后使用TextComposition或其一个子类。



毫无疑问,我的切换到STA线程的解决方案也将适用于你,但是首先,您需要知道如何使用XpsDocuments处理的代码从MTA线程运行。通常,来自GUI的任何代码(例如按钮按下)都会自动运行在STA线程中。



您可以使用操作XPS文档的代码在没有GUI的情况下被执行?从用户创建的线程?从WCF服务或Web服务?从ASPX页面跟踪,您可能会找到您的解决方案。



如果不行,请告诉我们调用GetFixedDocumentSequence的路径,所以我们可以诊断它。直接周围的代码并不像调用堆栈那样重要,并且它最初被调用。如果很难解释,您可能应该添加一个调用堆栈,以防止误会,并帮助我们进一步诊断问题,或者告诉您如何在特定情况下启动STA线程。


I am creating a xps document as below.

Assembly assembly = Assembly.GetExecutingAssembly();
//read embedded xpsDocument file
Stream helpStream = assembly.GetManifestResourceStream(resourceNameOfContext);
if (helpStream != null)
{
    Package package = Package.Open(helpStream);
    string inMemoryPackageName = "memorystream://" + topicName + ".xps";
    Uri packageUri = new Uri(inMemoryPackageName);
    //Add package to PackageStore
    PackageStore.AddPackage(packageUri, package);
    docXps = new XpsDocument(package, CompressionOption.Maximum, inMemoryPackageName);
}
return docXps;

When i am trying to get docXps.GetFixedDocumentSequence(); I am getting the above error. Can anyone help?

Thanks,

解决方案

Your problem has nothing to do with the code surrounding the creation or use of the XPS document. It has everything to do with what thread you are running under.

You will receive the The calling thread must be STA, because many UI components require this error whenever any of the following are attempted on a MTA thread:

  • You construct any object derived from FrameworkElement (including Controls and Panels)
  • You construct any object derived from BitmapEffect
  • You construct any object derived from TextComposition
  • You construct any object derived from HwndSource
  • You access the current InputManager
  • You access the primary KeyboardDevice, StylusDevice, or TabletDevice
  • You attempt to change the focus on a FrameworkContentElement
  • You provide mouse, keyboard or IME input to any control that accepts text input
  • You make WPF content visible or update its layout
  • You manipulate the visual tree in such a way as to cause a re-evaluation for rendering
  • Several other changes, mostly having to do with display and input

For example, I received this error last year when I tried to deserialize some XAML that contained <Button> and other WPF objects from within a WCF service. The problem was simple to solve: I just switch to a STA thread to do the processing.

Obviously most work with XPS documents will trigger one or more of the above conditions. In your case I suspect that GetFixedDocumentSequence ends up using TextComposition or one of its subclasses.

No doubt the my solution of switching to a STA thread will also work for you, but first you need to figure out how your code that works with XpsDocuments is getting executed run from a MTA thread. Normally any code from from the GUI (eg a button press) is automatically run in a STA thread.

Is it possible that your code that manipulates XPS Documents may be being executed without a GUI? From a user-created thread? From a WCF service or a web service? From an ASPX page? Track that down and you'll probably find your solution.

If that doesn't work, let us know the details of the path through which GetFixedDocumentSequence is called, so we can diagnose it. The directly surrounding code isn't nearly as important as the call stack and how it is originally being invoked. If it is hard to explain you probably should add a call stack to prevent misunderstandings and help us diagnose the problem further, or tell you how to start a STA thread in your particular situation.

这篇关于“调用线程必须是STA,因为许多UI组件需要这个。” WPF出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆