0

I need to have some parameters in my MainWindow, like so:

    public MainWindow(DbContext _ctx, string dbSetName)
    {
        Type setType = Type.GetType(dbSetName);
        Type gType = typeof(GenericViewModel<>).MakeGenericType(setType);
        var oVM = Activator.CreateInstance(gType);
        this.DataContext = oVM;
        InitializeComponent();
    }

And now I want to show the MainWindow from a different project:

var w = new MainWindow (PersonCbContext, "People"

I get an exception saying:

"The calling thread must be STA, because many UI components require this."
  • The exception hasn't got to do with the constructor parameters. As the messages says, the thread where you create the MainWindow instance must be set to [STA](http://stackoverflow.com/q/2329978/1136211). – Clemens Jul 15 '16 at 08:21
  • Yeah, I've already seen that question, but it is very specific so I can't relate it to my problem... – Aleksa Kojadinovic Jul 15 '16 at 08:24
  • Mind explaining a bit? – Aleksa Kojadinovic Jul 15 '16 at 08:25
  • Sorry, this has been asked and answered a thousand times here. – Clemens Jul 15 '16 at 08:31
  • Yeah but every single question is tightly tied to its own topic so I can't understand any of them... – Aleksa Kojadinovic Jul 15 '16 at 08:33
  • You haven't even told anything about the (background) thread you're creating the MainWindow instance in. If you've created that Thread on your own, you should be able to call `SetApartmentState(ApartmentState.STA)` on it. If it's a thread you didn't create (e.g. from a thread pool), you can't do that, and as a result, can't create a window there. Then you would probably want to invoke window creation on the UI thread, e.g. by using `Application.Current.Dispatcher.Invoke(...)`. – Clemens Jul 15 '16 at 08:38

0 Answers0