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

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

问题描述

开始涉足 Xamarin Forms.有两件事我想不通:

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

绑定我的列表视图:

我有一堂课:

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

    }

我的 XAML 看起来像:

My XAML looks like:

   <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".

到目前为止,我只能弄清楚如何在抓取所有 Url 后设置 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 而不是 List,因为 ObservableCollection 将允许 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天全站免登陆