Description
Description
Due to our recent efforts in optimizing the performance of dynamic resources, the incorrect initialization of such resources might now lead to crashing applications, given an incorrect type of resource is used. Previously, this would have resulted in an InvalidOperationException
, which would have been consumed without causing the crash.
Version
.NET 10 Preview 4
Previous behavior
Applications using the DynamicResources
incorrectly would go on to run without crashing, although, the values would fallback to default. Also, as mentioned earlier, the InvalidOperationException
would come up in outputs.
New behavior
The application would crash with error reading something like this: System.Windows.Markup.XamlParseException: Set property 'System.Windows.ResourceDictionary.Source' threw an exception.
Type of breaking change
- Binary incompatible: Existing binaries might encounter a breaking change in behavior, such as failure to load or execute, and if so, require recompilation.
- Source incompatible: When recompiled using the new SDK or component or to target the new runtime, existing source code might require source changes to compile successfully.
- Behavioral change: Existing binaries might behave differently at run time.
Reason for change
This change is a direct consequence of our efforts to gain time performance while using DynamicResources
. The PR that caused this change: dotnet/wpf#10532
Recommended action
An example that could lead to such a crash would be:
<SolidColorBrush x:Key="RedColorBrush" Color="#FFFF0000" />
<SolidColorBrush x:Key="ResourceName" Color="{DynamicResource RedColorBrush}" />
The reason of this exception being thrown is that we have used a SolidColorBrush
instead of System.Windows.Media.Color
. Changing the code to the snippet below would fix the issue.
<Color x:Key="RedColor">#FFFF0000</Color>
<SolidColorBrush x:Key="ResourceName" Color="{DynamicResource RedColor}" />
As explained in the snippet above, this can be easily mitigated by the developers by using the correct resource types. Following links might be helpful in understanding the change further:
- Optimizing DynamicResources PR: Improving DynamicResource performance (second try) wpf#10532 (As also mentioned earlier)
- Discussions around this particular issue: Improve performance of DynamicResource usages wpf#5610 (comment)
- An issue filed for the same: [dotnet-sdk-10.0.100-preview.4.25224.103] Fail to launch Text-Grab with error: System.Windows.Markup.XamlParseException: Set property 'System.Windows.ResourceDictionary.Source' threw an exception. wpf#10820
Feature area
Windows Presentation Foundation (WPF)
Affected APIs
No response
Metadata
Metadata
Assignees
Type
Projects
Status