MainViewModel list with no parameters

Jul 17, 2011 at 1:30 PM
Edited Jul 17, 2011 at 1:51 PM

Just trying to wrap my head around this framework. I started with a databound app sample (seemed appropriate to my sample). In it we normally call a LoadData method to fetch some data from an outside service. I'm looking at a sample that's similar to the weather one you provide but there's one step backwards. I'm fetching a list of all sites before the user drills into one (a list of states, cities, etc.). Just struggling to get the initial list loaded.

Would I call DataManager.Current.Load<MainViewModel>() directly in the LoadData() method of the MainViewModel to fetch a list of cities or use something like the WeatherForecastVmLoader (in your sample) to fetch the list? I don't see anywhere the Vm Loader is being called so I assume it's being calls by the DataManager? Just trying to figure out how to get a list of items going initially without a key.

Right now I have the MainViewModel which would contain a list of locations (ItemViewModel in property Items) and the LoadData method would fill this in by fetching it from a data service (one that never expires during the life of the app, it's a pretty static list of locations). Once I've got the list I can see AgFx fitting in for each item as I can fetch the data based on some key in the location list.

Jul 21, 2011 at 12:01 AM

Hi -

It sounds like you're starting from the List template in the Windows Phone projects.  It uses a slightly different model because it doesn't have the sorts of facilities available to it that AgFx does.

In your case, I would do this in two steps:

First, create a ViewModel that represents the list of sites.  This is going to be a bit more like the WeatherForcecastVm than MainViewModel, but basically it'll look something like this (very simplified):

public class SitesViewModel : ModelItemBase {

// this may be a more complex collection, but i'll just use strings for now...

public ObservableCollection<string> SiteNames { 



// your data loader goes here, say it knows how to get sites from a state code like "CA" or "WA"


Now, on your main page, you'd have some XAML like this:

<ListBox x:Name="lbSites" ItemsSource="{Binding SiteNames}">


Which would be your list of sites.

In your Main.xaml.cs, you'd then do this:

lbSites.DataContext = DataManager.Current.Load<SitesViewModel>("WA"): // load all the sites in washington.

This will populate the ListBox listed above with the cities.

Then repeat the process for whatever your Site view model looks like, and bind that to whatever UI you need to on the ListBox's SelectedIndexChanged handler:

private void lbSites_SelectedIndexChanged(...) {

string siteName = lbSites.SelectedItem as string;


if (siteName != null) {

siteDetails.DataContext = DataManager.Load<SiteVm>(siteName);



Hope that helps,


Jul 21, 2011 at 12:25 AM
Edited Jul 22, 2011 at 3:36 AM

Actually I spoke too soon trying to follow the bouncing ball. The mixture of ModelItemBase and ModelItemBase<Type> is throwing me off, as is your example.

The problem is this, how do I get AgFx to load the SiteNames (the list of states, which in my case is a dynamic list from Xml on the service I'm calling). The data loader in your SitesViewModel is fine because it knows how to load the cities in a given state but I'm scratching my head at what the model looks like to load the initial set of states?

Hard to explain in words. I can define a set of vms that will load given a key, but how do I load that initial set of keys (in my case, provinces). That's the first step in my data model that I need to fetch and throw into Agfx because new ones may come online so I'll just cache the list throughout the life of the app.


Edit: Shawn posted some sample code to get going where the key didn't matter (since I'm loading a list of items). The thread can be found here.