We have an SDK which has many WPF views/components and logic that we deliver to our customers, and then on top of that they have their own WPF views etc.
Our UI elements are created and managed by our own UI thread, which has its own dispatcher.
Our thread is initialized something like below:
_feedbackThread = new Thread(FeedbackThread)
{
// Name all SDK threads for debug output
Name = "FeedbackUiThread",
IsBackground = true
};
_feedbackThread.SetApartmentState(ApartmentState.STA);
_feedbackThread.Start();
and the FeedbackThread
method looks like:
private void FeedbackThread()
{
// create UI window etc
//.....
Dispatcher.Run();
}
Now the problem happens when they apply shared global styles, that is because when they add for example a global style for StackPanel
, the style object is created once the application opens, and then at a later point on a button click they will execute the SDK code, and open a new window from it, hence the StackPanel
object from the SDK will try to reuse the same shared style objects created globally, and it fails because these objects are created from a different thread.
One solution to this problem is to edit our StackPanel
objects to set the style to NULL or a similar approach, but this is costly to identify all UI elements that need to have this style set to NULL, and secondly it’s hard to not forget in the future to apply the same trick on the new UI elements.
Is there any better solution to this problem?
A simple repo to check the issue can be found here