"The calling thread must be STA because many user interface components require this." Error in WPF?

I am creating an xps document as shown 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 try to get docXps.GetFixedDocumentSequence (); I get the above error. Can anyone help?

Thanks,

+3
source share
2 answers

Your problem has nothing to do with code related to creating or using an XPS document. It has everything related to the thread in which you work.

You will receive an error The calling thread must be STA, because many UI components require thiswhenever any of the following actions are taken in the MTA stream:

  • , FrameworkElement ( )
  • , BitmapEffect
  • , TextComposition
  • , HwndSource
  • InputManager
  • KeyboardDevice, StylusDevice TabletDevice
  • FrameworkContentElement
  • , IME ,
  • WPF .
  • ,
  • ,

, , XAML, <Button> WPF WCF. : STA .

, XPS . , GetFixedDocumentSequence TextComposition .

, STA , , , XpsDocuments, MTA. (, ) STA.

, , XPS-, GUI? ? WCF -? ASPX? , , , .

, , GetFixedDocumentSequence, . , . , , , , , STA .

+18

xps ? , . , .

, , GetFixedDocumentSequence()?

+1

Source: https://habr.com/ru/post/1722293/


All Articles