Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I had a similar issue with this. I'm not sure how are you are implementing the background. I can try to explain my situation and maybe you can get something out of it. I created my own base Window let's call it MyWindow which is inheriting from Window. Ie.</p> <pre><code>public class MyWindow : Window { } </code></pre> <p>What I was going for is applying the background from a dynamic resource from the applications resource dictionary. </p> <p>I first went for this answer</p> <pre><code>public class MyWindow : Window { public MyWindow() { this.SetResourceReference(BackgroundProperty, "MyResourceKey"); } } </code></pre> <p>Now this worked for me when the Resource was a set color ie. </p> <pre><code>&lt;SolidColorBrush x:Key="MyResourceKey" Color="White"/&gt; </code></pre> <p>I found that when I set the resource reference to a system color I would get the issue you are getting. </p> <pre><code>&lt;SolidColorBrush x:Key="MyResourceKey" Color="{DynamicResource {x:Static SystemColors.WindowColorKey}}"/&gt; </code></pre> <p>It would work the first time but the 2nd time I'd get the parent freezable error. So my initial thought was, oh it's a threading issue I just need to invoke the dispatcher. Now this is where I got in a snag because I thought I needed to invoke it on the window. Not true. You need to invoke it on the dependency object of that resource.<br> The problem. You don't have the object using SetResourceReference because it looks for the resource and creates a reference to it. So what we need is the actual dependency object. To get the object from the resource you can do this.</p> <pre><code>object temp = this.TryFindResource("MyResourceKey"); </code></pre> <p>Now you have the object but this needs to be a dependency object. I haven't tried this but you may just be able to do this</p> <pre><code>DependencyObject temp = (DependencyObjet)this.TryFindResource("MyResourceKey"); </code></pre> <p>Now you have the dependency object! This is what is causing our threading issue with a freezable parent. Now we invoke the dispatcher on this object. I eventually ended up with something like this. This worked for me but I might try to clean it up a bit.</p> <pre><code>public class MyWindow: Window { public MyWindow() { SetResources(); } private void SetResources() { DependencyObject dependencyObject; object temp; temp = this.TryFindResource("MyResourceKey"); if (temp != null) { if (temp is DependencyObject) { dependencyObject = (DependencyObject)temp; if (!dependencyObject.CheckAccess()) { dependencyObject.Dispatcher.BeginInvoke(new System.Action(() =&gt; { this.SetResources(); })); } else { this.SetValue(BackgroundProperty, temp); } } } } } </code></pre> <p>Now this is just setting the background property. I believe this should work the same for a style. So you can do </p> <pre><code>this.SetValue(StyleProperty, temp) </code></pre> <p>Took a little bit to figure it out. But once I got it working I was exhilarated. It looks like the dependency object our resource is using is encountering a threading issue hence it's loading the first time but not the 2nd. The first time it's on the correct thread, but somewhere along the way another thread is set off. Yet to figure this one out. If someone has a better solution to this I would love to see it. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload