的Windows Phone 8一长串选择 - 滚动到底后的数据异步加载 [英] Windows Phone 8 Long List Selector - scroll to bottom after data loaded async

查看:94
本文介绍了的Windows Phone 8一长串选择 - 滚动到底后的数据异步加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建类似于股票消息的应用程序。但我很新的WP8发展,也为C#和.NET

I'm creating an app similar to the stock Messaging. But i'm very new to wp8 development and also to c# and .net

我使用的是一长串选择器显示的消息。消息被加载页面的NavigatedTo事件。该处理程序是异步,因为它是从一个web服务加载数据,当有0存储在本地数据库,然后将其保存他们在本地数据库中。

I'm using the Long List Selector to display messages. Messages are loaded on NavigatedTo event of the page. the handler is async as it is loading the data from a webservice, when there are 0 stored in local db, then it saves them in local database.

我想加载数据后滚动到最后一条消息。

I would like to scroll to the last message after the data is loaded.

OnNavigated页面

the page OnNavigated to

protected override async void OnNavigatedTo(NavigationEventArgs e)
{
    string contactId = "";
    if (NavigationContext.QueryString.TryGetValue("contactId", out contactId))
    {
        await App.MessagesViewModel.LoadData();

        DataContext = App.MessagesViewModel;

        //scroll to last message, but it's apparently to soon
        var lastMessage = App.MessagesViewModel.Messages.LastOrDefault();
        llsMessages.ScrollTo(lastMessage);
    }
}

但这抛出异常System.ArgumentException:提供的项目不集合中存在。所以我想名单仍未改变。

but this throws an exception System.ArgumentException: The provided item doesn't exist in the collection. So i figured the list hasn't yet changed.

所以我尝试LongListSelector不同的事件,这将表明,它已经从视图模型添加的数据。过了一会experimetnation后,我想出了这个

So i tried different events of LongListSelector that would indicate that it has already added the data from the view model. After a while of experimetnation i came up with this

private void llsMessages_SizeChanged(object sender, SizeChangedEventArgs e)
{
    var lastMessage = App.MessagesViewModel.Messages.LastOrDefault();
    if (lastMessage != null)
    {
        llsMessages.ScrollTo(lastMessage);
    }
}

但这仅当消息从数据库中加载。当从web服务加载的最后一条消息为null。

but this works only when the messages are loaded from the database. When loading from webservice the last message is null.

所以负载我在顶部的第一条消息后,然后我导航离开该页面,然后再回来,列表滚动至底部。我想消除这一点,但我不知道怎么样。

So after load i'm on the first message at top, then i navigate away from the page, then come back, the list scrolls to bottom. i would like to eliminate this, but i have no idea how.

有什么办法如何做到这一点?

is there any way how to accomplish this?

推荐答案

也许这将工作:

Maybe this will work:

private async Task DoAndScroll()
{
   await App.MessagesViewModel.LoadData();
   var lastMessage = App.MessagesViewModel.Messages.LastOrDefault();
   llsMessages.ScrollTo(lastMessage);
}

protected override void OnNavigatedTo(NavigationEventArgs e)
{
   string contactId = "";
   if (NavigationContext.QueryString.TryGetValue("contactId", out contactId))
   {
      DataContext = App.MessagesViewModel;  
      DoAndScroll();     
   }
}

这篇关于的Windows Phone 8一长串选择 - 滚动到底后的数据异步加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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