title: Windows Phone 8 Long List Selector - scroll to bottom after data loaded async
body: <p>I'm creating an app similar to the stock Messaging. But i'm very new to wp8 development and also to c# and .net</p>
  
  <p>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.</p>
  
  <p>I would like to scroll to the last message after the data is loaded.</p>
  
  <p>the page OnNavigated to</p>
  
  <pre><code>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);
      }
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>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</p>
  
  <pre><code>private void llsMessages_SizeChanged(object sender, SizeChangedEventArgs e)
  {
      var lastMessage = App.MessagesViewModel.Messages.LastOrDefault();
      if (lastMessage != null)
      {
          llsMessages.ScrollTo(lastMessage);
      }
  }
  </code></pre>
  
  <p>but this works only when the messages are loaded from the database. When loading from webservice the last message is null.</p>
  
  <p>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.</p>
  
  <p>is there any way how to accomplish this?</p>