在WP 8.1 MVVM应用程序中实现ISupportIncrementalLoading

时间:2015-02-13 10:22:43

标签: c# listview mvvm windows-phone-8.1 infinite-scroll

我在WP 8.1应用程序中有一个ListView,我使用来自web api的数据填充。 我的问题是我希望列表具有"无限滚动",在WP 8中我会使用LongListSelector,但由于WP 8.1中不可用,我必须使用ListView然后使用ISupportIncrementalLoading问题是我真的无法弄明白。

我的ListView看起来像这样。

<ListView ItemsSource="{Binding LatestVideos}" ItemTemplate="{StaticResource LatestVideoDataTemplate}">
            <Interactivity:Interaction.Behaviors>
                <core:EventTriggerBehavior EventName="SelectionChanged">
                    <core:NavigateToPageAction TargetPage="WindowsWorldStar.SelectedPage" />
                    <core:InvokeCommandAction Command="{Binding SendVideoMessageCommand}"
                                              CommandParameter="{Binding SelectedItem, ElementName=ListView}" />
                </core:EventTriggerBehavior>
            </Interactivity:Interaction.Behaviors>
</ListView>

我的ViewModel看起来像这样:

public class MainViewModel : INotifyPropertyChanged
{
    private List<Video> _latestVideo;

    public List<Video> LatestVideos
    {
        get { return _latestVideo; }
        set
        {
            _latestVideo = value;
            OnPropertyChanged();
        }
    }

    public ICommand RefreshCommand { get; set; }

    public ICommand SendVideoMessageCommand { get; set; }

    public MainViewModel()
    {
        RefreshCommand = new RelayCommand(async () => await DownloadDataAsync());

        SendVideoMessageCommand = new RelayCommand<Video>(SendVideoMessage);

        DownloadDataAsync();
    }

    private void SendVideoMessage(Video video)
    {

        Messenger.Default.Send(video);
    }


    private async Task DownloadDataAsync()
    {
        var dataServices = new DataServices();

        LatestVideos = await dataServices.GetVideoAsync("http://mywebservice.com");
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }

}

像这样的GetVideoAsync:

public async Task<List<Video>> GetVideoAsync(string url)
    {
        var httpClient = new HttpClient();

        var jsonResponse = await httpClient.GetStringAsync(url);

        MyClass myClass = JsonConvert.DeserializeObject<MyClass>(jsonResponse);

        var latestPosts = myClass.response.videos.video;
        return latestPosts;
    }

非常感谢任何帮助。 : - )

0 个答案:

没有答案