Skip to content

Fix EmojiCompat crash when Entry bound to float with StringFormat #30791

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 23, 2025

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!

Description

Fixes a crash that occurs when an Entry is bound to a float value with fractional string formatting (e.g., {0:F2}) on Android devices with EmojiCompat enabled.

Problem

When users type in an Entry bound to a float with StringFormat='{}{0:F2}', the app crashes with:

Java.Lang.IllegalArgumentException: 'end should be < than charSequence length'

Root Cause: When a user types "1", the StringFormat converts it to "1.00", changing the text length from 1 to 4 characters. EmojiCompat gets confused about cursor positioning when SetTextKeepState() is called with the new text length, causing the crash.

Reproduction:

<Entry Text="{Binding FloatValue, StringFormat='{}{0:F2}'}" />
  1. Clear the entry field completely
  2. Type a number (e.g., "1", "3.5")
  3. App crashes on physical Android devices

Solution

Added a try-catch block around SetTextKeepState() in the UpdateTextFromPlatform() method:

  • Normal case: SetTextKeepState() works as before, preserving cursor position
  • EmojiCompat crash case: Falls back to regular setText() with manual cursor positioning
try
{
    editText.SetTextKeepState(newText);
}
catch (Java.Lang.IllegalArgumentException)
{
    // Fallback to regular setText with manual cursor positioning
    var cursorPosition = editText.SelectionStart;
    editText.Text = newText;
    var newCursorPosition = System.Math.Min(cursorPosition, newText?.Length ?? 0);
    if (newCursorPosition <= editText.Text?.Length)
    {
        editText.SetSelection(newCursorPosition);
    }
}

Benefits

  • Prevents the crash - No more IllegalArgumentException
  • Minimal impact - Only affects the specific problematic scenario
  • Works for Entry and Editor - Both controls use the same code path
  • Preserves functionality - Maintains cursor positioning when possible
  • No breaking changes - Existing behavior unchanged in normal cases

Testing

Added comprehensive test case Issue25728:

  • UI reproduction page for manual testing
  • Automated tests verifying no crashes when typing numbers with StringFormat
  • Edge case coverage: decimals, zero, programmatic value changes

Microsoft Docs

Fixes #25728.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Add try-catch around SetTextKeepState in UpdateTextFromPlatform to handle IllegalArgumentException thrown by EmojiCompat when text length changes during formatting. Falls back to regular setText with manual cursor positioning.

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] App crashes when entry bound to float value with fractional format Fix EmojiCompat crash when Entry bound to float with StringFormat Jul 23, 2025
@Copilot Copilot AI requested a review from jfversluis July 23, 2025 14:14
Copilot finished work on behalf of jfversluis July 23, 2025 14:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

App crashes when entry bound to float value with fractional format
2 participants