dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.13k stars 1.74k forks source link

[Windows] Setter to Shadow when the VisualState is PointerOver causes a crash #14454

Open jonghoon023 opened 1 year ago

jonghoon023 commented 1 year ago

Description

Hello MAUI team! I have added a VisualState Property to a Button to give it a shadow effect when the mouse is Hovered over it. However, when I click on the Button, I get the following exception and it crashes. I would appreciate it if you could tell me how to solve this problem.

The object has been closed. (0x80000013)

https://user-images.githubusercontent.com/38038052/230539891-ae1e9613-a033-4c6f-a3df-c1fe7cbe3524.mp4

Steps to Reproduce

  1. Add Source to Style.xaml (Resources -> Styles -> Styles.xaml)

    <Style
        x:Key="CrashButtonStyle"
        TargetType="Button">
        <Setter
            Property="CornerRadius"
            Value="4" />
        <Setter
            Property="TextColor"
            Value="White" />
        <Setter
            Property="BackgroundColor"
            Value="Orange" />
        <Setter
            Property="VisualStateManager.VisualStateGroups">
            <VisualStateGroupList>
                <VisualStateGroup
                    x:Name="CommonStates">
                    <VisualState
                        x:Name="Normal" />
    
                    <VisualState
                        x:Name="Disabled">
                        <VisualState.Setters>
                            <Setter
                                Property="BackgroundColor"
                                Value="DarkGray" />
                        </VisualState.Setters>
                    </VisualState>
    
                    <VisualState
                        x:Name="PointerOver">
                        <VisualState.Setters>
                            <Setter
                                Property="Shadow">
                                <Setter.Value>
                                    <Shadow
                                        Brush="Skyblue" />
                                </Setter.Value>
                            </Setter>
                        </VisualState.Setters>
                    </VisualState>
    
                    <VisualState
                        x:Name="Pressed">
                        <VisualState.Setters>
                            <Setter
                                Property="BackgroundColor"
                                Value="Violet" />
                        </VisualState.Setters>
                    </VisualState>
                </VisualStateGroup>
            </VisualStateGroupList>
        </Setter>
    </Style>
  2. Replace Source for MainPage.xaml

<ContentPage
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="Maui.Shadow.Crash.MainPage">

    <VerticalStackLayout
        Spacing="50">
        <Button
            Style="{StaticResource CrashButtonStyle}"
            ImageSource="dotnet_bot.png"
            ContentLayout="Left, 8"
            HeightRequest="50"
            Text="Crash Button" />

        <Button
            ImageSource="dotnet_bot.png"
            ContentLayout="Left, 8"
            HeightRequest="50"
            Text="No Crash Button">
            <Button.Style>
                <Style
                    TargetType="Button">
                    <Setter
                        Property="CornerRadius"
                        Value="4" />
                    <Setter
                        Property="TextColor"
                        Value="Red" />
                    <Setter
                        Property="BackgroundColor"
                        Value="GreenYellow" />
                    <Setter
                        Property="VisualStateManager.VisualStateGroups">
                        <VisualStateGroupList>
                            <VisualStateGroup
                                x:Name="CommonStates">
                                <VisualState
                                    x:Name="Normal" />

                                <VisualState
                                    x:Name="Disabled">
                                    <VisualState.Setters>
                                        <Setter
                                            Property="BackgroundColor"
                                            Value="DarkGray" />
                                    </VisualState.Setters>
                                </VisualState>

                                <VisualState
                                    x:Name="PointerOver">
                                    <VisualState.Setters>
                                        <Setter
                                            Property="BackgroundColor"
                                            Value="MintCream" />
                                    </VisualState.Setters>
                                </VisualState>

                                <VisualState
                                    x:Name="Pressed">
                                    <VisualState.Setters>
                                        <Setter
                                            Property="BackgroundColor"
                                            Value="DarkGoldenrod" />
                                    </VisualState.Setters>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateGroupList>
                    </Setter>
                </Style>
            </Button.Style>
        </Button>
    </VerticalStackLayout>

</ContentPage>

Link to public reproduction project repository

https://github.com/JongHoon023/Maui.Shadow.Issue

Version with bug

7.0 (current)

Last version that worked well

Unknown/Other

Affected platforms

Windows

Affected platform versions

Editon : Windows 11 Pro Version : 22H2

Did you find any workaround?

No response

Relevant log output

No response

jonghoon023 commented 1 year ago

Windows App SDK

Version : 1.2.221109.1

image

jonghoon023 commented 1 year ago

Related issues https://github.com/dotnet/maui/issues/7316

SarthakB26 commented 1 year ago

Honestly VisualStates is very unstable right now.

ghost commented 1 year ago

We've added this issue to our backlog, and we will work to address it as time and resources allow. If you have any additional information or questions about this issue, please leave a comment. For additional info about issue management, please read our Triage Process.

tinhmdxdev commented 1 year ago

I got the same issue.

homeyf commented 9 months ago

Verified this issue with Visual Studio Enterprise 17.9.0 Preview 2. Not repro on windows platforms with sample project. https://github.com/JongHoon023/Maui.Shadow.Issue image

ghost commented 9 months ago

Hi @JongHoon023. We have added the "s/try-latest-version" label to this issue, which indicates that we'd like you to try and reproduce this issue on the latest available public version. This can happen because we think that this issue was fixed in a version that has just been released, or the information provided by you indicates that you might be working with an older version.

You can install the latest version by installing the latest Visual Studio (Preview) with the .NET MAUI workload installed. If the issue still persists, please let us know with any additional details and ideally a reproduction project provided through a GitHub repository.

This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

jonghoon023 commented 9 months ago

Visual Studio Enterprise 17.9.0 미리 보기 2에서 이 문제를 확인했습니다. 샘플 프로젝트가 있는 Windows 플랫폼에서는 재현되지 않습니다. https://github.com/JongHoon023/Maui.Shadow.Issue 이미지

Thank for the comment. I have installed Visual Studio 2022 Preview 2.0 as you said, and after running it, I am getting the same error.

https://github.com/dotnet/maui/assets/38038052/c20bb69c-6dd2-45af-853d-e460ad8d920b

Exception Message

The object has been closed.

The given object has already been closed / disposed and may no longer be used.

Stack Trace

   / WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|39_0(Int32 hr)
   / ABI.Microsoft.UI.Composition.IDropShadowMethods.set_Mask(IObjectReference _obj, CompositionBrush value)
   / Microsoft.Maui.Platform.WrapperView.<CreateShadowAsync>d__19.MoveNext()
   / Microsoft.Maui.Platform.WrapperView.<ShadowChanged>d__36.MoveNext()
   / System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_0(Object state)
   / Microsoft.UI.Dispatching.DispatcherQueueSynchronizationContext.<>c__DisplayClass2_0.<Post>b__0()