Xamarin Forms-绑定Listview进行延迟加载 [英] Xamarin Forms - Binding Listview for lazy loading

查看:509
本文介绍了Xamarin Forms-绑定Listview进行延迟加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

开始涉猎Xamarin形式. 我无法弄清的两件事:

Started to dabble in Xamarin Forms. Two things I cant figure out:

我的Listview的绑定:

Binding of my Listview:

我有一堂课:

    public class Mainlist
    {
        public string Title
        {
            get;
            set;
        }
        public string Value
        {
            get;
            set;
        }

    }

我的XAML如下:

   <ListView x:Name="mainlist">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Orientation="Horizontal">
                            <StackLayout Orientation="Vertical">
                                <Label Text="{Binding Title}" Font="18"></Label>
                                <Label Text="{Binding Value}" TextColor="Gray"></Label>
                            </StackLayout>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

现在发生的是,我有一个URL列表.我从每个URL中使用HTMLAgilityPack foreach循环抓取某些信息,效果很好.

What happens now is that i have a list of URLS. From every URL I am scraping certain info with HTMLAgilityPack foreach loop, which is working fine.

我想在每次循环运行后将抓取的数据添加到列表视图中,并显示它.类似于延迟加载".

I would like to add the scraped data after each run of the loop to the listview and have it display. Something like "lazy loading".

到目前为止,我只能弄清楚在刮掉所有Urls并一次显示以下内容后如何设置itemsource:

Up to now i could only figure out how to set the itemsource after all Urls are scraped and have it display at once with something like this:

 //set itemsource to URL collection
            mainlist.ItemsSource = new List<Mainlist>() {
                new Mainlist()
            {

              //scraped info from each URL
                    Title = title.ToString().Trim(),
                    Value = value.ToString().Trim(),

                },
            };

推荐答案

首先,创建一个名为 MyViewModel.cs 的视图模型类:

First, create a view model class, called MyViewModel.cs:

public class MyViewModel : INotifyPropertyChanged
{
    // property changed event handler
    public event PropertyChangedEventHandler PropertyChanged;

    private ObservableCollection<Mainlist> _list;

    public ObservableCollection<Mainlist> List
    {
        get { return _list; }
        set
        {
            _list = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(List)));
        }
    }

    public MyViewModel()
    {
        _list = new ObservableCollection<Mainlist>();
    }

    public async void StartScraping()
    {
        // assuming you are 'awaiting' the results of your scraping method...
        foreach (...)
        {
            await ... scrape a web page ...

            var newItem = new Mainlist()
            {
                Title = title.ToString().Trim(),
                Value = value.ToString().Trim()
            };

            // if you instead have multiple items to add at this point,
            // then just create a new List<Mainlist>, add your items to it,
            // then add that list to the ObservableCollection List.

            Device.BeginInvokeOnMainThread(() => 
            {
                List.Add(newItem);
            });
        }

    }
}

现在,在页面的xaml.cs代码后面的文件中,将视图模型设置为您的BindingContext:

Now in your page's xaml.cs code behind file, set the view model as your BindingContext:

public class MyPage : ContentPage // (assuming the page is called "MyPage" and is of type ContentPage)
{
    MyViewModel _viewModel;

    public MyPage()
    {
        InitializeComponent();
        _viewModel = new MyViewModel();
        BindingContext = _viewModel;
        // bind the view model's List property to the list view's ItemsSource:
        mainList.setBinding(ListView.ItemsSourceProperty, "List");
    }
}

请注意,在视图模型中,您需要使用ObservableCollection<T>而不是List<T>,因为ObservableCollection<T>允许ListView每次添加或删除项目时自动更新.

And note that in your view model, you'll need to use an ObservableCollection<T> instead of a List<T>, as ObservableCollection<T> will allow the ListView to be updated automatically whenever you add or remove items from it.

此外,为减轻混乱,我建议将类名从Mainlist更改为MainListItem.

Also, to ease a bit of confusion, I'd recommend changing the class name from Mainlist to MainListItem.

这篇关于Xamarin Forms-绑定Listview进行延迟加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆