[WPF] XamlParseException ----> The calling thread must be STA
-
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> -
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>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:
-
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:
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.
-
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.
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:
-
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: