Maui: [BUG] WinUI statecontainer crashes app

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

If you click too fast on the statecontainer change state button in sample, the app crashes due to task cancellation beeing thrown to cancel the state changing task

Expected Behavior

It should just cancel the state change task. Without an exception

Steps To Reproduce

Open windows Navigate to statecontainer Click any state changing button fast enough that a new state is requested before the current state is finished with transitioning.

App hard crash happens in windows, due to WinUI missing a global unhandled exception for async tasks (they have for voids though)

Link to public reproduction project repository

Use the sample app in this repo

Environment

- .NET MAUI CommunityToolkit:
- OS:
- .NET MAUI:
All environments and version of Maui which creates a WinUI application

Anything else?

I can fix this

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 17

Most upvoted comments

I’m still looking into the best way to resolve this Issue, but wanted to share a workaround for anyone experiencing this problem in the meantime:

Workaround

Set ShouldAnimateOnStateChange to false:

XAML

mct:StateContainer.ShouldAnimateOnStateChange="False"

C#

StateContainer.SetShouldAnimateOnStateChange(myLayout, false);

Explanation

The TaskCanceledException is thrown when CurrentState changes before the fade animation has completed.

When ShouldAnimateOnStateChange == false, the SwitchToState() and SwitchToContent() methods run synchronously (eg They don’t need to await the fade animations). This means the methods always complete immediately and never throw a TaskCanceledException.