This project has moved. For the latest updates, please go here.

Real-world example

Jul 21, 2011 at 4:06 AM

In trying to get my head wrapped around this I thought I would present where my data is coming from and see if I can figure out how I can get Agfx and this to play well together. This is a weather app so most of the forecast stuff will be similar to the sample in the codebase (however the first step is tripping me up).

First we need to fetch a list of all sites available. This is currently available in xml format here:

http://dd.weatheroffice.ec.gc.ca/citypage_weather/xml/siteList.xml

Each site has a code, a name, and a province associated with it. I would see loading this list at start of the app and it would continue the life of the app.

Now once I have the list loaded, I can use LINQ to split it up and say only show a list of cities for Alberta (provinceCode = AB). With that list I can bind it to a listbox, let the user select a city (Athabasca, code = s000001) and form the url to the forecast feed. The feed for Athabasca is:

http://dd.weatheroffice.ec.gc.ca/citypage_weather/xml/AB/s0000001_e.xml

From there you just parse the xml to get the full forecast (XSDs are available here: http://dd.weatheroffice.ec.gc.ca/citypage_weather/schema/ for generating a .cs class for deserialization) and present some kind of UI.

So the starting point for me is how to get Agfx to load the initial list of sites. There's no key to load the list but each site becomes its own key to load the data later for a specific site.

Thanks.

Coordinator
Jul 21, 2011 at 9:35 PM

Hi -

Just use a static key, it doesn't really matter.

 

public class SiteListVm : ModelItemBase {

	public const string Default = "_Default";



	public ObservableCollection<Site> Sites {

		get;set;

	}

	public class SiteListDataLoader : IDataLoader {

		public LoadRequest GetLoadRequest(LoadContext context, Type objectType) {

			return new WebLoadRequest(context, new Uri("http://dd.weatheroffice.ec.gc.ca/citypage_weather/xml/siteList.xml"));

		}



		public object Deserialize (LoadContext context, Type objectType, Stream data) {

			SiteListVm vm = new SiteListVm();

			vm.LoadContext  = context;



			var sites = ParseXml(data);



			foreach(var site in sites) {

				var newSite = new Site(site);

				vm.Sites.Add(newSite);

			} 

			return sites;

		}

	}

}

then just do:

this.DataContext = DataManager.Current.Load<SitesVm>(SitesVm.Default);

Jul 22, 2011 at 4:34 AM

Thanks for this. Got it to work. Had to make a couple of changes you posted:

  1. The Deserialize function needs to return the SiteListVm, not the sites
  2. Couldn't seem to get it to work if I just implemented IDataLoader. I had to make it IDataLoader<SiteLoadContext>. Otherwise I kept getting an argument error

Also if anyone is reading this, the DataContext line should read DataManager.Current.Load<SiteListVm>(SiteListVm.Default);

Thanks! Off to coding now...

Coordinator
Jul 22, 2011 at 7:59 PM

OK, well that's what I get for typing code off the top of my head, sorry for the typos and thanks for calling them out.

Glad to help!