WeatherView, a C# Universal App Sample
- Posted: Jun 04, 2014 at 6:00 AM
- 14,101 Views
- 6 Comments
Loading User Information from Channel 9
Something went wrong getting user information from Channel 9
Loading User Information from MSDN
Something went wrong getting user information from MSDN
Loading Visual Studio Achievements
Something went wrong getting the Visual Studio Achievements
Today's project shows off a simple yet "real" universal application that's already available in the Stores for free, but the source is available too...
This is a sample which creates a universal Windows app written in C#, that runs on both Windows 8.1 and Windows Phone 8.1. You can see the app the sample is based on in the Windows Store and the Windows Phone Store.
What the app does
This app uses geolocation and two web services to create a graphical weather app. First, it uses your location to create a query to send to the Weather Underground web service. This service returns a JSON string, which we parse to extract the current weather, and two forecasts. (Setting up the queries can be a little tricky, which is why I wrote this sample in the first place!)
We take this weather information returned by Weather Underground, and display it: "60 F in Redmond!" - but then more interestingly, we ask Bing for an image to go with it. For example, if the weather forecast is “raining frogs” we ask Bing for some images related to “raining frogs”, and then display that in the background.
What you need to do first
As this app uses some webservices, you will need to create accounts with them in order to obtain API keys. Without the keys, the app will not function. The good news is that you can obtain these keys for free, although they may time-out, or only operate for a set number of calls per month. If you are going to write a commercial app, you need to carefully consider the terms, conditions and potential costs of using the web services. For experimenting, the free accounts are more than adequate.
Structure of the app
The app is a universal app, which means it contains both the desktop and the phone projects within the one Visual Studio solution. You can choose which to build by selecting the default start-up project.
Both projects share the same MainPage.xaml and C# code-behind source, and the MainPageViewModel. There are a few cases when some Phone-specific or Desktop-specific code is commented out for compile time. For example, the Phone does not need the SettingsFlyout code.
The app is written following the MVVM design model, and so the XAML file references parameters which are defined in the MainPageViewMode file.
A universal Windows app written in C# demonstrating geolocation and webservices.
- Using webservices to obtain weather forecast data,
- Using Bing/Azure to obtain an image related to a search term
- Written following MVVM using C#
Here's a snap of the Solution;
As you can see, you share what you can share, you customize for a device where you need, or want, too.