Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. [WPF] XamlParseException ----> The calling thread must be STA

[WPF] XamlParseException ----> The calling thread must be STA

Scheduled Pinned Locked Moved WPF
wpfcsharphtmlcomdesign
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pbalaga
    wrote on last edited by
    #1

    Hi, currently I'm testing a really small WPF app that uses resources from external sources and at first it appeared to work fine. Now, magically, it's not even able to load a resource from the same assembly. The following code:

            Uri uri = new Uri("thewinda.xaml", UriKind.Relative);
            Window w = Application.LoadComponent(uri) as Window;
    

    causes a XamlParseException with the message:

    Cannot create instance of 'thewinda' defined in assembly 'WpfTemplateTry, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Exception has been thrown by the target of an invocation. Error in markup file 'thewinda.xaml' Line 1 Position 9.

    One of its inner exceptions says: The calling thread must be STA, because many UI components require this. This is quite weird as "thewinda.xaml" is a file added to the same project as my application. I've read this error can be connected with threading/async issues, but I have no idea, how I could apply this hint to my case. This is the body of "thewinda.xaml": <Window x:Class="WpfTemplateTry.thewinda" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300"> </Window>

    M 1 Reply Last reply
    0
    • P pbalaga

      Hi, currently I'm testing a really small WPF app that uses resources from external sources and at first it appeared to work fine. Now, magically, it's not even able to load a resource from the same assembly. The following code:

              Uri uri = new Uri("thewinda.xaml", UriKind.Relative);
              Window w = Application.LoadComponent(uri) as Window;
      

      causes a XamlParseException with the message:

      Cannot create instance of 'thewinda' defined in assembly 'WpfTemplateTry, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Exception has been thrown by the target of an invocation. Error in markup file 'thewinda.xaml' Line 1 Position 9.

      One of its inner exceptions says: The calling thread must be STA, because many UI components require this. This is quite weird as "thewinda.xaml" is a file added to the same project as my application. I've read this error can be connected with threading/async issues, but I have no idea, how I could apply this hint to my case. This is the body of "thewinda.xaml": <Window x:Class="WpfTemplateTry.thewinda" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300"> </Window>

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      Are you doing the load on a separate thread? What is the build type set to on the thewinda.xaml file?

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      P 1 Reply Last reply
      0
      • M Mark Salsbery

        Are you doing the load on a separate thread? What is the build type set to on the thewinda.xaml file?

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        P Offline
        P Offline
        pbalaga
        wrote on last edited by
        #3

        Thanks for your response. I've managed to overcome the problem, but rather by changing my approach than by understanding what's actually wrong. I was not loading the mentioned file on a separate thread explicitly. Maybe the system was doing something alike without letting me know. Anyway, I tried to run that WPF application with a custom Main() method. (I didn't alter the auto-generated entry point file, but created my own basing on WinForms experience.) It was like that:

        Main()
        {
        DerivedAppClass app = new DerivedAppClass();
        app.Run();
        }

        DerivedAppClass was located in an external dll as an extension of the WPF's Application class. It's obvious, but seems to be not that straightforward in WPF. After I started following the default scheme(App.xaml + App.xaml.cs), it's working as expected.

        M 1 Reply Last reply
        0
        • P pbalaga

          Thanks for your response. I've managed to overcome the problem, but rather by changing my approach than by understanding what's actually wrong. I was not loading the mentioned file on a separate thread explicitly. Maybe the system was doing something alike without letting me know. Anyway, I tried to run that WPF application with a custom Main() method. (I didn't alter the auto-generated entry point file, but created my own basing on WinForms experience.) It was like that:

          Main()
          {
          DerivedAppClass app = new DerivedAppClass();
          app.Run();
          }

          DerivedAppClass was located in an external dll as an extension of the WPF's Application class. It's obvious, but seems to be not that straightforward in WPF. After I started following the default scheme(App.xaml + App.xaml.cs), it's working as expected.

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          New threads are MTA by default, but if you need STA you can specify STA. On your custom entry point function you can use STAThreadAttribute:

          [STAThread]
          Main()
          {
          DerivedAppClass app = new DerivedAppClass();
          app.Run();
          }

          On dynamically created threads you can use the SetApartmentState() method:

          mynewthread.SetApartmentState(ApartmentState.STA);
          mynewthread.Start(...);

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          P 1 Reply Last reply
          0
          • M Mark Salsbery

            New threads are MTA by default, but if you need STA you can specify STA. On your custom entry point function you can use STAThreadAttribute:

            [STAThread]
            Main()
            {
            DerivedAppClass app = new DerivedAppClass();
            app.Run();
            }

            On dynamically created threads you can use the SetApartmentState() method:

            mynewthread.SetApartmentState(ApartmentState.STA);
            mynewthread.Start(...);

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            P Offline
            P Offline
            pbalaga
            wrote on last edited by
            #5

            Oh, excellent remark. LoadComponent() is working flawlessly from now on. Thank you for your help and time.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups