-
Notifications
You must be signed in to change notification settings - Fork 1.8k
[Windows] Fixed CanvasDrawingSession Exception on Clipping Image #30028
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes a Windows-only CanvasDrawingSessionException
by resetting the canvas state after each draw, and adds a full repro via HostApp and automated tests.
- Adds
_canvas.ResetState()
inOnDraw
to ensure the canvas is cleared after drawing. - Introduces a new UI page (
Issue18430
) with clipping drawable and embeds the required image resource. - Adds shared and host-app test cases to validate that no exception is thrown.
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
File | Description |
---|---|
src/Graphics/src/Graphics/Platforms/Windows/PlatformGraphicsView.cs | Call ResetState() after drawing to clear state |
src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue18430.cs | New shared UI test checking that the page loads |
src/Controls/tests/TestCases.HostApp/Issues/Issue18430.cs | HostApp page and clipping drawable for repro |
src/Controls/tests/TestCases.HostApp/Controls.TestCases.HostApp.csproj | Embed royals.png so the drawable can load the image |
_canvas.Session = args.DrawingSession; | ||
_canvas.CanvasSize = new global::Windows.Foundation.Size(_dirty.Width, _dirty.Height); | ||
_drawable.Draw(_canvas, _dirty); | ||
_canvas.ResetState(); | ||
PlatformGraphicsService.ThreadLocalCreator = null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider wrapping the draw logic (setting session, size, and calling Draw) and the ResetState call in a try/finally block so that the canvas state is always reset even if _drawable.Draw(...)
throws an exception.
_canvas.Session = args.DrawingSession; | |
_canvas.CanvasSize = new global::Windows.Foundation.Size(_dirty.Width, _dirty.Height); | |
_drawable.Draw(_canvas, _dirty); | |
_canvas.ResetState(); | |
PlatformGraphicsService.ThreadLocalCreator = null; | |
try | |
{ | |
_canvas.Session = args.DrawingSession; | |
_canvas.CanvasSize = new global::Windows.Foundation.Size(_dirty.Width, _dirty.Height); | |
_drawable.Draw(_canvas, _dirty); | |
} | |
finally | |
{ | |
_canvas.ResetState(); | |
PlatformGraphicsService.ThreadLocalCreator = null; | |
} |
Copilot uses AI. Check for mistakes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the suggestion is correct.
_canvas.Session = args.DrawingSession; | ||
_canvas.CanvasSize = new global::Windows.Foundation.Size(_dirty.Width, _dirty.Height); | ||
_drawable.Draw(_canvas, _dirty); | ||
_canvas.ResetState(); | ||
PlatformGraphicsService.ThreadLocalCreator = null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the suggestion is correct.
@jsuarezruiz I have modified the suggested changes |
/azp run |
Azure Pipelines successfully started running 3 pipeline(s). |
Note
Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!
Root Cause
The exception CanvasDrawingSessionException occurred because the Canvas state was not reset after drawing
Description of Change
Added a call to _canvas.ResetState() in the OnDraw method to ensure the canvas state is properly reset after drawing.
Tested the behavior in the following platforms:
Issues Fixed
Fixes #18430
Screenshot