Forum Cross Platform with Xamarin

Clear an Entry form

Hi,
I have a StackLayout with "Entry" in the layout and I have an Clear button at the bottom. I want to be able to clear out the Values that the users keyed in if they press the "Clear" Button. I'm not sure how to do this.
I'm new to Xamarin
Thank you,

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    The value in the Entry should be binded to a property on your ViewModel.
    So set that property to string.empty

    Ideally, the button should raise a command, such as ClearCommand then the handler for that command is in your ViewModel and resets the property.

  • ScoobyMugScoobyMug USMember ✭✭

    @ClintStLaurent
    Thank you,
    Do you have an example?

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    An example of... ? Properties... MVVM... Commands....

  • ScoobyMugScoobyMug USMember ✭✭

    @ClintStLaurent
    Hi,
    I've added a Handle for the Clear button and then set the field to empty. When I debug I can see it hits the line to Empty the text but nothing happens on the screen.

        void Handle_Clear_Clicked(object sender, System.EventArgs e)
        {
            Field1.Text = string.Empty;
        }
    
  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Stop.

    When you said you were new to Xamarin I didn't assume you were new to development in general - or at least to how things are done in a modern MVVM pattern.

    You shouldn't be doing things like it was Windows Forms circa 1998.
    I really did mean Command not button click handler.
    I really did mean change the property in your ViewModel - not this ancient changing of the UI element directly.
    Nobody works like this these days, and WPF and Xamarin weren't really meant to handle this old, old way of doing things.

    I urge you to work through some lessons on MVVM and modern practices.

  • ScoobyMugScoobyMug USMember ✭✭

    @ClintStLaurent
    Thanks you , I will do some reviewing.

  • GaryParkinGaryParkin USMember ✭✭
    edited April 2019

    @ClintStLaurent, would you please be so kind as to point me to an example of MVVM thet is NOT using Xamarin Forms (or MVVM Light/3rd Party)? Sure XF does this, but I can't find a way to databind to my model using native Xamarin with C#.
    I too have to do it the old way by assigning the model and it's painful. Reminds me of my younger years.

    @ScoobyMug, did you find a databinding solution?

  • JohnHardmanJohnHardman GBUniversity admin

    @GaryParkin

    You have hooked onto a thread in the cross-platform forum, but do not want to use Xamarin.Forms .

    Are you building for Xamarin.Android or Xamarin.iOS ? You haven't said.

    For Xamarin.Android, see https://www.codeproject.com/articles/1070662/data-binding-in-xamarin-android
    I'm pretty sure a quick Google will find something similar for Xamarin.iOS

  • GaryParkinGaryParkin USMember ✭✭

    @JohnHardman Oh crap, I'm so sorry.
    I misread the top of the forum. We're using native Xamarin on Android 8.1 with no addins at all. :(
    Google shows nothing for data binding because native Xamarin doesn't allow for it.
    So sorry for the confusion and thanks for the reply.

  • JohnHardmanJohnHardman GBUniversity admin

    @GaryParkin said:
    Google shows nothing for data binding because native Xamarin doesn't allow for it.

    For Xamarin.Android, see https://www.codeproject.com/articles/1070662/data-binding-in-xamarin-android

  • FersqFersq Member ✭✭

    Try the following it is easier.
    NoCamioneta_Picker = Convert.ToString(-1);

    private string _NoCamioneta_Picker;
    public string NoCamioneta_Picker
    {
    get => _NoCamioneta_Picker;
    set { SetProperty(ref _NoCamioneta_Picker, value); }
    }

Sign In or Register to comment.