Skip to content

Conversation

@BagavathiPerumal
Copy link
Contributor

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 issue occurs because VisualStateManager setters with TargetName rely on FindByName(), which only resolves elements within the same namescope. Since ControlTemplates create a new namescope, visual states defined inside a template cannot access elements outside it, resulting in the crash: "Cannot resolve [TargetName] as Setter Target."

Fix Description:

The fix involves using element.FindByName() for targets within the same or child namescopes and, if the target is not found, traversing the parent hierarchy to inspect each parent element’s namescope. This approach enables elements defined within a control template to resolve a TargetName outside their own namescope, ensuring reliable target resolution while maintaining compatibility with existing visual state behavior.

Issues Fixed

Fixes #26977

Tested the behaviour in the following platforms

  • Android
  • Windows
  • iOS
  • Mac

Output Screenshot

Before Issue Fix After Issue Fix
26977-BeforeFix.mov
26977-AfterFix.mov

…lution across ControlTemplate namescope boundaries
Copy link
Contributor

Copilot AI left a 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 crash in VisualStateManager when using Setter.TargetName with a ControlTemplate. The root cause is that FindByName() only resolves elements within the same namescope, and ControlTemplates create new namescopes. The fix adds a FindTargetByName() helper method that traverses the parent hierarchy to inspect each parent's namescope when the target is not found in the current scope.

Key Changes

  • Added namescope traversal logic to resolve targets across ControlTemplate boundaries
  • Created UI tests to validate the fix on all platforms
  • Improved reliability of visual state setters with named targets in templated scenarios

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
src/Controls/src/Core/Setter.cs Introduced FindTargetByName() method to walk up parent tree when resolving TargetName, enabling cross-namescope resolution for ControlTemplates
src/Controls/tests/TestCases.HostApp/Issues/Issue26977.xaml Added UI test page demonstrating VisualStateManager with Setter.TargetName inside a ControlTemplate
src/Controls/tests/TestCases.HostApp/Issues/Issue26977.xaml.cs Implemented code-behind with state toggling logic for the test scenario
src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue26977.cs Added NUnit Appium test to verify the fix doesn't crash and correctly applies visual state setters

@@ -0,0 +1,20 @@
namespace Maui.Controls.Sample.Issues;

[Issue(IssueTracker.Github, 26977, "Setter.TargetName + ControlTemplate crash", PlatformAffected.Android)]
Copy link

Copilot AI Dec 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PlatformAffected should be set to PlatformAffected.All instead of PlatformAffected.Android. According to the PR description, this fix was tested on Android, Windows, iOS, and Mac. The issue is a VisualStateManager namescope resolution problem that affects all platforms where ControlTemplates create namescope boundaries, not just Android.

Suggested change
[Issue(IssueTracker.Github, 26977, "Setter.TargetName + ControlTemplate crash", PlatformAffected.Android)]
[Issue(IssueTracker.Github, 26977, "Setter.TargetName + ControlTemplate crash", PlatformAffected.All)]

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community ✨ Community Contribution partner/syncfusion Issues / PR's with Syncfusion collaboration

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Setter.TargetName + ControlTemplate crash

1 participant