MVVMFresh SelectedItem is not Working

Hello.

Im testing the MVVMFresh for a project, with the following code:

<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:ffimageloading="clr-namespace:FFImageLoading.Forms;assembly=FFImageLoading.Forms" x:Class="Test.Pages.WeeklyEventsPage"> <StackLayout> <ListView ItemsSource="{Binding WEEKLY_EVENTS}" SelectedItem="{Binding SelectedEvents}" HasUnevenRows="True" SeparatorVisibility="None"> <ListView.ItemTemplate> <DataTemplate> <ViewCell> <ffimageloading:CachedImage HeightRequest="180" Aspect="AspectFill" Source="{Binding Media_Path}" ></ffimageloading:CachedImage> </ViewCell> </DataTemplate> </ListView.ItemTemplate> </ListView> </StackLayout> </ContentPage>

`
using FreshMvvm;
using PropertyChanged;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace Test.PageModels
{
[ImplementPropertyChanged]
public class WeeklyEventsPageModel : FreshBasePageModel
{
public ObservableCollection WEEKLY_EVENTS { get; set; }

    public async override void Init(object initData)
    {
        var eventsList = await DATABASE.GetInstance().GetItemsAsync<Event>();
        WEEKLY_EVENTS = new ObservableCollection<Event>(eventsList);
    }

    public Command<Event> SelectedEvents
    {
        get
        {
            return new Command<Event>(async (selected_Event) => {
                await CoreMethods.DisplayAlert("System", selected_Event.Name, "OK");
            });
        }
    }
}

}
`

When I runned this, the listView works perfect, but I pick one and nothing happen.

That I missing something @MichaelRidland ??

Posts

  • ClintStLaurentClintStLaurent ✭✭✭✭✭ USUniversity ✭✭✭✭✭

    I don't see where you are binding the command to anything. Maybe I just missed it. Or maybe the parser here mangled your XAML.

    Here's an article on how to format your code so the parser doesn't eat it.

  • FabianGomez.1091FabianGomez.1091 USMember

    UP

  • ClintStLaurentClintStLaurent ✭✭✭✭✭ USUniversity ✭✭✭✭✭

    @FabianGomez.1091 said:
    UP

    If you're too busy to type out a message letting those trying to help you if their advice was useful or not, maybe you should wait until you have time before posting.

    UP

    doesn't tell anyone anything.

    but I pick one and nothing happen.

    As I said in my earlier reply, it doesn't look like you're binding your command anywhere in your XAML. So why would you expect tapping an item to do something?

    Maybe when you get time to type more than 2 character you can explain what it is you are expecting to happen. "Doesn't do anything" doesn't tell us what you think should happen.

  • FabianGomez.1091FabianGomez.1091 USMember

    Sorry for that @ClintStLaurent .

    Im expecting to fire the Event "SelectedEvents" , to show this : .DisplayAlert("System", selected_Event.Name, "OK");

    Im new working this new framework MVVMFresh, its said that is "Convention over Configuration", I understood that the MVVM Fresh would bind the XAML with ViewModel throughthe NameConvention. Thats right?

    Thanks for replying.

  • Amar_BaitAmar_Bait ✭✭✭✭✭ DZMember ✭✭✭✭✭
    edited April 2017

    ListView's SelectedItem needs to be bound to a Event property, not a Command.

    So your code should look like this:

    public Event SelectedEvents { get; set; } // Better call this property SelectedEvent since there will be only 1 selected item in the ListView
    
    // Since you're using Fody.PropertyChanged, this will be called automatically when SelectedEvents changes
    private async void SelectedEventsChanged()
    {
        if (SelectedEvents != null)
        {
             await CoreMethods.DisplayAlert("System", SelectedEvents.Name, "OK");
             // Do whatever you want with SelectedEvents here...
        }
    
        SelectedEvents = null; // if you want to clear selection from the ListView
    }
    

    Also your variable naming is awful, WEEKLY_EVENTS should be WeeklyEvents, check this article for a better understanding of C# naming conventions https://msdn.microsoft.com/en-us/library/ms229043(v=vs.110).aspx

  • FabianGomez.1091FabianGomez.1091 USMember

    Thanks @nadjib , you are tottally right. Now Im calling the Command throught the Property.

    Event _selectedEvent;
            public Event SelectedEvent {
                get
                {
                    return _selectedEvent;
                }
                set
                {
                    _selectedEvent = value;
                    if (value != null)
                        EventSelected.Execute(value);
                }
            } 
    
             public Command<Event> EventSelected
            {
                 get
                 {
                     return new Command<Event>(async (selected_Event) => {
                         await DisplayAlert("SISTEMA", selected_Event.Name, "OK");
    
                     });
                 }
             }
    

    Thanks guys.

Sign In or Register to comment.