Skip to content

[Windows] Fix for SemanticScreenReader.Default.Announce throws exception when called from MainPage constructor#30264

Merged
jfversluis merged 4 commits intodotnet:inflight/currentfrom
HarishwaranVijayakumar:fix-21121
Dec 2, 2025
Merged

[Windows] Fix for SemanticScreenReader.Default.Announce throws exception when called from MainPage constructor#30264
jfversluis merged 4 commits intodotnet:inflight/currentfrom
HarishwaranVijayakumar:fix-21121

Conversation

@HarishwaranVijayakumar
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!

Issue Details

  • When SemanticScreenReader.Default.Announce() is called from the MainPage constructor, it throws a System.NullReferenceException: "Object reference not set to an instance of an object."

Root Cause of the issue

  • During MainPage construction, window.Content is null, which causes FindAutomationPeer to return null. As a result, the call to peer.RaiseNotificationEvent(...) throws a NullReferenceException.

Description of Change

  • Added a null check for window.Content in the Announce method and exited early to prevent a potential exception during early application initialization.

Issues Fixed

Fixes #21121

Tested the behaviour in the follwing platforms

  • - Windows
  • - macOS
  • - Android
  • - iOS

Output

Before Fix After Fix

@dotnet-policy-service dotnet-policy-service bot added the community ✨ Community Contribution label Jun 27, 2025
@dotnet-policy-service
Copy link
Contributor

Hey there @@HarishwaranVijayakumar! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@dotnet-policy-service dotnet-policy-service bot added the partner/syncfusion Issues / PR's with Syncfusion collaboration label Jun 27, 2025
@jsuarezruiz jsuarezruiz added platform/windows t/a11y Relates to accessibility labels Jun 27, 2025
Copy link
Contributor

@jsuarezruiz jsuarezruiz left a comment

Choose a reason for hiding this comment

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

Could include a related test?

@HarishwaranVijayakumar
Copy link
Contributor Author

@jsuarezruiz, I attempted to write a test case for this scenario by invoking SemanticScreenReader.Announce() in the constructor. However, during test execution, the issue did not reproduce, and the test passed even without the fix. The exception was only observed in the HostApp when the page was instantiated directly using return mainPage ?? new Issue21121(). When the page was navigated to using CoreNavigationPage, the issue did not occur.

@HarishwaranVijayakumar HarishwaranVijayakumar marked this pull request as ready for review June 27, 2025 12:50
@HarishwaranVijayakumar HarishwaranVijayakumar requested a review from a team as a code owner June 27, 2025 12:50
{
// If the window content is null, we can't announce anything yet.
// This can happen if the app is still starting up.
return;
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn’t exactly fix the issue in question, right?

We’re just avoiding the crash when window.Content is null. But this ends up silently skipping the announcement if the visual tree isn’t ready. Now the user gets no feedback, the announcement just doesn’t happen. No crash, no warning, just silence.

From what I understand, on Windows, we need to locate a control with an automation peer to perform the announce operation. If that’s the expected behavior, maybe we shouldn’t silently skip here.

Instead, could we consider throwing a meaningful exception with a helpful message, so users know they’re calling Announce() too early and can fix it by moving the call to a better place?

Copy link
Contributor

Choose a reason for hiding this comment

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

@bhavanesh2001 Throwing an exception in an accessibility scenario is not an appropriate approach. Accessibility features are intended to enhance the user experience, not disrupt it. If a screen reader announcement fails, the application should continue functioning normally.

A better approach would be to return silently along with logging the issue using diagnostic tools such as Debug.WriteLine. This ensures that the failure is recorded for developers without affecting the user experience. In the Essentials project, diagnostic logging is commonly used to capture such errors without interrupting the app’s flow.
https://github.com/dotnet/maui/blob/main/src/Essentials/src/Permissions/Permissions.ios.cs#L38

@PureWeen, your thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

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

@sheiksyedm Just to clarify ,I wasn’t suggesting we should throw on end users’ devices. My point was to help devs catch the issue early, rather than silently skipping the announcement without any signal.

By users, I meant devs using the API, not app users.

Copilot AI review requested due to automatic review settings December 2, 2025 13:14
@jfversluis jfversluis changed the base branch from main to inflight/current December 2, 2025 13:15
@jfversluis jfversluis added this to the .NET 10.0 SR2 milestone Dec 2, 2025
@jfversluis jfversluis merged commit 203f72e into dotnet:inflight/current Dec 2, 2025
4 of 5 checks passed
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 Windows-specific crash where SemanticScreenReader.Default.Announce() throws a NullReferenceException when called during early application initialization (e.g., from MainPage constructor). The fix adds a null check for window.Content before attempting to find the automation peer.

Key changes:

  • Adds null check in Windows SemanticScreenReader implementation to prevent crashes during early initialization
  • Updates maui-mobile template with visual refinements (Border background handling, Visual State Management improvements)
  • Adds UI test infrastructure improvements (Done button accessibility, test coverage for various issues)
  • Platform-specific fixes for iOS (WebView navigation, SwipeView, Slider tap-to-move, TabBar iOS 18/26 handling)
  • Android fixes (activity result registration, SwipeView touch propagation)
  • Windows fixes (ContentView background, transformation NaN handling)

Reviewed changes

Copilot reviewed 1 out of 1 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/Essentials/src/SemanticScreenReader/SemanticScreenReader.windows.cs Core fix: Adds null check for window.Content to prevent NullReferenceException during early initialization
src/Templates/src/templates/maui-mobile/* Template refinements: Border background moved from global style to specific usage, improved Visual State setters consistency
src/TestUtils/src/Microsoft.Maui.IntegrationTests/WindowsTemplateTest.cs Adds test for Microsoft.Identity.Client package compatibility with Windows template
src/Essentials/src/Platform/ActivityForResultRequest.android.cs Prevents activity launcher re-registration from temporary activities
src/Core/src/Handlers/ContentView/ContentViewHandler.Windows.cs Adds IsHitTestVisible and background handling for ContentView on Windows
src/Core/src/Handlers/Slider/SliderHandler.iOS.cs Implements tap-to-move functionality for iOS Slider
src/Core/src/Platform/iOS/MauiDoneAccessoryView.cs Adds accessibility identifier to Done button for UI testing
src/Core/src/Platform/iOS/TabbedViewExtensions.cs Adds iOS 26+ check to disable iOS 18 toolbar tab workaround
src/Core/src/Platform/iOS/MauiWebViewNavigationDelegate.cs Fixes target="_blank" link handling in WebView
src/Core/src/Platform/iOS/MauiSwipeView.cs Fixes SwipeView threshold calculation for Left/Up directions
src/Core/src/Platform/Android/MauiSwipeView.cs Prevents parent touch propagation during swipe gestures
src/Core/src/Platform/Windows/TransformationExtensions.cs Adds NaN validation for rotation values
src/Core/tests/DeviceTests/Services/ImageSource/StreamImageSourceServiceTests.Android.cs Re-enables Android test with improved color assertion
src/Controls/tests/Xaml.UnitTests/Issues/Maui28711.xaml* Adds test for x:Name on SolidColorBrush in Resources
src/Controls/tests/Xaml.UnitTests/Issues/Maui27202.xaml* Adds test for Visual State Manager inheritance in derived styles
src/Controls/tests/UITests/Tests/Issues/Issue15810.cs Adds UI test for TapGestureRecognizer on Windows
src/Controls/tests/TestCases/Issues/Issue15810.cs Adds test case for TapGestureRecognizer issue
src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/MauiDoneAccessoryButton.cs Adds UI test for Done button accessibility
src/Controls/tests/TestCases.iOS.Tests/snapshots/ios/* Screenshot snapshots for iOS UI tests
src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/* Screenshot snapshots for Windows UI tests

PureWeen pushed a commit that referenced this pull request Dec 3, 2025
…ion when called from MainPage constructor (#30264)

* Added fix

* Modified fix

* Space added

* Updated the fix
PureWeen pushed a commit that referenced this pull request Dec 3, 2025
…ion when called from MainPage constructor (#30264)

* Added fix

* Modified fix

* Space added

* Updated the fix
PureWeen pushed a commit that referenced this pull request Dec 3, 2025
…ion when called from MainPage constructor (#30264)

* Added fix

* Modified fix

* Space added

* Updated the fix
PureWeen pushed a commit that referenced this pull request Dec 3, 2025
…ion when called from MainPage constructor (#30264)

* Added fix

* Modified fix

* Space added

* Updated the fix
PureWeen pushed a commit that referenced this pull request Dec 3, 2025
…ion when called from MainPage constructor (#30264)

* Added fix

* Modified fix

* Space added

* Updated the fix
github-actions bot pushed a commit that referenced this pull request Dec 5, 2025
…ion when called from MainPage constructor (#30264)

* Added fix

* Modified fix

* Space added

* Updated the fix
@github-actions github-actions bot locked and limited conversation to collaborators Jan 2, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

community ✨ Community Contribution partner/syncfusion Issues / PR's with Syncfusion collaboration platform/windows t/a11y Relates to accessibility

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SemanticScreenReader.Default.Announce throws exception when called from MainPage constructor

6 participants