DeferredReference
-
Monday, September 29, 2008 8:45 PMI am getting this:
A first chance exception of type 'System.InvalidCastException' occurred in WindowsBase.dll
An unhandled exception of type 'System.InvalidCastException' occurred in WindowsBase.dll
Additional information: Unable to cast object of type 'System.Windows.Media.SolidColorBrush' to type 'System.Windows.DeferredReference'.
When changing resource dictionaries to switch between skins, the program crashes but next time it looks fine and works OK (the skin actually changed).
I've seen this other post here:
http://forums.microsoft.com/MSDN/ShowPost.aspx?siteid=1&PostID=3939145
So maybe it is a bug? not sure.
Any solutions or workaround?
Thanks
David
DCProven
All Replies
-
Wednesday, October 01, 2008 5:37 PMFor now,
I am just asking the user to exit the application in order for the new skin to take effect. The skin works perfectly the first time, it's the second time that crashes.
I hate this solution, but I can't find any other way so far.
No other ideas?
Thanks.
David
DCProven -
Friday, October 03, 2008 5:48 AM
Could you please provide a small, complete and ready-to-run example to demonstrate the issue you are encountering?
Thanks
-
Friday, October 03, 2008 9:08 AMI am also seeing the same issue. Interestingly it is working fine on one machine and fails on another
Mine is a WPF application, configured to be deployed as click once application. The users getting the exception, do see the other skin being used when they start again as mentioned by David
Further investigation showed that this happens only when vista is running in classic theme and not on vista theme
MVP Client App- Edited by Atul Gupta Friday, October 03, 2008 9:17 AM added when the error is seen
-
Friday, October 03, 2008 10:03 AMfurther to previous post, I tried to debug using the symbol server and source code. it poped a dialog asking for path to ExecutionContext.cs (from threading folder), but I could not locate that file (didn't get downloaded). Cancelling it i got the exception displayed on the following code in AppDomain.cs
[MethodImplAttribute(MethodImplOptions.InternalCall)]
private extern int _nExecuteAssembly(Assembly assembly, String[] args);
internal int nExecuteAssembly(Assembly assembly, String[] args)
{
return _nExecuteAssembly(assembly.InternalAssembly, args);
}
Also as I said earlier, this happens when Vista is configured to run with classic theme. Marco, I guess you can take any WPF application that allows you to change skins dynamically at run time and you should be able to see this error
MVP Client App -
Monday, October 06, 2008 7:09 PMMarco,
email me if you want: david AT dolphinimaging DOT com
I'll send you what you need, I can't publish the code here as it is a commercial product, but I'll certainly help you with anything I can.
Again, it seems that when you have a nice enough theme (colors, image files, sizes, ...) changes it happens. It didn't happen when it was just a couple of color changes (first prototype).
Thanks
David.
DCProven -
Tuesday, October 07, 2008 7:00 AM
Okay, I finally come up with a smallest repro code, here it is:
<Window x:Class="SwitchSkinBug.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="_View" MenuItem.Click="OnViewMenuItemClick" >
<MenuItem Header="Red"/>
<MenuItem Header="Green"/>
<MenuItem Header="Blue"/>
</MenuItem>
</Menu>
<CheckBox
Content="Show"
Foreground="{DynamicResource TextBlock_Foreground}">
<CheckBox.Template>
<ControlTemplate TargetType="CheckBox">
<StackPanel>
<Path Fill="{TemplateBinding Foreground}"/>
<ContentPresenter
Content="{TemplateBinding ContentControl.Content}"/>
</StackPanel>
</ControlTemplate>
</CheckBox.Template>
</CheckBox>
</DockPanel>
</Window>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void OnViewMenuItemClick(Object sender, RoutedEventArgs e)
{
MenuItem menuItem = e.Source as MenuItem;
if (menuItem != null)
{
Uri uri = new Uri(menuItem.Header + ".xaml", UriKind.Relative);
ApplySkin(uri);
}
}
private ResourceDictionary loadedDictionary = null;
public void ApplySkin(Uri uri)
{
if (loadedDictionary != null)
{
this.Resources.MergedDictionaries.Remove(loadedDictionary);
}
ResourceDictionary currentDictionary = Application.LoadComponent(uri) as ResourceDictionary;
if (currentDictionary != null)
{
this.Resources.MergedDictionaries.Add(currentDictionary);
loadedDictionary = currentDictionary;
}
}
}
Here is the Skin dictionary for "Red":
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="TextBlock_Foreground" Color="Red"/>
</ResourceDictionary>
It seems that you could consistently reproduce this bug if you place a Path and a ContentPresenter inside the control template, and the Path's Fill property is templated bound to the Foreground property as follows:
<ControlTemplate TargetType="CheckBox">
<StackPanel>
<Path Fill="{TemplateBinding Foreground}"/>
<ContentPresenter
Content="{TemplateBinding ContentControl.Content}"/>
</StackPanel>
</ControlTemplate>
Both RadioButton and CheckBox has this usage pattern in his own control template, could you please confirm if you are skining the RadioButton or CheckBox controls in your own application?
I would greatly appreciate it if you could file a bug on this at the connect site:
https://connect.microsoft.com/feedback/default.aspx?SiteID=212&wa=wsignin1.0
You could refer to the repro project when feedbacking on this issue at the following link:
http://cid-e9ffaf5298e5a301.skydrive.live.com/self.aspx/Documents/WPF/CodeSamples/SwitchSkinBug.zip
Hope this helps
- Proposed As Answer by Atul Gupta Wednesday, October 08, 2008 4:29 PM
-
Tuesday, October 07, 2008 11:38 AM
In my application checkbox or radio button aren't templated like this. What i working on is the Family.Show application (http://www.codeplex.com/familyshow). The application allows you to change the theme between black and silver. if you set a vista machine with classic theme and then try running it, it should give you the error
MVP Client App -
Wednesday, October 08, 2008 6:08 AM-> if you set a vista machine with classic theme and then try running it, it should give you the error
I don't get any error when running the Family.show demo application.
But the code I pasted above consistently fails both under .NET Framework 3.5 and .NET Framework 3.5 SP1.
Thanks
-
Wednesday, October 08, 2008 4:29 PMi guess that is fine, since you have any confirmed that this is a bug
MVP Client App -
Wednesday, October 08, 2008 8:29 PMThanks Marco,
I'll file the bug report.
I appreciate your efforts on this one!
David
DCProven -
Sunday, October 04, 2009 9:37 AMWell, it's been a year... any progress? Any workarounds?Edit: Loading the resource in code rather than binding it as a dynamic resource in XAML seems to work.
-
Wednesday, December 16, 2009 11:17 PM
there is a knowledge base article and hotfix here:http://support.microsoft.com/kb/974477- Proposed As Answer by Skyguard Wednesday, December 16, 2009 11:18 PM

