We can do this using a ListView, but we have to get the feed, and to do that we need a library; specifically the NuGet package Microsoft.Http.Client Libraries.[Click on the image to see it full size]
|This is part of the on-going series of posts on Learning Xamarin; a complete index to the series can be found here.|
To create our application we’ll start with a new Forms application by firing up Xamarin Studio and selecting New Solution -> C# -> Mobile Apps -> Blank App (Xamarin.Forms.Portable
Let’s add structure to the app by adding these folders:
Creating the Model
Our Model is very simple, just one class: BlogEntry,
Of these properties, for now we’ll only be concerned with Title and PubDate.
Creating the View Model
The View Model (VM) will mediate between the model and the view. Again, ours is very simple. Create a file named BlogsViewModel,
Creating the View
Right-click on the Views folder and choose Add->New File -> Forms -> Forms Content Page XAML. Name the new page BlogsPage
In the Xaml we’ll add a ListView and modify the Cel to display both the Title and the PubDate,
The list itself binds to Blogs; the collection declared in the View Model. We’ll make the connection to the VM as the binding context in the code behind. Similarly, the two Labels bind their Text properties to properties on each BlogEntry object in the list.
The code behind for the view depends on our Services files. There are two files. The first, Urls.cs is pretty simple,
For now, the only URL we care about is the RSS feed for the Falafel blogs. Later we can add other URLs for other types of data, as hinted at by the enum.
The second, and more important class, is the DataLoader class,
You can see that this consists of a key public method, GetRecentBlogs and a helper method, GetBlogList. The first uses the HttpClient library referred to earlier. To add this to your project, right click on the project and select Add -> Add Packages and search for Microsoft.Http.Client. Check the Microsoft.Http.Client libraries and click Add Package. Hey! Presto! It is added to your project, along with the appropriate references.
Notice that this runs asynchronously. We’ll want to remember that in a moment when we return to the view to add the call to this method.
Back in BlogPage.xaml.cs you’ll create a constructor and a private method GetBlogs. The constructor calls GetBlogs and GetBlogs calls the loader asynchronously.
That’s it, your page will come up and a few moments later it will populate with the titles and the publication date.
There are a few essential things to take care of, such as showing an indeterminate progress indicator while loading the data and adding links to the blog titles, but we’ll leave those for another day.
I’ll be speaking on Xamarin at FalafelCon!
More information here.
Latest posts by Falafel Posts (see all)
- Matching Complex Query String Rewrite Rule in IIS - March 22, 2017
- Disable Content Filters in Sitefinity - March 8, 2017
- On Sitefinity Custom Widget Caching - February 22, 2017
- Dynamic Content Detail Widget Templates in Sitefinity - February 8, 2017
- Using Google Services in UWP C# Apps – Part 2 - February 7, 2017