Windows icon indicating copy to clipboard operation
Windows copied to clipboard

ShadeEffect crashes with unhandled error with a ThemeResource

Open bjorn-malmo opened this issue 1 year ago • 1 comments

Describe the bug

Assigning a ThemeResource in XAML for the Color property of ShadeEffect throws the following exception:

Exception thrown: 'System.Runtime.InteropServices.COMException' in System.Private.CoreLib.dll WinRT information: Failed to assign to property 'CommunityToolkit.WinUI.UI.Media.ShadeEffect.Color'. [Line: 316 Position: 48] An exception of type 'System.Runtime.InteropServices.COMException' occurred in System.Private.CoreLib.dll but was not handled in user code

Using a StaticResource works great.

The following XAML code will reproduce the issue:

<Window
    x:Class="App1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:media="using:CommunityToolkit.WinUI.UI.Media">

    <Grid>
        <TextBlock Text="Text underneath a media effect"
                   HorizontalAlignment="Center"
                   VerticalAlignment="Center"/>

        <Grid Width="64" Height="64">
            <Grid.Background>
                <media:PipelineBrush Source="{media:BackdropSource}">
                    <media:BlurEffect Amount="2"/>
<!-- 
                    Any one of these will work:
                    
                    <media:ShadeEffect Color="Blue" Intensity="0.4"/>   
                    <media:ShadeEffect Color="{StaticResource SystemAccentColor}" Intensity="0.4"/> 
-->
                    
                    <!-- But this one doesn't -->
                    <media:ShadeEffect Color="{ThemeResource SystemAccentColor}" Intensity="0.4"/>
                    
                </media:PipelineBrush>
            </Grid.Background>
        </Grid>
    </Grid>
    
</Window>

Regression

No response

Reproducible in sample app?

  • [X] This bug can be reproduced in the sample app.

Steps to reproduce

1. Create a new WinUI application
2. Replace the XAML for MainWindow with the one in the description above
3. Start the application

Expected behavior

Expected theme resource to "just work" the same as a static resource.

Screenshots

No response

Windows Build Number

  • [ ] Windows 10 1809 (Build 17763)
  • [ ] Windows 10 1903 (Build 18362)
  • [ ] Windows 10 1909 (Build 18363)
  • [ ] Windows 10 2004 (Build 19041)
  • [ ] Windows 10 20H2 (Build 19042)
  • [ ] Windows 10 21H1 (Build 19043)
  • [X] Windows 11 21H2 (Build 22000)
  • [ ] Other (specify)

Other Windows Build number

No response

App minimum and target SDK version

  • [ ] Windows 10, version 1809 (Build 17763)
  • [ ] Windows 10, version 1903 (Build 18362)
  • [ ] Windows 10, version 1909 (Build 18363)
  • [X] Windows 10, version 2004 (Build 19041)
  • [ ] Other (specify)

Other SDK version

No response

Visual Studio Version

2022

Visual Studio Build Number

No response

Device form factor

Desktop

Nuget packages

CommunityToolkit.WinUI.UI.Media 7.1.2

Additional context

No response

Help us help you

No.

bjorn-malmo avatar Apr 10 '24 09:04 bjorn-malmo

I was wondering, maybe the color that you're trying to pass is a SolidBrushColor instead of Color, they are from different libs. I had this problem on my project but was a type problem when I was declaring my theme resources. Hope it works for you!

riqmariz avatar Apr 19 '24 17:04 riqmariz