How can I cancel or delete the change of a value of an entry?

- 3,425
- 30
- 38
- 48

- 63
- 2
- 11
-
1Please explain or illustrate the problem you are trying to solve. Currently it is unclear what you are asking. – EvZ Apr 02 '18 at 15:15
-
I have a form that contains a set of entries and a button. the problem of the entry that you can change its value the value remains existing even if you navigate to another page and you come back the typed value remains existing, what I wanted to do is if the user changes the value of an entry and navigates to another page without clicking the button then returns to the page, the page removes the value typed by the user and loads the old value and if the user clicks the button after typing a new value for the entry the page saves the value typed by the user in the database. – Aicha Maghrebi Apr 02 '18 at 15:35
-
@AichaMaghrebi could you update the original post by showing the code you have right now? – pinedax Apr 02 '18 at 17:19
3 Answers
You can bind your Entry Text and make that property in your view model, after that:
- assign default value of that entry as a database stored value
- On save button click update database stored value with the current value of entry(That's the binded Text)
So whenever you will navigate again to that page it will display entry value as a last stored value in database, make sure to use INotifyPropertychanged to update value.

- 1,247
- 9
- 14
There are many possibilities
You can use TextChanged
event to rollback value (or empty entry or something else) if an unexpected character is entered
Completed
when user press ENTER (or ok or return on mobile) then you can do everything with the entry
Also Unfocused
can help in the same way.
Depending on how you want to interact with the user, you can implement one of these three ways.
Here is an example which use TextChanged
, I want user to write max 20 characters, when characters are over 20 I rollback to the preview value.
bool IsEditing = false;
private void OnEntryTextChanged(object sender, TextChangedEventArgs e)
{
var entry = (Entry)sender;
if (IsEditing || string.IsNullOrEmpty(entry.Text))
return;
IsEditing = true;
if (entry.Text.Length > 20)
{
entry.Text = e.OldTextValue;
}
IsEditing = false;
}

- 648
- 9
- 23
There is not enough details in your question to give you an exact answer. But this is how you could deal with it.
If you are doing the logic from the code behind, its pretty simple. The content page has a disappearing event. You can override this method, and set the Model to null
protected override void OnDisappearing () { base.OnDisappearing (); mobileUser = null }
- If you are using MVVM pattern and you do the logic from ViewModel or else where, things can be a bit more complicated. Thats where MVVM frameworks come in handy, most Frameworks have exposed those events, and u can call them from ViewModels, or you could do it yourself by adding Behaviours to the ContentPage, and adding a command

- 7,070
- 5
- 29
- 52