【问题标题】:Xamarin Forms - Binding Listview for lazy loadingXamarin Forms - 用于延迟加载的绑定 Listview
【发布时间】:2018-08-31 09:06:53
【问题描述】:

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

我的 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 循环抓取某些信息,这工作正常。

我想在每次循环运行后将抓取的数据添加到列表视图并显示。诸如“延迟加载”之类的东西。

到目前为止,我只能弄清楚如何在所有 Url 都被抓取后设置 itemsource 并让它立即显示,如下所示:

 //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(),

                },
            };

【问题讨论】:

  • 研究使用 MVVM 模式,它会让解决这个问题变得相当简单
  • @sme 如果你能帮助我,那就太好了。我一直在尝试加载。
  • 我现在在下面发布了答案

标签: c# listview data-binding xamarin.forms listviewitem


【解决方案1】:

首先,创建一个视图模型类,名为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

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&lt;T&gt; 而不是List&lt;T&gt;,因为ObservableCollection&lt;T&gt; 将允许在您向其中添加或删除项目时自动更新ListView .

另外,为了避免混淆,我建议将类名从 Mainlist 更改为 MainListItem

【讨论】:

  • 完美!非常感谢!
  • 我更新了方法,我只需要假设你将如何抓取网页。现在可能会更清楚一点
【解决方案2】:

我认为你可以这样做:

mainlist.ItemsSource = new ObservableCollection<Mainlist>();

foreach (var item in yourDataFromHtmlAgilityPackScraping) {
    mainlist.ItemsSource.Add(new Mainlist()
            {
                //scraped info from each URL
                Title = item.title.ToString().Trim(),
                Value = item.value.ToString().Trim(),
            });
}

这里的重要部分是 ObservableCollection。这允许在添加新元素时更新 Listview。

【讨论】:

  • 谢谢,是的,我正在寻找类似的东西。像这样,它在“添加”上给了我一个错误,尽管说:没有给出与所需的形式参数相对应的参数。看起来这里有一些语法问题。
猜你喜欢
  • 1970-01-01
  • 2018-01-03
  • 1970-01-01
  • 2018-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多