Infinite loop in SaveCommentOnBlur when Unfocused event is triggered in MAUI

Omkar Pawar 100 Reputation points
2025-01-12T18:39:33.88+00:00

In my custom MAUI entry control, the SaveCommentOnBlur method gets stuck in an infinite loop when the EntryField_Unfocused event is triggered. This causes the keyboard to hang, and either the keyboard or the app becomes unresponsive.

Steps to Reproduce:

  1. Click on the entry field and try to add or remove text.
  2. Observe that the focus event is triggered multiple times, causing an infinite loop.

Expected Behavior: The SaveCommentOnBlur method should be triggered only once, and the loss of focus should not cause any unintended loops or freezes.

Actual Behavior: The SaveCommentOnBlur method is triggered multiple times, causing an infinite loop and freezing the keyboard.

Sample Code: GitHub Repository - UnfocusedIssue

I need help resolving this.

Record_2025-01-12-23-31-00-ezgif.com-video-to-gif-converter

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,864 questions
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 78,916 Reputation points Microsoft Vendor
    2025-01-13T06:32:14.71+00:00

    Hello,

    Infinite loop in SaveCommentOnBlur when Unfocused event is triggered in MAUI

    This issue is related to the Listview's item reuse mechanism. SaveCommentOnBlur will be executed repeatedly, if you have over one items in the Listview.

    If you want to SaveCommentOnBlur called once when click the entry, you can use BindableLayout.ItemsSource in <Stacklayout>, it do not have reuse mechanism, it will create MaterialEntry for every item. If you have many items. you want to scroll, you can add <ScrollView> like following code.

    <ContentPage.Content>
           
                <ScrollView Padding="10">
                    <StackLayout BindableLayout.ItemsSource="{Binding DocumentsList}">
                        <BindableLayout.ItemTemplate>
                            <DataTemplate>
                                <StackLayout Orientation="Vertical">
                                    <Grid HorizontalOptions="FillAndExpand">
    
                                        <StackLayout Orientation="Horizontal" Grid.Column="1">
                                            <control:MaterialEntry Text="{Binding Comments}" 
                                    Placeholder="Comment" 
                                    Unfocused="SaveCommentOnBlur" 
                                    HorizontalOptions="FillAndExpand" 
                                    VerticalOptions="Center"/>
                                        </StackLayout>
    
                                        <Label Text="{Binding Comments}" IsVisible="True"/>
    
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="110"/>
                                            <ColumnDefinition Width="*"/>
                                        </Grid.ColumnDefinitions>
                                    </Grid>
                                    <BoxView Margin="0,2,0,2" BackgroundColor="Black" HeightRequest=".5"/>
                                </StackLayout>
                            </DataTemplate>
    
                        </BindableLayout.ItemTemplate>
                    </StackLayout>
                </ScrollView>
    
        </ContentPage.Content>
    </ContentPage>
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.