ListView's refresh is crashed when DataTemplate is a custom ContentView

eugene1700eugene1700 RUMember
edited December 2017 in Xamarin.Forms

Hi, help me, please.

I'm using the Prism. MetersListPage has a SearchBar and ListView. ListView DataTemplate is a BasicMeterInfoView. When I rewrite FilterMeters, application is crached with "The application is in break mode". Can you tell me how can I get a stacktrace and how can I binding context to custom DataTemplate?

MetersListPageView:

<StackLayout>
    <SearchBar Placeholder="Search" TextChanged="SearchBar_OnTextChanged"/>
    <StackLayout>
        <ListView x:Name="listMeters" ItemsSource="{Binding FilterMeters}" HasUnevenRows="True">
            <ListView.ItemTemplate>
                            <DataTemplate>
                    <ViewCell>
                        <views:BasicMeterInfoView BindingContext="{Binding .}"/>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
</StackLayout>
    private MetersListPageViewModel Model => ((MetersListPageViewModel)BindingContext);

    private void SearchBar_OnTextChanged( object sender, TextChangedEventArgs e )
    {
            listMeters.BeginRefresh();
            Model.MeterFilterCommand.Execute(e.NewTextValue);
            listMeters.EndRefresh();
    }

MetersListPageViewModel :

public class MetersListPageViewModel : ServerConnectionAwareBaseViewModel
{
    private ObservableCollection< BasicMeterInfoViewModel > _lineItems, _filterItems;

    public MetersListPageViewModel( ) : base( navigationService, dataProvider, eventAggregator )
    {
        Title = MainResourceFile.MetersListPageTitle;
        MeterFilterCommand = new DelegateCommand< string >( MeterFilterExecute );
    }

    public DelegateCommand<string> MeterFilterCommand { get; }

    private void MeterFilterExecute( string filterText )
    {
       if (!string.IsNullOrWhiteSpace( filterText ))
            FilterMeters = new ObservableCollection< BasicMeterInfoViewModel >(_lineItems.Where(_ => _.Name.Contains(filterText)));
    }

    public ObservableCollection< BasicMeterInfoViewModel > FilterMeters
    {
        get => _filterItems;
        set => SetProperty( ref _filterItems, value );
    }

    public ObservableCollection< BasicMeterInfoViewModel > Meters
    {
        get => _lineItems;
        set => SetProperty( ref _lineItems, value );
    }

public override async void OnNavigatedTo( NavigationParameters parameters )
    {
        base.OnNavigatedTo( parameters );
        await PopulateMeterList();
    }

    private async Task PopulateMeterList()
    {
        if( _lineItems == null )
        {
            var items =
                await
                    GetServerDataGuarded(
                        () => DataProvider.LoadMeterInfos( new MetersRequest( _clock.Now.Month, _clock.Now.Year ) ) );
            Meters =
                new ObservableCollection< BasicMeterInfoViewModel >(
                    items.Select( _ =>
                    {
                        var res = _mapper.Map< BasicMeterInfoViewModel >( _ );
                        res.Command = new DelegateCommand( () => { ItemSelected( res ); } );
                        return res;
                    } ) );
            FilterMeters = Meters;
        }
    }

}
Sign In or Register to comment.