This is driving me crazy. I have been looking for the source of the issue for hours, but i am starting to suspect this is not an issue in my logic... Maybe i am wrong.
I have a simple Entry. Its Text property is bound to a property with type double in a ViewModel. At the same time i subscribe to the Unfocused
Event whose EventHandler simply sets the entry.Text
property to "1.0"
(actually i can reproduce the issue for x.y0, that is any decimal whose final digit is 0). If now i write in the Entry anything (except "1" or "1." or "1.0"!!!) and leave the Entry (by tapping outside or taping on Done) so that Unfocused
is fired, the App becomes unresponsive.
Note: I know that it sounds a bit strange to set entry.Text = 1.0
in the event handler. The truth is that i came across this issue by trying to format the entry.Text
value as follows.
if (double.TryParse(entry.Text, out double result))
{
entry.Text = String.Format("{0:F2}", result);
}
There String.Format
tries to round the decimal to two decimals. If i give 6.999
the expected value should be 7.00
, but the App becomes unresponsive instead.
<StackLayout>
<Entry Text="{Binding Weight}"
Unfocused="entry_Unfocused"/>
</StackLayout>
BindingContext
property of the page as follows:public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
protected override void OnAppearing()
{
base.OnAppearing();
BindingContext = new viewmodel();
}
private void entry_Unfocused(object sender, FocusEventArgs e)
{
((Entry)sender).Text = "1.0";
}
}
public class viewmodel : INotifyPropertyChanged
{
public viewmodel()
{
}
private double _Weight;
public double Weight
{
get => _Weight;
set
{
if (_Weight != value)
{
_Weight = value;
OnPropertyChanged();
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] String propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Unfocused
is fired.Could anyone explain what is going on here, and anyway to work around this issue?
I found the source of the Issue!
Thanks to @LeoZhu-MSFT for his insight!
The thing is that when entry.Text = "3.00"
is set, this value is apparently parsed to double and then send to my property setter. In fact:
double _Weight = double.Parse("3.00"); // _Weight ends up having value 3!!!
So entry.Text
and _Weight
have always different values!!! and the binding tries to set indefinitely _Weight
to 3.00
but all _Weight
gets in the setter is 3
...
To solve that, instead of changing my property _Weight
to String as suggested by @LeoZhu-MSFT, which is sub-optimal (i still want to perform mathematical operations with my Weight
property!), i can change it to decimal!!!
In fact:
decimal _Weight = decimal.Parse("3.00"); // _Weight has now value 3.00. As wanted!!!
So after i change _Weight
and Weight
type to decimal in my View Model, the infinite-loop-behavior disappeared :D