Entry Completed trigger Listview Refresh - MVVM

ZedexZedex USUniversity ✭✭

Hi,

I have a Xam Forms App containing an Entry and a Listview.

I've tried to implement the MVVM design but somethings still puzzle me.

From what I understand, I will do the binding in the View(XAML) for the Listview datasource and data template.
However, I need the search terms to come from the Entry on Complete event, which will trigger a weather service to retrieve an ObservableCollection to be updated into the listview binding.

Currently, I'm doing it like this in the XAML Code Behind:

    ObservableCollection<WeatherResultModel> resultList; 
private async void Entry_Completed(object sender, EventArgs e)
    {
        string input = ((Entry)sender).Text;
        if (input != null)
        {
            WeatherService weatherService = new WeatherService();
            resultList = await weatherService.GetForecastByCityOrCountry(input);

            SearchResultListView.ItemsSource = resultList;
        }
    }

How can I do this in a way that fits the MVVM paradigm?

Thanks!

Best Answer

Answers

  • ZedexZedex USUniversity ✭✭

    Hi @Mabrouk,

    Thanks for the suggestion with the TextChanged event.

    I've thought of using SearchBar and binding the SearchCommand to trigger the Command in the ViewModel as well.

    I'm still new to this so I was wondering with an Entry view, whether there was a way I could shift the code from CodeBehind into my ViewModel as compared to what I am currently doing now, and also in the use case that I do not want to use the Searchbar as you have mentioned.

    Thanks again!

  • JulienRosenJulienRosen CAMember ✭✭✭✭
    edited August 2017

    You should use the SearchBar

    Bind the SearchText to a property in your VM, and the SearchCommand to a method in your VM. no code behind required.

Sign In or Register to comment.