1

I've seen a lot of questions here related to the OnNavigatedTo method, but none of them seem to answer the basic question of, "At what point should I load data?" The documentation on MSDN doesn't explicitly answer this question, as far as I can tell.

If I need to load a list of data from the local database, which method is the most appropriate to use? Should I use the OnNavigatedTo method, or the Loaded event?

What I've been using so far is this pattern, which seems to work well:

protected override void OnNavigatedTo(NavigationEventArgs e) {
   base.OnNavigatedTo(e);

   if (NavigationMode.New == e.NavigationMode) {
      var data = LoadData();
      this.DataContext = data;
   }
}

What this means is that for a new instance of a page, load the data synchronously. This also means that the page will not be rendered until the data has finished loading and the profiler complains that I'm using too much UI thread time.

An alternate approach is this pattern:

protected override async void OnNavigatedTo(NavigationEventArgs e) {
   base.OnNavigatedTo(e);

   if (NavigationMode.New == e.NavigationMode) {
      var data = await LoadData();
      this.DataContext = data;
   }
}

But with this pattern, it seems to me that navigation, and therefore page rendering may occur before I've loaded the data and set the DataContext, meaning unnecessary re-paints and what-not.

Michael Nero
  • 1,396
  • 13
  • 24

2 Answers2

3

I usualy bind to a ViewModel directly in XAML. Then in OnNavigatedTo I trigger the view model to fetch its data async.

This allows me to show basic values from start (page title etc.). Then when I start fetching the data I can also activate a progressbar directly in the ViewModel and then remove it once the data is fetched.

vonLochow
  • 448
  • 2
  • 8
  • But if I'm loading data only from the local database where the time to load the data is very short, is it still advisable to show a "Loading..." screen? I'm not sure the very brief flash of a loading screen is preferable in that case. – Michael Nero May 24 '13 at 21:16
  • Is it always a very short time and how short is "very short"? If we're talking about 100ms you might wanna skip the progressbar and just handle it async without showing any progress. – vonLochow May 27 '13 at 06:45
1

I recommend you load your data asynchronously. OnNavigatedTo is one place where you can start the loading. If you're talking about a page that the user is almost certainly going to navigate to, then you may be able to start loading earlier.

I have a series of blog posts that look at how async has some friction with traditional OOP. There are a couple of posts that look at data binding in particular, e.g., asynchronous construction (the section on asynchronous initialization) and asynchronous properties (the section on data binding).

Just a few hours ago I announced the first stable release for my AsyncEx library, which includes the NotifyTaskCompletion types that you can use to kick off an asynchronous loading operation and have your view respond automatically (via data binding) when it completes.

But back to the core problem: you do have to show something while the data is loading. I recommend you do not consider this "unnecessary", but rather accept it as an opportunity to provide a better user experience. Think about what you want your app to look like on a slower phone or if there is an error loading the data. Any time there's I/O, design the "Loading..." and "Error" states as well as the "Loaded" state.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810