Struggling to unsubscribe correctly from events fired for the same entry control. I have seen a similar answer from @JohnHardmanJohnHardman here: https://forums.xamarin.com/discussion/139977/how-to-programmatically-insert-some-text-in-entry-in-any-cursor-position however my requirements are slightly different - I have to use an entry's Focused/Completed for different things, and it seems after tapping these entries (roughly 3-4 times) the events are no longer working correctly and get confused (fire multiple times or not all) It's probably a confusing between the entry getting unfocused on hitting enter. I'm therefore assuming it's a memory leak because the events are not unsubscribed from correctly. Unfortunately, because of the dynamic nature of the entries, I can't use xaml either.
Code sample below:
Entry currentTextEntry = new Entry { BackgroundColor = Color.Transparent, HeightRequest = 16 };
currentTextEntry.Focused += TextInput_Focused;
currentTextEntry.Completed += onTappedEnterForEntry;
private void TextInput_Focused(object sender, FocusEventArgs e)
{
Entry textInput = (Entry)e.VisualElement;
try
{
FontSize.Text = $"{textInput.FontSize}";
SelectFontSize.Value = textInput.FontSize;
var italic = italicSwitch.IsToggled;
var bold = boldSwitch.IsToggled;
var color = textColorButton.BackgroundColor;
textInput.TextColor = color;
//other logic
} catch (Exception ex)
{
Debug.WriteLine(ex);
}
textInput.Focused -= TextInput_Focused;
}
void onTappedEnterForEntry(object sender, EventArgs e)
{
SaveTextNotationToDb();
var textInput = sender as Entry;
if (textInput != null) textInput.Completed -= onTappedOutsideEntry;
}