Closed juniorsaraviao closed 1 year ago
This issue seems to occur when binding a string longer than the length specified in MaxLength of Entry, Editor to TextProperty.
The startPosition and endPosition obtained in the SetTextRange method of the TextInputExtions class were null. You should put a null check before calling GetTextRange method or consider MaxLength when calling GetPosition method.
Below is the source code of .NET MAUI side.
[src\Core\src\Platform\iOS\TextInputExtensions.cs]
internal static void SetTextRange(this IUITextInput platformView, int start, int selectedTextLength)
{
int end = start + selectedTextLength;
// Let's be sure we have positive positions
start = Math.Max(start, 0);
end = Math.Max(end, 0);
// Switch start and end positions if necessary
start = Math.Min(start, end);
end = Math.Max(start, end);
var startPosition = platformView.GetPosition(platformView.BeginningOfDocument, start);
var endPosition = platformView.GetPosition(platformView.BeginningOfDocument, end);
platformView.SelectedTextRange = platformView.GetTextRange(startPosition, endPosition);
}
If it is only a null check, it can be solved as follows.
[src\Core\src\Platform\iOS\TextInputExtensions.cs]
internal static void SetTextRange(this IUITextInput platformView, int start, int selectedTextLength)
{
int end = start + selectedTextLength;
// Let's be sure we have positive positions
start = Math.Max(start, 0);
end = Math.Max(end, 0);
// Switch start and end positions if necessary
start = Math.Min(start, end);
end = Math.Max(start, end);
var startPosition = platformView.GetPosition(platformView.BeginningOfDocument, start);
var endPosition = platformView.GetPosition(platformView.BeginningOfDocument, end);
if (startPosition is not null && endPosition is not null)
{
platformView.SelectedTextRange = platformView.GetTextRange(startPosition, endPosition);
}
}
Or if you consider MaxLength,
[src\Core\src\Platform\iOS\TextInputExtensions.cs]
internal static void SetTextRange(this IUITextInput platformView, int start, int selectedTextLength, int maxLength)
{
int end = start + selectedTextLength;
// Let's be sure we have positive positions
start = Math.Max(start, 0);
end = Math.Max(end, 0);
// Switch start and end positions if necessary
start = Math.Min(start, end);
end = Math.Max(start, end);
if (maxLength >= 0)
{
start = Math.Min(start, maxLength);
end = Math.Min(end, maxLength);
}
var startPosition = platformView.GetPosition(platformView.BeginningOfDocument, start);
var endPosition = platformView.GetPosition(platformView.BeginningOfDocument, end);
platformView.SelectedTextRange = platformView.GetTextRange(startPosition, endPosition);
}
[src\Controls\src\Core\Platform\iOS\Extensions\TextExtensions.cs]
public static void UpdateText(this UITextView textView, InputView inputView)
{
// Setting the text causes the cursor to be reset to the end of the UITextView.
// So, let's set back the cursor to the last known position and calculate a new
// position if needed when the text was modified by a Converter.
var oldText = textView.Text ?? string.Empty;
var newText = TextTransformUtilites.GetTransformedText(
inputView?.Text,
textView.SecureTextEntry ? TextTransform.Default : inputView.TextTransform
);
// Re-calculate the cursor offset position if the text was modified by a Converter.
// but if the text is being set by code, let's just move the cursor to the end.
var cursorOffset = newText.Length - oldText.Length;
var cursorPosition = textView.IsFirstResponder ? textView.GetCursorPosition(cursorOffset) : newText.Length;
if (oldText != newText)
textView.Text = newText;
textView.SetTextRange(cursorPosition, 0, inputView.MaxLength);
}
public static void UpdateText(this UITextField textField, InputView inputView)
{
// Setting the text causes the cursor to be reset to the end of the UITextView.
// So, let's set back the cursor to the last known position and calculate a new
// position if needed when the text was modified by a Converter.
var oldText = textField.Text ?? string.Empty;
var newText = TextTransformUtilites.GetTransformedText(
inputView?.Text,
textField.SecureTextEntry ? TextTransform.Default : inputView.TextTransform
);
// Re-calculate the cursor offset position if the text was modified by a Converter.
// but if the text is being set by code, let's just move the cursor to the end.
var cursorOffset = newText.Length - oldText.Length;
var cursorPosition = textField.IsEditing ? textField.GetCursorPosition(cursorOffset) : newText.Length;
if (oldText != newText)
textField.Text = newText;
textField.SetTextRange(cursorPosition, 0, inputView.MaxLength);
}
Additional Information: This issue also occurs on Android. Below is the exception information.
[HostConnection] HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_native_sync_v3 ANDROID_EMU_native_sync_v4 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_vulkan ANDROID_EMU_deferred_vulkan_commands ANDROID_EMU_vulkan_null_optional_strings ANDROID_EMU_vulkan_create_resources_with_requirements ANDROID_EMU_YUV_Cache ANDROID_EMU_vulkan_ignored_handles ANDROID_EMU_has_shared_slots_host_memory_allocator ANDROID_EMU_vulkan_free_memory_sync ANDROID_EMU_vulkan_shader_float16_int8 ANDROID_EMU_vulkan_async_queue_submit ANDROID_EMU_sync_buffer_data ANDROID_EMU_read_color_buffer_dma GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_host_side_tracing ANDROID_EMU_gles_max_version_2 Java.Lang.IndexOutOfBoundsException: 'setSpan (11 ... 11) ends beyond length 10'
Java.Lang.IndexOutOfBoundsException: 'setSpan (11 ... 11) ends beyond length 10'
[mono-rt] [ERROR] FATAL UNHANDLED EXCEPTION: Java.Lang.IndexOutOfBoundsException: setSpan (11 ... 11) ends beyond length 10
[mono-rt] at Java.Interop.JniEnvironment.InstanceMethods.CallNonvirtualVoidMethod(JniObjectReference instance, JniObjectReference type, JniMethodInfo method, JniArgumentValue args) in /Users/runner/work/1/s/xamarin-android/external/Java.Interop/src/Java.Interop/obj/Release/net7.0/JniEnvironment.g.cs:line 12324
[mono-rt] at Java.Interop.JniPeerMembers.JniInstanceMethods.InvokeVirtualVoidMethod(String encodedMember, IJavaPeerable self, JniArgumentValue parameters) in /Users/runner/work/1/s/xamarin-android/external/Java.Interop/src/Java.Interop/Java.Interop/JniPeerMembers.JniInstanceMethods_Invoke.cs:line 75
[mono-rt] at Android.Widget.EditText.SetSelection(Int32 index) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/net7.0/android-33/mcw/Android.Widget.EditText.cs:line 208
[mono-rt] at Microsoft.Maui.Controls.Platform.EditTextExtensions.UpdateText(EditText editText, InputView inputView) in C:\dotnet-maui-work\src\Controls\src\Core\Platform\Android\Extensions\EditTextExtensions.cs:line 46
[mono-rt] at Microsoft.Maui.Controls.Editor.MapText(IEditorHandler handler, Editor editor) in C:\dotnet-maui-work\src\Controls\src\Core\Editor\Editor.Android.cs:line 21
[mono-rt] at Microsoft.Maui.Controls.Editor.MapText(EditorHandler handler, Editor editor) in C:\dotnet-maui-work\src\Controls\src\Core\Editor\Editor.Android.cs:line 16
[mono-rt] at Microsoft.Maui.PropertyMapper2.<>c__DisplayClass5_0[[Microsoft.Maui.Controls.Editor, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Handlers.EditorHandler, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].<Add>b__0(IElementHandler h, IElement v) in C:\dotnet-maui-work\src\Core\src\PropertyMapper.cs:line 172 [mono-rt] at Microsoft.Maui.PropertyMapper.UpdatePropertyCore(String key, IElementHandler viewHandler, IElement virtualView) in C:\dotnet-maui-work\src\Core\src\PropertyMapper.cs:line 47 [mono-rt] at Microsoft.Maui.PropertyMapper.UpdateProperties(IElementHandler viewHandler, IElement virtualView) in C:\dotnet-maui-work\src\Core\src\PropertyMapper.cs:line 82 [mono-rt] at Microsoft.Maui.Handlers.ElementHandler.SetVirtualView(IElement view) in C:\dotnet-maui-work\src\Core\src\Handlers\Element\ElementHandler.cs:line 79 [mono-rt] at Microsoft.Maui.Handlers.ViewHandler
2[[Microsoft.Maui.IEditor, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[AndroidX.AppCompat.Widget.AppCompatEditText, Xamarin.AndroidX.AppCompat, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].SetVirtualView(IView view) in C:\dotnet-maui-work\src\Core\src\Handlers\View\ViewHandlerOfT.cs:line 53
[mono-rt] at Microsoft.Maui.Handlers.EditorHandler.SetVirtualView(IView view) in C:\dotnet-maui-work\src\Core\src\Handlers\Editor\EditorHandler.Android.cs:line 34
[mono-rt] at Microsoft.Maui.Handlers.ViewHandler2[[Microsoft.Maui.IEditor, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[AndroidX.AppCompat.Widget.AppCompatEditText, Xamarin.AndroidX.AppCompat, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].SetVirtualView(IElement view) in C:\dotnet-maui-work\src\Core\src\Handlers\View\ViewHandlerOfT.cs:line 56 [mono-rt] at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler newHandler) in C:\dotnet-maui-work\src\Controls\src\Core\Element\Element.cs:line 831 [mono-rt] at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value) in C:\dotnet-maui-work\src\Controls\src\Core\Element\Element.cs:line 781 [mono-rt] at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.set_Handler(IElementHandler value) in C:\dotnet-maui-work\src\Controls\src\Core\VisualElement\VisualElement.cs:line 1660 [mono-rt] at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement view, IMauiContext context) in C:\dotnet-maui-work\src\Core\src\Platform\ElementExtensions.cs:line 96 [mono-rt] at Microsoft.Maui.Platform.ElementExtensions.ToPlatform(IElement view, IMauiContext context) in C:\dotnet-maui-work\src\Core\src\Platform\ElementExtensions.cs:line 127 [mono-rt] at Microsoft.Maui.Handlers.LayoutHandler.SetVirtualView(IView view) in C:\dotnet-maui-work\src\Core\src\Handlers\Layout\LayoutHandler.Android.cs:line 43 [mono-rt] at Microsoft.Maui.Handlers.ViewHandler
2[[Microsoft.Maui.ILayout, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Platform.LayoutViewGroup, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].SetVirtualView(IElement view) in C:\dotnet-maui-work\src\Core\src\Handlers\View\ViewHandlerOfT.cs:line 56
[mono-rt] at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler newHandler) in C:\dotnet-maui-work\src\Controls\src\Core\Element\Element.cs:line 831
[mono-rt] at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value) in C:\dotnet-maui-work\src\Controls\src\Core\Element\Element.cs:line 781
[mono-rt] at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.set_Handler(IElementHandler value) in C:\dotnet-maui-work\src\Controls\src\Core\VisualElement\VisualElement.cs:line 1660
[mono-rt] at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement view, IMauiContext context) in C:\dotnet-maui-work\src\Core\src\Platform\ElementExtensions.cs:line 96
[mono-rt] at Microsoft.Maui.Platform.ElementExtensions.ToPlatform(IElement view, IMauiContext context) in C:\dotnet-maui-work\src\Core\src\Platform\ElementExtensions.cs:line 127
[mono-rt] at Microsoft.Maui.Handlers.LayoutHandler.SetVirtualView(IView view) in C:\dotnet-maui-work\src\Core\src\Handlers\Layout\LayoutHandler.Android.cs:line 43
[mono-rt] at Microsoft.Maui.Handlers.ViewHandler2[[Microsoft.Maui.ILayout, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Platform.LayoutViewGroup, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].SetVirtualView(IElement view) in C:\dotnet-maui-work\src\Core\src\Handlers\View\ViewHandlerOfT.cs:line 56 [mono-rt] at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler newHandler) in C:\dotnet-maui-work\src\Controls\src\Core\Element\Element.cs:line 831 [mono-rt] at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value) in C:\dotnet-maui-work\src\Controls\src\Core\Element\Element.cs:line 781 [mono-rt] at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.set_Handler(IElementHandler value) in C:\dotnet-maui-work\src\Controls\src\Core\VisualElement\VisualElement.cs:line 1660 [mono-rt] at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement view, IMauiContext context) in C:\dotnet-maui-work\src\Core\src\Platform\ElementExtensions.cs:line 96 [mono-rt] at Microsoft.Maui.Platform.ElementExtensions.ToPlatform(IElement view, IMauiContext context) in C:\dotnet-maui-work\src\Core\src\Platform\ElementExtensions.cs:line 127 [mono-rt] at Microsoft.Maui.Handlers.LayoutHandler.SetVirtualView(IView view) in C:\dotnet-maui-work\src\Core\src\Handlers\Layout\LayoutHandler.Android.cs:line 43 [mono-rt] at Microsoft.Maui.Handlers.ViewHandler
2[[Microsoft.Maui.ILayout, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Platform.LayoutViewGroup, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].SetVirtualView(IElement view) in C:\dotnet-maui-work\src\Core\src\Handlers\View\ViewHandlerOfT.cs:line 56
[mono-rt] at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler newHandler) in C:\dotnet-maui-work\src\Controls\src\Core\Element\Element.cs:line 831
[mono-rt] at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value) in C:\dotnet-maui-work\src\Controls\src\Core\Element\Element.cs:line 781
[mono-rt] at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.set_Handler(IElementHandler value) in C:\dotnet-maui-work\src\Controls\src\Core\VisualElement\VisualElement.cs:line 1660
[mono-rt] at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement view, IMauiContext context) in C:\dotnet-maui-work\src\Core\src\Platform\ElementExtensions.cs:line 96
[mono-rt] at Microsoft.Maui.Platform.ElementExtensions.ToPlatform(IElement view, IMauiContext context) in C:\dotnet-maui-work\src\Core\src\Platform\ElementExtensions.cs:line 127
[mono-rt] at Microsoft.Maui.Handlers.ScrollViewHandler.UpdateInsetView(IScrollView scrollView, IScrollViewHandler handler) in C:\dotnet-maui-work\src\Core\src\Handlers\ScrollView\ScrollViewHandler.Android.cs:line 175
[mono-rt] at Microsoft.Maui.Handlers.ScrollViewHandler.MapContent(IScrollViewHandler handler, IScrollView scrollView) in C:\dotnet-maui-work\src\Core\src\Handlers\ScrollView\ScrollViewHandler.Android.cs:line 109
[mono-rt] at Microsoft.Maui.PropertyMapper2.<>c__DisplayClass5_0[[Microsoft.Maui.IScrollView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Handlers.IScrollViewHandler, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].<Add>b__0(IElementHandler h, IElement v) in C:\dotnet-maui-work\src\Core\src\PropertyMapper.cs:line 172 [mono-rt] at Microsoft.Maui.PropertyMapper.UpdatePropertyCore(String key, IElementHandler viewHandler, IElement virtualView) in C:\dotnet-maui-work\src\Core\src\PropertyMapper.cs:line 47 [mono-rt] at Microsoft.Maui.PropertyMapper.UpdateProperties(IElementHandler viewHandler, IElement virtualView) in C:\dotnet-maui-work\src\Core\src\PropertyMapper.cs:line 82 [mono-rt] at Microsoft.Maui.Handlers.ElementHandler.SetVirtualView(IElement view) in C:\dotnet-maui-work\src\Core\src\Handlers\Element\ElementHandler.cs:line 79 [mono-rt] at Microsoft.Maui.Handlers.ViewHandler
2[[Microsoft.Maui.IScrollView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Platform.MauiScrollView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].SetVirtualView(IView view) in C:\dotnet-maui-work\src\Core\src\Handlers\View\ViewHandlerOfT.cs:line 53
[mono-rt] at Microsoft.Maui.Handlers.ViewHandler2[[Microsoft.Maui.IScrollView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Platform.MauiScrollView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].SetVirtualView(IElement view) in C:\dotnet-maui-work\src\Core\src\Handlers\View\ViewHandlerOfT.cs:line 56 [mono-rt] at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler newHandler) in C:\dotnet-maui-work\src\Controls\src\Core\Element\Element.cs:line 831 [mono-rt] at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value) in C:\dotnet-maui-work\src\Controls\src\Core\Element\Element.cs:line 781 [mono-rt] at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.set_Handler(IElementHandler value) in C:\dotnet-maui-work\src\Controls\src\Core\VisualElement\VisualElement.cs:line 1660 [mono-rt] at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement view, IMauiContext context) in C:\dotnet-maui-work\src\Core\src\Platform\ElementExtensions.cs:line 96 [mono-rt] at Microsoft.Maui.Platform.ElementExtensions.ToPlatform(IElement view, IMauiContext context) in C:\dotnet-maui-work\src\Core\src\Platform\ElementExtensions.cs:line 127 [mono-rt] at Microsoft.Maui.Handlers.ContentViewHandler.UpdateContent(IContentViewHandler handler) in C:\dotnet-maui-work\src\Core\src\Handlers\ContentView\ContentViewHandler.Android.cs:line 44 [mono-rt] at Microsoft.Maui.Handlers.ContentViewHandler.MapContent(IContentViewHandler handler, IContentView page) in C:\dotnet-maui-work\src\Core\src\Handlers\ContentView\ContentViewHandler.Android.cs:line 49 [mono-rt] at Microsoft.Maui.PropertyMapper
2.<>cDisplayClass5_0[[Microsoft.Maui.IContentView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Handlers.IContentViewHandler, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].2[[Microsoft.Maui.IContentView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Platform.ContentViewGroup, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].SetVirtualView(IView view) in C:\dotnet-maui-work\src\Core\src\Handlers\View\ViewHandlerOfT.cs:line 53 [mono-rt] at Microsoft.Maui.Handlers.ContentViewHandler.SetVirtualView(IView view) in C:\dotnet-maui-work\src\Core\src\Handlers\ContentView\ContentViewHandler.Android.cs:line 27 [mono-rt] at Microsoft.Maui.Handlers.ViewHandler
2[[Microsoft.Maui.IContentView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Platform.ContentViewGroup, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].SetVirtualView(IElement view) in C:\dotnet-maui-work\src\Core\src\Handlers\View\ViewHandlerOfT.cs:line 56
[mono-rt] at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler newHandler) in C:\dotnet-maui-work\src\Controls\src\Core\Element\Element.cs:line 831
[mono-rt] at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value) in C:\dotnet-maui-work\src\Controls\src\Core\Element\Element.cs:line 781
[mono-rt] at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.set_Handler(IElementHandler value) in C:\dotnet-maui-work\src\Controls\src\Core\VisualElement\VisualElement.cs:line 1660
[mono-rt] at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement view, IMauiContext context) in C:\dotnet-maui-work\src\Core\src\Platform\ElementExtensions.cs:line 96
[mono-rt] at Microsoft.Maui.Platform.ElementExtensions.ToPlatform(IElement view, IMauiContext context) in C:\dotnet-maui-work\src\Core\src\Platform\ElementExtensions.cs:line 127
[mono-rt] at Microsoft.Maui.Platform.MauiContextExtensions.ToPlatform(IView view, IMauiContext fragmentMauiContext, Context context, LayoutInflater layoutInflater, FragmentManager childFragmentManager) in C:\dotnet-maui-work\src\Core\src\Platform\Android\MauiContextExtensions.cs:line 96
[mono-rt] at Microsoft.Maui.Controls.Platform.Compatibility.ShellFragmentContainer.OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) in C:\dotnet-maui-work\src\Controls\src\Core\Compatibility\Handlers\Shell\Android\ShellFragmentContainer.cs:line 26
[mono-rt] at AndroidX.Fragment.App.Fragment.n_OnCreateView_Landroid_view_LayoutInflater_Landroid_view_ViewGroup_Landroid_osBundle(IntPtr jnienv, IntPtr native
On the Android side, an IndexOutOfBoundsException occurred when calling the SetSelection method in the UpdateText method of the EditTextExtensions class.
[src\Controls\src\Core\Platform\Android\Extensions\EditTextExtensions.cs]
public static void UpdateText(this EditText editText, InputView inputView)
{
(var oldText, var newText) = GetTexts(editText, inputView);
if (oldText != newText)
{
editText.Text = newText;
// When updating from xplat->plat, we set the selection (cursor) to the end of the text
editText.SetSelection(newText.Length);
}
}
If you consider the MaxLength property of Entry, Editor,
[src\Controls\src\Core\Platform\Android\Extensions\EditTextExtensions.cs]
public static void UpdateText(this EditText editText, InputView inputView)
{
(var oldText, var newText) = GetTexts(editText, inputView);
if (oldText != newText)
{
editText.Text = newText;
// When updating from xplat->plat, we set the selection (cursor) to the end of the text
int selectionLength = newText.Length;
if (inputView.MaxLength >= 0)
{
selectionLength = Math.Min(selectionLength, inputView.MaxLength);
}
editText.SetSelection(selectionLength);
}
}
Exceptions don't occur on Windows, but if you want to do the same, it would look like this:
[src\Controls\src\Core\Platform\Windows\Extensions\TextBoxExtensions.cs]
public static void UpdateText(this TextBox platformControl, InputView inputView)
{
var hasFocus = platformControl.FocusState != UI.Xaml.FocusState.Unfocused;
var passwordBox = platformControl as MauiPasswordTextBox;
var isPassword = passwordBox?.IsPassword ?? false;
var textTransform = inputView?.TextTransform ?? TextTransform.None;
// Setting the text causes the cursor to be reset to position zero.
// So, let's retain the current cursor position and calculate a new cursor
// position if the text was modified by a Converter.
var oldText = platformControl.Text ?? string.Empty;
var newText = TextTransformUtilites.GetTransformedText(
inputView?.Text,
isPassword ? TextTransform.None : textTransform
);
// Re-calculate the cursor offset position if the text was modified by a Converter.
// but if the text is being set by code, let's just move the cursor to the end.
var cursorOffset = newText.Length - oldText.Length;
int cursorPosition = hasFocus ? platformControl.GetCursorPosition(cursorOffset) : newText.Length;
if (inputView?.MaxLength >= 0)
{
cursorPosition = Math.Min(cursorPosition, inputView.MaxLength);
}
if (oldText != newText && passwordBox is not null)
passwordBox.Password = newText;
else if (oldText != newText)
platformControl.Text = newText;
platformControl.Select(cursorPosition, 0);
}
However, there is no problem with the Windows TextBox Select method even if you specify an Index that exceeds MaxLength, so there is no problem even if you do not include a guard condition.
In addition to the solution in PR #7371, the MaxLength property should be considered.
Hi, @cat0363, thanks for your responses. One question, I really need the MaxLength working on both platforms because I'll get the value from API and now it's breaking my app. When could these changes be merged? Or can I include this functionality using Handlers?
Hi, @juniorsaraviao The easiest workaround is to ensure that the string you bind does not exceed MaxLength until the problem is fixed. For example, cut the part exceeding MaxLength at the stage of setting the value in ViewModel. When re-implementing with Handler, it is recommended to check the source code below as it is the first caller that triggers this issue.
[src\Controls\src\Core\Editor\Editor.Android.cs] [src\Controls\src\Core\Entry\Entry.Android.cs] [src\Controls\src\Core\Editor\Editor.iOS.cs] [src\Controls\src\Core\Entry\Entry.iOS.cs] [src\Controls\src\Core\Editor\Editor.Windows.cs]
In that case, you'll probably have to reimplement the MapText part yourself, but you'll have to port everything you need. I can create a PR, but I don't know when it will be reviewed and merged.
We've just run into this error in our app on iOS devices only but I think its slightly different cause as we aren't exceeding MaxLength
We've added a custom handler to sanitize the text as we want the result to be visible to the user if there was a change however when setting the sanitized text back to the entry or editor we get this crash (The handler gets called a few times before the crash and successfully completes)
Ive added some code to check for exceeding the maxlength and we are nowhere near exceeding it, my current test is around 1600 characters in a 5000 character limit
This is the handler we've added after adding the maxlength check in as well
Microsoft.Maui.Handlers.EditorHandler.Mapper.AppendToMapping("FixEditorFocus", (handler, view) =>
{
#if WINDOWS
handler.PlatformView.TextChanged += (object sender, Microsoft.UI.Xaml.Controls.TextChangedEventArgs e) =>
#endif
#if ANDROID
handler.PlatformView.TextChanged += (object sender, Android.Text.TextChangedEventArgs e) =>
#endif
#if IOS
handler.PlatformView.TextSetOrChanged += (object sender, EventArgs e) =>
#endif
{
var clean = Regex.Replace(handler.PlatformView.Text, @"[^\u0009\u000A\u000D\u0020-\u007E]", string.Empty);
var maxLength = -1;
if (handler.VirtualView is Editor editor && editor.MaxLength > 0)
{
maxLength = editor.MaxLength;
}
if (handler.VirtualView is Entry entry && entry.MaxLength > 0)
{
maxLength = entry.MaxLength;
}
if (maxLength > 0 && clean.Length > maxLength)
{
clean = clean.Substring(0, maxLength);
Logger.WriteDebug($"Reduced cleaned string to {maxLength} to prevent crash after sanitized string");
}
if (clean != handler.PlatformView.Text)
{
handler.PlatformView.Text = clean;
}
};
});
The maxlength substring is not executing in the above as we aren't exceeding the maxlength
This is the error we are getting
Logger.WriteException: Value cannot be null. (Parameter 'fromPosition'), someuserid: id, error message: at ObjCRuntime.ThrowHelper.ThrowArgumentNullException(String argumentName)
at ObjCRuntime.NativeObjectExtensions.GetNonNullHandle(INativeObject self, String argumentName)
at UIKit.UITextView.GetTextRange(UITextPosition fromPosition, UITextPosition toPosition)
at Microsoft.Maui.Platform.TextInputExtensions.SetTextRange(IUITextInput platformView, Int32 start, Int32 selectedTextLength)
at Microsoft.Maui.Controls.Platform.TextExtensions.UpdateText(UITextView textView, InputView inputView)
at Microsoft.Maui.Controls.Editor.MapText(IEditorHandler handler, Editor editor)
at Microsoft.Maui.Controls.Editor.MapText(EditorHandler handler, Editor editor)
at Microsoft.Maui.PropertyMapper`2.<>c__DisplayClass5_0[[Microsoft.Maui.Controls.Editor, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Handlers.EditorHandler, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].<Add>b__0(IElementHandler h, IElement v)
at Microsoft.Maui.PropertyMapper.UpdatePropertyCore(String key, IElementHandler viewHandler, IElement virtualView)
at Microsoft.Maui.PropertyMapper.UpdateProperty(IElementHandler viewHandler, IElement virtualView, String property)
at Microsoft.Maui.Handlers.ElementHandler.UpdateValue(String property)
at Microsoft.Maui.Controls.Element.OnPropertyChanged(String propertyName)
at Microsoft.Maui.Controls.BindableObject.SetValueActual(BindableProperty property, BindablePropertyContext context, Object value, Boolean currentlyApplying, SetValueFlags attributes, Boolean silent)
at Microsoft.Maui.Controls.BindableObject.SetValueCore(BindableProperty property, Object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes)
at Microsoft.Maui.Controls.BindingExpression.ApplyCore(Object sourceObject, BindableObject target, BindableProperty property, Boolean fromTarget)
at Microsoft.Maui.Controls.BindingExpression.Apply(Boolean fromTarget)
at Microsoft.Maui.Controls.BindingExpression.BindingExpressionPart.<PropertyChanged>b__49_0()
at Microsoft.Maui.Controls.DispatcherExtensions.DispatchIfRequired(IDispatcher dispatcher, Action action)
at Microsoft.Maui.Controls.BindingExpression.BindingExpressionPart.PropertyChanged(Object sender, PropertyChangedEventArgs args)
at Microsoft.Maui.Controls.BindingExpression.WeakPropertyChangedProxy.OnPropertyChanged(Object sender, PropertyChangedEventArgs e)
at Namespace.anamespace.OurApp.Common.BindableBase.OnPropertyChanged(String propertyName) in /Users/me/Source/anamespace.OurApp/OurApp/OurApp.Common/Common/BindableBase.cs:line 81
at Namespace.anamespace.OurApp.Common.BindableBase.SetProperty[String](String& storage, String value, String propertyName) in /Users/me/Source/anamespace.OurApp/OurApp/OurApp.Common/Common/BindableBase.cs:line 61
at Namespace.anamespace.OurApp.Common.ViewModels.APage.AVM.set_SummaryText(String value) in /Users/me/Source/anamespace.OurApp/OurApp/OurApp.Common/ViewModels/APage/AVM.cs:line 41
at Namespace.anamespace.OurApp.Common.ViewModels.APage.AVM.ReloadAssessmentSummary() in /Users/me/Source/anamespace.OurApp/OurApp/OurApp.Common/ViewModels/APage/AVM.cs:line 177
at Namespace.anamespace.OurApp.Common.ViewModels.APage.AVM.OnAppearing() in /Users/me/Source/anamespace.OurApp/OurApp/OurApp.Common/ViewModels/APage/AVM.cs:line 160
at Namespace.anamespace.OurApp.XForms.Pages.APage.CreateControlAsync(CancellationToken cancellationToken) in /Users/me/Source/anamespace.OurApp/OurApp/OurApp/Pages/Main/APagePage.xaml.cs:line 159
at Namespace.anamespace.OurApp.XForms.Pages.APage.<OnBindingContextChanged>b__11_1(Object sender, PropertyChangedEventArgs e) in /Users/me/Source/anamespace.OurApp/OurApp/OurApp/Pages/Main/APagePage.xaml.cs:line 72
at ObjCRuntime.Runtime.ThrowException(IntPtr gchandle)
at UIKit.UIApplication.UIApplicationMain(Int32 argc, String[] argv, IntPtr principalClassName, IntPtr delegateClassName)
at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass)
at OurApp.Program.Main(String[] args) in /Users/me/Source/anamespace.OurApp/OurApp/OurApp/Platforms/iOS/Program.cs:line 15, unique instance id: URYBLZGE [line 243]
I ran into the same error, but the root cause was not the string length, but there were line breaks within the strings that was assigned.
I now added logic that removes \r and \n from the string and the app does not crash anymore.
The author of this PR to fix this has confirmed that this is no longer an issue. Please test with .NET 8 from next week and let us know if this has not been resolved for you. Thanks!
@jfversluis I am just now encountering this issue and I am using net8.0. I can't for the life of me figure out what's causing it, it's with a new page that I have added to my app. It has some entries, editors as well as other controls. Navigating to the page causes no issue, neither does navigating back and returning to a new instance of the page. However, if I edit the value of one Entry for example, then navigate back and to a new instance this exception is thrown. The stack trace is here:
System.ArgumentNullException: Value cannot be null. (Parameter 'fromPosition')
at ObjCRuntime.ThrowHelper.ThrowArgumentNullException(String argumentName) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/ObjCRuntime/ThrowHelper.cs:line 28
at ObjCRuntime.NativeObjectExtensions.GetNonNullHandle(INativeObject self, String argumentName) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/ObjCRuntime/INativeObject.cs:line 42
at UIKit.UITextField.GetTextRange(UITextPosition fromPosition, UITextPosition toPosition) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/build/dotnet/ios/generated-sources/UIKit/UITextField.g.cs:line 648
at Microsoft.Maui.Platform.TextInputExtensions.SetTextRange(IUITextInput platformView, Int32 start, Int32 selectedTextLength)
at Microsoft.Maui.Controls.Platform.TextExtensions.UpdateText(IUITextInput textInput, InputView inputView, Boolean isEditing)
at Microsoft.Maui.Controls.Platform.TextExtensions.UpdateText(UITextField textField, InputView inputView)
at Microsoft.Maui.Controls.Entry.MapText(IEntryHandler handler, Entry entry)
at Microsoft.Maui.PropertyMapperExtensions.<>c__DisplayClass2_0`2[[Microsoft.Maui.Controls.Entry, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Handlers.IEntryHandler, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].<ReplaceMapping>b__0(IEntryHandler h, Entry v, Action`2 p)
at Microsoft.Maui.PropertyMapperExtensions.<>c__DisplayClass1_0`2[[Microsoft.Maui.Controls.Entry, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Handlers.IEntryHandler, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].<ModifyMapping>g__newMethod|0(IElementHandler handler, IElement view)
at Microsoft.Maui.PropertyMapper`2.<>c__DisplayClass5_0[[Microsoft.Maui.IEntry, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Handlers.IEntryHandler, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].<Add>b__0(IElementHandler h, IElement v)
at Microsoft.Maui.PropertyMapper.UpdatePropertyCore(String key, IElementHandler viewHandler, IElement virtualView)
at Microsoft.Maui.PropertyMapper.UpdateProperties(IElementHandler viewHandler, IElement virtualView)
at Microsoft.Maui.Handlers.ElementHandler.SetVirtualView(IElement view)
at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.IEntry, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Platform.MauiTextField, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].SetVirtualView(IView view)
at Microsoft.Maui.Handlers.EntryHandler.SetVirtualView(IView view)
at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.IEntry, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Platform.MauiTextField, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].SetVirtualView(IElement view)
at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler newHandler)
at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value)
at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.set_Handler(IElementHandler value)
at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement view, IMauiContext context)
at Microsoft.Maui.Platform.ElementExtensions.ToPlatform(IElement view, IMauiContext context)
at Microsoft.Maui.Handlers.LayoutHandler.SetVirtualView(IView view)
at Syncfusion.Maui.Core.SfViewHandler.SetVirtualView(IView view)
at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.ILayout, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Platform.LayoutView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].SetVirtualView(IElement view)
at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler newHandler)
at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value)
at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.set_Handler(IElementHandler value)
at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement view, IMauiContext context)
at Microsoft.Maui.Platform.ElementExtensions.ToPlatform(IElement view, IMauiContext context)
at Microsoft.Maui.Handlers.LayoutHandler.SetVirtualView(IView view)
at Syncfusion.Maui.Core.SfViewHandler.SetVirtualView(IView view)
at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.ILayout, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Platform.LayoutView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].SetVirtualView(IElement view)
at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler newHandler)
at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value)
at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.set_Handler(IElementHandler value)
at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement view, IMauiContext context)
at Microsoft.Maui.Platform.ElementExtensions.ToPlatform(IElement view, IMauiContext context)
at Microsoft.Maui.Handlers.LayoutHandler.SetVirtualView(IView view)
at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.ILayout, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Platform.LayoutView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].SetVirtualView(IElement view)
at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler newHandler)
at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value)
at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.set_Handler(IElementHandler value)
at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement view, IMauiContext context)
at Microsoft.Maui.Platform.ElementExtensions.ToPlatform(IElement view, IMauiContext context)
at Microsoft.Maui.Handlers.LayoutHandler.SetVirtualView(IView view)
at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.ILayout, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Platform.LayoutView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].SetVirtualView(IElement view)
at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler newHandler)
at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value)
at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.set_Handler(IElementHandler value)
at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement view, IMauiContext context)
at Microsoft.Maui.Platform.ElementExtensions.ToPlatform(IElement view, IMauiContext context)
at Microsoft.Maui.Handlers.BorderHandler.UpdateContent(IBorderHandler handler)
at Microsoft.Maui.Handlers.BorderHandler.MapContent(IBorderHandler handler, IBorderView border)
at Microsoft.Maui.PropertyMapper`2.<>c__DisplayClass5_0[[Microsoft.Maui.IBorderView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Handlers.IBorderHandler, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].<Add>b__0(IElementHandler h, IElement v)
at Microsoft.Maui.PropertyMapper.UpdatePropertyCore(String key, IElementHandler viewHandler, IElement virtualView)
at Microsoft.Maui.PropertyMapper.UpdateProperties(IElementHandler viewHandler, IElement virtualView)
at Microsoft.Maui.Handlers.ElementHandler.SetVirtualView(IElement view)
at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.IBorderView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Platform.ContentView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].SetVirtualView(IView view)
at Microsoft.Maui.Handlers.BorderHandler.SetVirtualView(IView view)
at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.IBorderView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Platform.ContentView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].SetVirtualView(IElement view)
at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler newHandler)
at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value)
at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.set_Handler(IElementHandler value)
at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement view, IMauiContext context)
at Microsoft.Maui.Platform.ElementExtensions.ToPlatform(IElement view, IMauiContext context)
at Microsoft.Maui.Handlers.LayoutHandler.SetVirtualView(IView view)
at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.ILayout, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Platform.LayoutView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].SetVirtualView(IElement view)
at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler newHandler)
at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value)
at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.set_Handler(IElementHandler value)
at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement view, IMauiContext context)
at Microsoft.Maui.Platform.ElementExtensions.ToPlatform(IElement view, IMauiContext context)
at Microsoft.Maui.Handlers.ScrollViewHandler.UpdateContentView(IScrollView scrollView, IScrollViewHandler handler, ICrossPlatformLayout crossPlatformLayout)
at Microsoft.Maui.Handlers.ScrollViewHandler.MapContent(IScrollViewHandler handler, IScrollView scrollView)
at Microsoft.Maui.PropertyMapper`2.<>c__DisplayClass5_0[[Microsoft.Maui.IScrollView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Handlers.IScrollViewHandler, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].<Add>b__0(IElementHandler h, IElement v)
at Microsoft.Maui.PropertyMapper.UpdatePropertyCore(String key, IElementHandler viewHandler, IElement virtualView)
at Microsoft.Maui.PropertyMapper.UpdateProperties(IElementHandler viewHandler, IElement virtualView)
at Microsoft.Maui.Handlers.ElementHandler.SetVirtualView(IElement view)
at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.IScrollView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[UIKit.UIScrollView, Microsoft.iOS, Version=17.0.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065]].SetVirtualView(IView view)
at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.IScrollView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[UIKit.UIScrollView, Microsoft.iOS, Version=17.0.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065]].SetVirtualView(IElement view)
at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler newHandler)
at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value)
at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.set_Handler(IElementHandler value)
at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement view, IMauiContext context)
at Microsoft.Maui.Platform.ElementExtensions.ToPlatform(IElement view, IMauiContext context)
at Microsoft.Maui.Handlers.ContentViewHandler.UpdateContent(IContentViewHandler handler)
at Microsoft.Maui.Handlers.ContentViewHandler.MapContent(IContentViewHandler handler, IContentView page)
at Microsoft.Maui.PropertyMapper`2.<>c__DisplayClass5_0[[Microsoft.Maui.IContentView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Handlers.IContentViewHandler, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].<Add>b__0(IElementHandler h, IElement v)
at Microsoft.Maui.PropertyMapper.UpdatePropertyCore(String key, IElementHandler viewHandler, IElement virtualView)
at Microsoft.Maui.PropertyMapper.UpdateProperties(IElementHandler viewHandler, IElement virtualView)
at Microsoft.Maui.Handlers.ElementHandler.SetVirtualView(IElement view)
at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.IContentView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Platform.ContentView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].SetVirtualView(IView view)
at Microsoft.Maui.Handlers.ContentViewHandler.SetVirtualView(IView view)
at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.IContentView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Platform.ContentView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].SetVirtualView(IElement view)
at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler newHandler)
at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value)
at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.set_Handler(IElementHandler value)
at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement view, IMauiContext context)
at Microsoft.Maui.Platform.ViewExtensions.ToHandler(IView view, IMauiContext context)
at Microsoft.Maui.Controls.Platform.Compatibility.ShellSectionRenderer.PushPage(Page page, Boolean animated, TaskCompletionSource`1 completionSource)
at Microsoft.Maui.Controls.Platform.Compatibility.ShellSectionRenderer.OnPushRequested(NavigationRequestedEventArgs e)
at Microsoft.Maui.Controls.Platform.Compatibility.ShellSectionRenderer.OnNavigationRequested(Object sender, NavigationRequestedEventArgs e)
at Microsoft.Maui.Controls.ShellSection.InvokeNavigationRequest(NavigationRequestedEventArgs args)
at Microsoft.Maui.Controls.ShellSection.OnPushAsync(Page page, Boolean animated)
at Microsoft.Maui.Controls.ShellSection.PushStackOfPages(List`1 pages, Nullable`1 animate)
at Microsoft.Maui.Controls.ShellSection.GoToAsync(ShellNavigationRequest request, ShellRouteParameters queryData, IServiceProvider services, Nullable`1 animate, Boolean isRelativePopping)
at Microsoft.Maui.Controls.ShellNavigationManager.GoToAsync(ShellNavigationParameters shellNavigationParameters, ShellNavigationRequest navigationRequest)
at Logbook.Maui.ViewModels.FlightsPageViewModel.AddNewFlight() in /Users/wbeal/Code/Logbook/Logbook.Maui/ViewModels/FlightsPageViewModel.cs:line 85
I need to do some more investigating but for now I am stumped.
@varyamereon if that's the case please try and reproduce in a new .NET MAUI project built against .NET 8, add that project to a new issue and link to this one so we can track it properly. Please tag me and I will have a look, thanks!
Absolutely will do when I get a chance, still trying to find out the root cause.
@jfversluis ... I am also running into this issue.
Using net8.0-ios... This is currently halting my port to MAUI from going to production. It is very weird as the only way I can reliably reproduce it is this:
high level app UI>>>: List page with drill down to detail page.
---updated this to simplify after what I found: To replicate follow this: 1) start at list and drill down to a detail page 2) click on a field to bring up the Keyboard/Picker/any iOS native UI 3) navigate back to list page... and then go into detail page again.
Looking at the call stack below... it seems to occur on a UITextView... drilling in... YOU ARE NOT GOING TO BELIEVE THIS... my field is populated from a DB and it has a double dash -- in it...
SOO... after all this... it seems it is definitely a result of having some special characters in a field... in combination with having previously brought up a keyboard/Picker/ any type of iOS native dialog P.S.> I tested Char(10) and Char(13) and no issues with them... I don't know how many other types of special character combinations exist... regardless... this needs to be fixed.
Value cannot be null. (Parameter 'fromPosition')
----------
at ObjCRuntime.ThrowHelper.ThrowArgumentNullException(String argumentName) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/ObjCRuntime/ThrowHelper.cs:line 28
at ObjCRuntime.NativeObjectExtensions.GetNonNullHandle(INativeObject self, String argumentName) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/ObjCRuntime/INativeObject.cs:line 42
at UIKit.UITextView.GetTextRange(UITextPosition fromPosition, UITextPosition toPosition) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/build/dotnet/ios/generated-sources/UIKit/UITextView.g.cs:line 565
at Microsoft.Maui.Platform.TextInputExtensions.SetTextRange(IUITextInput platformView, Int32 start, Int32 selectedTextLength)
at Microsoft.Maui.Controls.Platform.TextExtensions.UpdateText(IUITextInput textInput, InputView inputView, Boolean isEditing)
at Microsoft.Maui.Controls.Platform.TextExtensions.UpdateText(UITextView textView, InputView inputView)
at Microsoft.Maui.Controls.Editor.MapText(IEditorHandler handler, Editor editor)
at Microsoft.Maui.PropertyMapperExtensions.<>c__DisplayClass2_0`2[[Microsoft.Maui.Controls.Editor, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Handlers.IEditorHandler, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].<ReplaceMapping>b__0(IEditorHandler h, Editor v, Action`2 p)
at Microsoft.Maui.PropertyMapperExtensions.<>c__DisplayClass1_0`2[[Microsoft.Maui.Controls.Editor, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Handlers.IEditorHandler, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].<ModifyMapping>g__newMethod|0(IElementHandler handler, IElement view)
at Microsoft.Maui.PropertyMapper`2.<>c__DisplayClass5_0[[Microsoft.Maui.IEditor, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Handlers.IEditorHandler, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].<Add>b__0(IElementHandler h, IElement v)
at Microsoft.Maui.PropertyMapper.UpdatePropertyCore(String key, IElementHandler viewHandler, IElement virtualView)
at Microsoft.Maui.PropertyMapper.UpdateProperty(IElementHandler viewHandler, IElement virtualView, String property)
at Microsoft.Maui.Handlers.ElementHandler.UpdateValue(String property)
at Microsoft.Maui.Controls.Element.OnPropertyChanged(String propertyName)
at Microsoft.Maui.Controls.BindableObject.SetValueActual(BindableProperty property, BindablePropertyContext context, Object value, Boolean currentlyApplying, SetValueFlags attributes, SetterSpecificity specificity, Boolean silent)
at Microsoft.Maui.Controls.BindableObject.SetValueCore(BindableProperty property, Object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes, SetterSpecificity specificity)
at Microsoft.Maui.Controls.BindingExpression.ApplyCore(Object sourceObject, BindableObject target, BindableProperty property, Boolean fromTarget, SetterSpecificity specificity)
at Microsoft.Maui.Controls.BindingExpression.Apply(Boolean fromTarget)
at Microsoft.Maui.Controls.BindingExpression.BindingExpressionPart.<PropertyChanged>b__50_0()
at Microsoft.Maui.Controls.DispatcherExtensions.DispatchIfRequired(IDispatcher dispatcher, Action action)
at Microsoft.Maui.Controls.BindingExpression.BindingExpressionPart.PropertyChanged(Object sender, PropertyChangedEventArgs args)
at Microsoft.Maui.Controls.BindingExpression.WeakPropertyChangedProxy.OnPropertyChanged(Object sender, PropertyChangedEventArgs e)
at CommunityToolkit.Mvvm.ComponentModel.ObservableObject.OnPropertyChanged(PropertyChangedEventArgs e)
at CommunityToolkit.Mvvm.ComponentModel.ObservableObject.OnPropertyChanged(String propertyName)
at CommunityToolkit.Mvvm.ComponentModel.ObservableObject.SetProperty[WODetails](WODetails& field, WODetails newValue, String propertyName)
at SOSMobile.WorkOrders.WorkOrderDetailViewModel.set_WOInfo(WODetails value) in C:\Source\net8\SOSMobile\Features\WorkOrders\WorkOrderDetailViewModel.cs:line 40
at SOSMobile.WorkOrders.WorkOrderDetailViewModel.RefreshData() in C:\Source\net8\SOSMobile\Features\WorkOrders\WorkOrderDetailViewModel.cs:line 138
I have the same issue in .NET 8 only in iOS, when setting the text on an 'Editor' via a view model binding property. I do have some '\n' and '\r' values in the text for formatting. This is a showstopper for moving to Maui.
Value cannot be null. (Parameter 'fromPosition')
" at ObjCRuntime.ThrowHelper.ThrowArgumentNullException(String argumentName) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/ObjCRuntime/ThrowHelper.cs:line 28
at ObjCRuntime.NativeObjectExtensions.GetNonNullHandle(INativeObject self, String argumentName) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/ObjCRuntime/INativeObject.cs:line 42
at UIKit.UITextView.GetTextRange(UITextPosition fromPosition, UITextPosition toPosition) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/build/dotnet/ios/generated-sources/UIKit/UITextView.g.cs:line 565
at Microsoft.Maui.Platform.TextInputExtensions.SetTextRange(IUITextInput platformView, Int32 start, Int32 selectedTextLength)
at Microsoft.Maui.Controls.Platform.TextExtensions.UpdateText(IUITextInput textInput, InputView inputView, Boolean isEditing)
at Microsoft.Maui.Controls.Platform.TextExtensions.UpdateText(UITextView textView, InputView inputView)
at Microsoft.Maui.Controls.Editor.MapText(IEditorHandler handler, Editor editor)
at Microsoft.Maui.PropertyMapperExtensions.<>c__DisplayClass2_0`2[[Microsoft.Maui.Controls.Editor, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Handlers.IEditorHandler, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].<ReplaceMapping>b__0(IEditorHandler h, Editor v, Action`2 p)
at Microsoft.Maui.PropertyMapperExtensions.<>c__DisplayClass1_0`2[[Microsoft.Maui.Controls.Editor, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Handlers.IEditorHandler, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].<ModifyMapping>g__newMethod|0(IElementHandler handler, IElement view)
at Microsoft.Maui.PropertyMapper`2.<>c__DisplayClass5_0[[Microsoft.Maui.IEditor, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Handlers.IEditorHandler, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].<Add>b__0(IElementHandler h, IElement v)
at Microsoft.Maui.PropertyMapper.UpdatePropertyCore(String key, IElementHandler viewHandler, IElement virtualView)
at Microsoft.Maui.PropertyMapper.UpdateProperty(IElementHandler viewHandler, IElement virtualView, String property)
at Microsoft.Maui.Handlers.ElementHandler.UpdateValue(String property)
at Microsoft.Maui.Controls.Element.OnPropertyChanged(String propertyName)
at Microsoft.Maui.Controls.BindableObject.SetValueActual(BindableProperty property, BindablePropertyContext context, Object value, Boolean currentlyApplying, SetValueFlags attributes, SetterSpecificity specificity, Boolean silent)
at Microsoft.Maui.Controls.BindableObject.SetValueCore(BindableProperty property, Object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes, SetterSpecificity specificity)
at Microsoft.Maui.Controls.BindingExpression.ApplyCore(Object sourceObject, BindableObject target, BindableProperty property, Boolean fromTarget, SetterSpecificity specificity)
at Microsoft.Maui.Controls.BindingExpression.Apply(Boolean fromTarget)
at Microsoft.Maui.Controls.BindingExpression.BindingExpressionPart.<PropertyChanged>b__50_0()
at Microsoft.Maui.Controls.DispatcherExtensions.DispatchIfRequired(IDispatcher dispatcher, Action action)
at Microsoft.Maui.Controls.BindingExpression.BindingExpressionPart.PropertyChanged(Object sender, PropertyChangedEventArgs args)
at Microsoft.Maui.Controls.BindingExpression.WeakPropertyChangedProxy.OnPropertyChanged(Object sender, PropertyChangedEventArgs e)"
@jyotsnar as this issue is actually closed, please see new issue I created and linked
@jyotsnar as this issue is actually closed, please see new issue I created and linked
Oh okay, thank you very much
@jfversluis I could still replicate this issue, but I think the fix is here :) https://github.com/dotnet/maui/pull/20584
Description
I'm using a StackLayout with
BindableLayout.ItemsSource
to show Entry or Editor. However, the app throws an exception:System.ArgumentNullException: Value cannot be null. (Parameter 'fromPosition')
Steps to Reproduce
Link to public reproduction project repository
https://github.com/juniorsaraviao/MauiSample
Version with bug
7.0.86
Last version that worked well
Unknown/Other
Affected platforms
iOS
Affected platform versions
iOS 15.2 and up
Did you find any workaround?
No
Relevant log output