0

In my code I need to save the data to my database whenever an Entry looses focus, unless it was triggered by the push of a Button. I notice that in Xamarin, when an Entry is focused and a button is clicked, an Unfocus event is first fired for the Entry and then the Button Focus event and then the Button Clicked event.

I need some way to determine if the Unfocus event for an Entry was fired due to the click of a Button. Is there some way to do this?

Kikanye
  • 1,198
  • 1
  • 14
  • 33
  • Seems there was no clean way to do this, but I got the desired result using the ides in my other question : https://stackoverflow.com/questions/70705268/i-would-like-to-run-an-async-method-synchronously-when-the-async-function-causes/70713892#70713892 – Kikanye Jan 15 '22 at 04:10

2 Answers2

0

"I need some way to determine if the Unfocus event for an Entry was fired due to the click of a Button. Is there some way to do this?"

Not directly. Here is a way to infer it.

Set a flag in the Unfocus event, and start a one-time timer. You’ll have to experiment to decide what time interval works well. My guess is somewhere in range 200 ms to 500 ms.

In Button focus event, clear that flag and/or stop the timer.

If the timer Elapsed event fires, without the button Focus event stopping it (or clearing the flag), then you’ve found an Unfocus not due to Button.

At that time, take the desired action.

ToolmakerSteve
  • 18,547
  • 14
  • 94
  • 196
0

You can know if the button is clicked by checking the "ispressed"

Here is my sample code you can refer to:

xmal:

 <ContentPage.Content>
         <StackLayout HorizontalOptions="Center" VerticalOptions="Center">
            <Entry x:Name="myentry" Unfocused="E_UFEvent"/>
            <Button x:Name="mybutton" Clicked="Onbutton_Clicked" Text="Click me"/>

        </StackLayout>
    </ContentPage.Content>

codebehind:

  void E_UFEvent(Object sender, EventArgs e)
        {if (mybutton.IsPressed)
            {
                DisplayAlert("Notice", "Fired after button clicked","ok");
            }

Update: You can set a flag, excute the method when it's true, and delay the unfoucused event to check if the button is cliked(set flag to false when button cliked), and change flag back when then etry is focused agagin.code as following:

xmal:

<ContentPage.Content>
         <StackLayout HorizontalOptions="Center" VerticalOptions="Center">
            <Entry x:Name="myentry" Unfocused="E_UFEvent" Focused="E_FEvent"/>
            <Button x:Name="mybutton" Pressed="OnButton_Pressed" Text="Click me"/>

        </StackLayout>
    </ContentPage.Content>

codebehind:

private Boolean IsExcute;
           async  void E_UFEvent(Object sender, EventArgs e)
                {await Task.Delay(2000);
    
                    if (IsExcute)
                    {
                       //do something
                    }
        void E_FEvent(Object sender, EventArgs e)
                {IsExcute=true;}
    
       void On_ButtonPressed(Object sender, EventArgs e)
                {IsExcute=false;}
Adrain
  • 1,946
  • 1
  • 3
  • 7