Some news for developers from Nokia World

Yesterday Nokia has showed off to the world, during Nokia World, many great news about Windows Phone and the Lumia series. In fact, Nokia has announced 2 new phones which falls into the “phablet” category (they both share a 6” screen), a tablet (based on Windows 8.1 RT) and many important apps that are coming in the next weeks, like Instagram, Vine and Flipboard.

They all are really good news for developers, since I’m sure that they will push even further the Windows Phone ecosystem and more phones around the world means more opportunities for devs Smile

To get all the information about the new products and apps, I suggest you to visit the official Nokia blog called Nokia Conversation, which covers in details all the news. In this post, I would like to focus on what’s interesting for developers regarding the new announcements.

Lumia 1320 and Lumia 1520

The new members of the Lumia family are part of the so called “phablet” category: they are phones, but with a very big screen (6 inches). The Lumia 1520 is a high end phone, which features the new resolution introduced with GDR3, which is 1080p (1080×1920), together with a quad core processor (another new feature for a Windows Phone device) and a 20 MPixel camera. The Lumia 1320, on the other side, is a mid end phone, with lower tech specs but with a much cheaper price. It features a 720p resolution (720×1280), which is not new for Windows Phone (it’s one of the new resolutions introduced in Windows Phone 8), but it’s new for Nokia, since previously all the other phones used only the WVGA (480×800) and WXGA (768×1280) resolutions.

And here comes the first news for developers: 1080p and 720p resolutions are very different from the other ones, since they offer a 16:9 aspect ratio, instead of the usual 15:9. This means basically two things:

  • If you’ve used a rigid layout in your application (using, for example, fixed margins and sizes), there’s a good chance that your app will not look very good on one of these devices. The solution is to use a fluid layout, which is able to adapt to the screen size. For example, when you create a Grid, try not to use fixed sizes for columns and rows. There’s a good article in the MSDN documentation about this topic.
  • Windows Phone 7 apps don’t support the new resolutions: if for WXGA phones is not a problem (aspect ratio is the same, so layout is simply adapted but not distorted), it’s not the same for 720p and 1080p. Since aspect ratio is different, there’s the chance that the layout will look bad: for this reason, Windows Phone 7 apps that are executed on a 720p or 1080p device will look “cut” at the top, since a black bar will be added to keep the 15:6 aspect ratio. The only solution to fix it is to upgrade your project to Windows Phone 8: only this way you’ll be able to fully support the new devices.

If, in the past, developers didn’t care too much about 720p, since only a few models with low market share supported it, now it’s definitely time to change and fully embrace it: otherwise, people that will buy the Lumia 1320 or the Lumia 1520 won’t be able to experience your apps with full quality.

Nokia has scheduled one webinar in two different dates and times (6 November and 7 November) on how to adapt your application to fully support the new resolutions. Don’t miss it!

Plus, if you remember my previous post, you should know that Microsoft is going to release soon a SDK update which features a new GDR3 emulator with the 1080p resolution. However, since aspect ratio is the same, you can start right now to test your apps for the new Lumia devices with the 720p one. If your app looks good a on a 720p phone, it will look good also on a 1080p one (unless you’ve used low quality images, in this cases they can look bad on a high resolution screen).

image_thumb[5]

Lumia 2520

Nokia has announced also his first tablet, a Windows 8.1 RT device with a look and feel very similar to the Lumia smartphones. It’s a quad core ARM device with a 1080p resolution which, as every other Windows RT device, is optimized for the new Windows Store apps introduced in Windows 8: desktop apps won’t run, except for the ones provided by Microsoft (like the Office suite).

From a developer point of view, it’s a new opportunity: if you’ve already have a Windows Phone app that could make sense on a larger screen, you can start thinking about a porting for Windows 8. There’s some work to do, but there are many shared classes between the Windows Runtime for Windows 8 and the Windows Runtime for Windows Phone, like storage APIs, sensors APIs, geolocalization APIs, etc. I suggest you to start looking at this series of webinars published on Channel 9 made by Ben Riga: they will introduce you to concepts like Model-View-ViewModel (which we covered deeply in this blog too) and Portable Class Libraries, which are two of the tools that will help you to reuse your Windows Phone code for a Windows 8 app.

image_thumb[11]

Nokia Developer Offers

The most interesting news for developers is, probably, the launch of a new program called Nokia Developer Offers, which gave to developers, for free, a set of very useful tool and resources:

  • A token to subscribe to the Store or to renew an existing subscription
  • A license for BugSense Performance Monitoring Solution, which is an analytics platform
  • A 1 year license for Telerik controls for Windows Phone
  • A 1 year license for Infragistics controls for Windows Phone

However, there are a couple of requirement to be able to join the program and get all these goodies for free:

  • You have to be subscribed to DVLUP, so if DVLUP is not available in your country yet, I’m sorry but you’ll have to wait
  • If you’re a new developer, you should have published at least 2 application on any mobile platform: not just Windows Phone or Asha, but also Android, iOS, etc.
  • If you already had a Nokia Developer Premium account in the past, you should have released at least one app after you’ve subscribed.

The Nokia team will take a few days to verify that you’re eligible. Once you’re in, it’s important to know that, in the future, you could get access to new benefits (for example, discounts on the XP points required to get rewards on DVLUP): to continue to be part of the program, you’ll have to publish at least a new app or an update to an existing app within 6 months from your subscription.

If everything I told you sounds good, go to the official website and apply!

Posted in Windows Phone | Tagged , | Leave a comment

Preview Program for Developers & GDR3

Yesterday Microsoft has announced two big news for all the Windows Phone users and developers. Let’s start to dig them!

GDR3 announcement

Microsoft has officialy revelead the third Windows Phone 8 update, called Update 3 (or GDR3). Like GDR2, the focus of this update are consumers, since it adds a coulpe of much requested features like:

  • A new driving mode, that is automatically enabled when you’re driving and that automatically turns off all the notifications so that you can’t be distracted.
  • A new option in Settings to lock screen rotation.
  • Custom ringtones support for all the notification types, like mails and text messages.
  • Improved task switcher, with an option to close suspended apps.
  • Now, during the first wizard, you’ll be able to restore a backup also using a Wi-Fi connection.

Anyway, the most important new feature, which is interesting also for developers, is the support for a new resolution, which is 1080p (1080×1920). Devices with this resolution will be able to display a new column of tiles in the main screen (while, even if some rumors stated the opposite, phones with lower resolution will keep the current layout with two columns). From a developer point of view, 1080p and 720p (which is one of the already supported resolutions) are the same: they have the same aspect ratio (16:9), which is different from the aspect ratio of the other two resolution (15:9). What does it mean? That it’s important to test your app with this new resolution if you want to deliver to your users the best experience: since the aspect ratio is different, some images and look could look different. In some cases, you’ll need to load specific images for the new resolution and to use a fluid layout, which is able to automatically adapt to the screen’s size. If you want to learn more about this topic, you can take a look at this article from the official MSDN documentation.

This new resolution will also make you reconsider the choice to not update your applications to Windows Phone 8: since 1080p has a different aspect ratio, 7.8 applications will look “cut”, with an empty space at the top of the screen. So, if you want that your app looks best on every device, you’ll have to upgrade it to Windows Phone 8, which is able to support all the resolutions.

You’ll be able to test your app against the new resolution very soon: Microsoft is going to release a SDK update, that will introduce new emulators aligned with GDR3. From a developer point of view, there are also other other changes, that are detailed in the MSDN documentation:

  • The memory cap for background audio agents has been raised from 20 to 25 MB on devices with 1 GB of RAM or more.
  • The memory cap for apps has been raised to 570 MB on devices with 2 GB of RAM.
  • There’s a new Uri scheme (ms-settings-screenrotation: ) to quickly access to the Screen Rotation setting.
  • Icon and tile of your application are now used also in the updated Task switcher view.
  • You can set a custom sound for toast notifications, using reflection.
  • The Internet Explorer’s viewport has been updated: this can impact applications that use WebBrowser control or mobile websites.

If you need to check if the current phone is running GDR3 or not, you’ll need to use the Environment.OSVersion.Version property: a GDR3 device will return 8.0.10492.

Preview Program for Developers

But what if you want to test your apps on a real GDR3 device? Here comes the most interesting news for developers: Microsoft has opened a Preview Program for Developers, which allows to get early access to new Windows Phone updates, without having to wait for carrier’s approval. This way, developers can get their apps ready when the new update will be publicly distributed to every user. To be part of the program you’ll just need:

  • A developer unlocked device. Phones can be unlocked using the Windows Phone Developer Registration tool that comes with the SDK. If you’re a registered developer, you’ll be able to unlock up to 3 phones; otherwise, you can unlock just one phone.
  • A paid developer account, which allows you to publish your apps on the Store, or a free App Studio account.

Once you have met this requirement, you can go to this page, where you can review the requirements and the program’s conditions: you’ll find a link that will redirect you to an application in the Store. You’ll simply have to download it, run it and, after accepting terms and conditions, enable the preview program. From now on, when you look for new updates in the Settings hub, you’ll directly contact Microsoft’s servers to get the latest version of the OS: if you do it right now, the GDR3 download and installation process will start. Cool, isn’t it?

In the end, here are some important things to know about the program:

  • The GDR3 version that is distributed with the Preview program is final, but it’s just the OS update offered by Microsoft. Diver and firmware updates from the phone’s vendor will be provided with the official GDR3 release.
  • GDR3 can be installed on any phone, but it should already have installed GDR2. The minimum required OS version is 8.0.10322.71.
  • There’s no turning back: after you’ve installed GDR3, you’ll be able to install future updates but not to go back to the previous state.
  • The update is incremental, so you won’t lose any data.
Posted in Windows Phone | Tagged , | Leave a comment

Windows Phone Week: a global event for all the developers!

WindowsPhone-WebBanner-01

Everything started like a sort of dream: the Windows Phone Development MVP team is made by very passionate people (as like every other MVP group) and it regularly happen that we are involved in organizing or supporting local events about Windows Phone. Me and my Dutch friend Joost Van Schaik, some months ago, started to wonder if we can use our passion to create something “more global”, that could involve many countries around the world. The idea was great, but also complex to realize: money, logistic, promotion, agenda; there are a lot of things to manage when you want to create something like this, so we temporary suspended it, to wait for the right time. That time arrived thanks to our mutual Brazilian friend and MVP Rodolpho Marques, which relaunched our idea by proposing a common event in Italy, Netherlands and Brazil. A mail exchange between me, Joost, Rodolpho and our MVP referral in Microsoft and the idea became reality: thanks to Microsoft and Nokia, we had the support we need to turn the idea into a real event!

I’m happy to announce that the Windows Phone Week is now live, with an official website and an official Twitter account: between the end of September and the end of October, many countries around the world will host a full day event about Windows Phone development. As you can see in the official website, many other MVP friends have supported our idea, so the initial country list has been extended from 3 to 13!

Every country will have their own agenda and registration: some countries will host an hackaton, some countries will host a traditional conference, some other countries will host a mix of both. And I can assure you that there will be lot of fun and surprises! Please refer to the official website to find your nearest location and to see all the details about the day: if you’re from Italy, I will be more than happy to meet you at the Italian event, that will be hosted by Microsoft Italy in Milan on Monday 30th September.

If you’re a Windows Phone developer, spread the word and join us!

Posted in Windows Phone | Tagged , | 2 Comments

New reduced price to become a Windows Phone developer

Very quick post to highlight a very important change for Windows Phone developers: as you know, development tools are completely free but, to publish applications on the Store, you have to pay an annual 99$ fee, which is required to cover all the costs needed to run the Store (certification process, reports, etc.). During the summer, Microsoft has launched a temporary promotion, that reduced the price to 19$. Now the promotion has ended but Microsoft, with a tweet posted by Windows Phone program manager Todd Brix, has announced that the 19$ reduction will be permanent. So, from now on, you’ll just have to pay 19$ to purchase a new subscription or to renew an already existing one. You don’t have anymore excuses to publish your Windows Phone apps! Smile

Posted in Windows Phone | Tagged | 1 Comment

App Studio: let’s take a look at a generated project

In the previous post we’ve seen how App Studio, the new tool by Microsoft, is able to generate a full Windows Phone application without having to write a single line of code. Anyway, even if the tool is very powerful, it doesn’t support every possible Windows Phone feature or scenario: for this reason, other than just downloading the XAP, you can also download the full Visual Studio project, so that you can edit and customize it.

In this post we’ll see the structure of this project and how it works, so that you can understand how to add new views or new features. Let’s start to download the project from App Studio, using the link provided at the end of the creation process: for this post I’m going to use the Comics application I’ve created in the previous post.

The project

When you’ll extract the compressed file you’ve downloaded, you’ll find a folder called src, which contains the real Visual Studio project.

image

Open the solution using the file Solution.sln with Visual Studio 2012 or 2013 Preview (since it’s a Windows Phone 8 project, it’s supported by both versions): you’ll find that the application is made by 3 projects, splitted in various folders. It’s an application based on the Model-View-ViewModel pattern, which is the most famous development pattern in the XAML world.  I’ve deeply talked about it in my series of posts about Caliburn Micro, which is one of the most popular MVVM frameworks. In this project, MVVM isn’t implemented using a specific toolkit or framework, but with custom classes that are built in the project. We’ll see the details in the rest of the post. Another important thing to highlight is that the project uses NuGet: most of the third party libraries are installed using this tool, but to use it you have to make sure that, in the NuGet settings (right click on the project, choose Mange NuGet packages and then click on Settings) the option Allow NuGet to download missing packages during the build in the Package restore section is enabled. This step is required because the project is configured using the Package restore NuGet’s feature: basically, the project just contains a reference to the packages that have been installed, but the real packages’ files are not included in the solution. They will be downloaded the first time the solution is built: this way, the size of the zip file generated by App Studio is reduced.

Here is how the solution looks like in Solution Explorer:

image

The folders contains various projects and files: the UI folder contains the real Windows Phone application (the one you’ll need to launch on the emulator or on a device to test it), while the Data folder contains two other projects called Entities and Repositories, which take care of handling the application data. Let’s see them in details.

Entities

This project contains all the classes that define the entities that are used in the app. You’ll find some basic entities (for common scenarios, like RSS or Twitter feeds), plus an entity for every data source you’ve defined in the web tool. For example, in my application I have a created a ComicsCollection (if you remember, it’s a collection to store a list of comics): the project will contain a class called ComicsCollectionSchema, which is the base class that identifies a comic (and that exposes the properties we’ve defined when we’ve created the data source columns in the visual editor).

As I’ve already mentioned in the beginning of the post, the project generated by App Studio is built using the MVVM pattern: for this reason, every entity is defined to support this pattern, by implementing the BindableBase class. It’s a custom class built-in in the project, that offers an implementation of the INotifyPropertyChanged interface: it’s one the key concepts when you deal with binding, since it allows to propagate property changes to the user interface automatically. If you take a look at one of the entities’ definition, you’ll notice that the value of each property is set using a method called SetProperty : it takes care of storing the new value and to raise the NotifyPropertyChanged event, so that the control which is in binding is notified of the update. This way, you can simply connect a ComicsCollectionSchema object’s property defined in a ViewModel to a control in the View simply like this:

<TextBlock Text="{Binding Path=Title}" />

Except for this, it’s a standard class that defines an entity: I have a property for every field, like Title, Author and Description.

Here is the code:

public class ComicsCollectionSchema : BindableBase
{
    /// <summary>
    /// Identifier for instances created according ComicsCollectionSchema Data Schema.
    /// </summary>
    public Guid Id { get; set; }

    private string _author;

    /// <summary>
    /// Gets/Sets value of Author column.
    /// </summary>
    public string Author
    {
        get { return _author; }
        set
        {
            SetProperty(ref _author, value);
        }
    }

    private string _title;

    /// <summary>
    /// Gets/Sets value of Title column.
    /// </summary>
    public string Title
    {
        get { return _title; }
        set
        {
            SetProperty(ref _title, value);
        }
    }

    private string _image;

    /// <summary>
    /// Gets/Sets value of Image column.
    /// </summary>
    public string Image
    {
        get { return _image; }
        set
        {
            SetProperty(ref _image, value);
        }
    }

    private string _description;

    /// <summary>
    /// Gets/Sets value of Description column.
    /// </summary>
    public string Description
    {
        get { return _description; }
        set
        {
            SetProperty(ref _description, value);
        }
    }
}

Repositories

The project uses an approach based on repositories to make data available in the application. Like for entities, the project contains a repository for each data source we’ve defined in the application: in this case, we’ll find a class called ComicsCollection. This class acts as a middle man between the application and the data: the queries on data (in this case, to retrieve the comics list) aren’t performed directly by the ViewModel, but by the repository class, which exposes the basic methods to load and filter the available data set. The generated class will be different according if you’re using a static or dynamic collection: in the first case, you’ll find a method called GetData() which takes care of initializing the data that has been defined in the web tool; in the second case, instead, it will provide the basic operations to query the dynamic service that has been generated with the application. Here is a sample of the second scenario:

public class ComicsCollection : IComicsCollection 
{
    private readonly IJsonDataSource _jsonDataSource; 

    private const string DataServiceUrl = "http://dowapp.cloudapp.net/api/data?clientId={0}&appId={1}&datasourceName={2}";

    /// <summary>
    /// Initializes a new instance of the <see cref="ComicsCollection" /> class.
    /// </summary>
    /// <param name="jsonDataSource">A JSON based data source.</param>
    public ComicsCollection(IJsonDataSource jsonDataSource)
    {
        _jsonDataSource = jsonDataSource;
    }

    /// <summary>
    /// Retrieves the data from a dynamic data service (URL specified in DataServiceUrl) , in an observable collection of ComicsCollectionSchema items.
    /// </summary>
    /// <returns>An observable collection of ComicsCollectionSchema items.</returns>
    public async Task<ObservableCollection<ComicsCollectionSchema>> GetData()
    {            
        return await LoadData();
    } 

    private async Task<ObservableCollection<ComicsCollectionSchema>> LoadData()
    {
        var items = await _jsonDataSource.LoadRemote<ComicsCollectionSchema[]>(string.Format(DataServiceUrl, "1426","5b07b520-7b22-4756-97ae-4d92660afa17", "ComicsCollection"));
        return items != null ? new ObservableCollection<ComicsCollectionSchema>(items.OrderBy(i=>i.Author)) : new ObservableCollection<ComicsCollectionSchema>();
    }
}

How does it work a dynamic data source, under the hood? It’s a REST service (which URL is set in the DataServiceUrl constant), which returns data using the JSON format.

The repository class uses a JsonDataSource class, which identifies a REST service that exchange data with the JSON format: the LoadRemote<T>() method (that is used in the LoadData() method) takes care of performing a network request and getting the data in JSON format from the service. Then, it’s able to automatically deserialize and convert it into a specific type, which is passed as T parameter: in this sample, the JSON data is converted into an array of ComicsCollectionSchema objects (which, if you remember, is the basic entity that defines a single comic). The method simply requires as parameter the URL of the service (that is combined with some fixed identifiers that are used to generate the unique URL) and return a collection of items, which is converted into an ObservableCollection<T>() to have built-in support to binding.

As you can see, every reporitory class implements an interface, which simply described the available methods. For example, the ComicsCollection class implements the following IComicsCollection interface:

public interface IComicsCollection
{
    Task<ObservableCollection<ComicsCollectionSchema>> GetData();
}

We’ll see later why this class is useful in the project’s ecosystem.

The Windows Phone application

The biggest and most important project is the Windows Phone application, which contains many folders to organize the application structure. Since the application is built with the MVVM pattern, the two most important folders are View and ViewModel: the first one contains the application pages, which are the XAML files; the second one, instead, contains the ViewModels, which are the classes that define the interaction logic behind the view. In the View folder you’ll find a view for every page defined in the web tool: in the comics application, there will be a page called Comics_List.xaml (which is the main page that contains a panorama control with the comics list) and one called Comics_Detail.xaml (which, instead, is the page that display the details about the selected comic). In the same way, in the ViewModel folder you’ll find the two view models that are connected to the views: Comics_ListViewModel e Comics_DetailViewModel.

How Views and ViewModels are connected together? First we need to do a step back and to highlight that the whole project is based on the dependency injection approach: it means that basically every class of the project (including ViewModels) isn’t manually created, but it’s automatically resolved at runtime by a class called dependency container. What does it mean? Let’s explain it with a concrete example: we’ve already seen that, to interact with the data, the project uses the repository approach, by offering a class called ComicsCollection that retrieves the data from the cloud service generated by the tool. This means that we can expect that, somewhere in the ViewModel of the main application page, the class creates a new ComicsCollection object and calls the GetData() method to populate the list:

public async void RefreshComics()
{
   ComicsCollection collection = new ComicsCollection();
   await collection.GetData();
}

Which is the problem of this approach in a complex application? That if we need to swap the data source (for example, because we’re in testing stage and, instead of using real data from the service, we want to use fake data), we need to change every point in the application in which we’ve created a new instance of the ComicsCollection class. Dependancy injection, instead, allows to specify in one single point (tipically, when the application starts) which concrete implementation we want to use when we require to work with a class. This is why the ComicsCollection class implements an IComicsCollection interface: we can register, in the dependency container, which implementation we want to use for that interface. The App Studio projects uses Unity (which is an open source project by Microsoft) to support dependency injection: you can see its implementation in the Ioc folder, which contains a class called Container: its purpose is to register all the application classes. As you can see in the class constructor, by using the RegisterType<T, Y>() method of the UnityContainer class, the application specifies, for every interface, which is the concrete implementation to use (the T parameter is the interface, the Y one is the implementation). Here is a sample implementation:

public Container()
{
    _currentContainer = new UnityContainer();

    _currentContainer.RegisterType<ILiveTileService, LiveTileService>();
    _currentContainer.RegisterType<ILockScreenService, LockScreenService>();
    _currentContainer.RegisterType<IDialogService, DialogService>();
    _currentContainer.RegisterType<IReminderService, ReminderService>();
    _currentContainer.RegisterType<IShareService, ShareService>();
    _currentContainer.RegisterType<ISpeechService, SpeechService>();
    _currentContainer.RegisterType<INavigationService, NavigationService>();
    _currentContainer.RegisterType<IJsonDataSource, JsonDataSource>();
    _currentContainer.RegisterType<IXmlDataSource, XmlDataSource>();
    _currentContainer.RegisterType<IYoutubeDataSource, YoutubeDataSource>();

    _currentContainer.RegisterType<IComics_ListViewModel, Comics_ListViewModel>();
    _currentContainer.RegisterType<IComics_DetailViewModel, Comics_DetailViewModel>();

    if (!System.ComponentModel.DesignerProperties.IsInDesignTool)
    {
        _currentContainer.RegisterType<IComicsCollection, ComicsCollection>(new ContainerControlledLifetimeManager());
    }
    else
    {
        _currentContainer.RegisterType<IComicsCollection, FakeComicsCollection>(new ContainerControlledLifetimeManager());
    }    
}

The application simply registers in the bootstrapper every available service and view model: it’s important to keep an eye on this class; if we want to add new pages or new services to the application, we’ll have to register them in this place. We can see also a sample of the scenario I’ve previously described: if the user is working with a page that uses the ComicsCollection class in the Visual Studio or Blend designer, it uses an implementation with fake data called FakeComicsCollection; otherwise, it uses the real one that gets data from the cloud service.

You can also see, that others than View and ViewModels, the application uses the concepts of services: basically, they are classes which takes care of satisfying a specific scenario and that are stored inside the Services folder. Instead of writing the code to perform a task directly in the ViewModel, it’s defined in a separate class, which is a service: then, in the ViewModel, we interact with this class to perform our operation. You can see, for example, that we have a LiveTileService, which exposes methods to create secondary tiles or to update them; a LockScreenService, to set an image as lock screen background; a ShareService, to support sharing on social network. One of the most important (that we’ll cover in the next post) is NavigationService, which can be used to manage navigation between different pages directly in the ViewModel.

Now that we’ve seen how classes are registered,  a question may arise: how are they resolved and used in a ViewModel? The most common way is completely automatic: it’s enough to declare the interface of the class we want to use as parameter of the public constructor. Since both the interface and the ViewModel are registered in the container, Unity will automatically resolve them. To see an example, open any ViewModel of the application: here is a sample of the first lines of code.

public partial class Comics_ListViewModel : BindableBase, IComics_ListViewModel
{       
     private readonly IDialogService _dialogService;
     private readonly INavigationService _navigationService;
     private readonly ILockScreenService _lockScreenService;
     private readonly IComicsCollection _comicsCollection;

     public Comics_ListViewModel(IDialogService dialogService, INavigationService navigationService, ILockScreenService lockScreenService, IComicsCollection comicsCollection)
     {
         _dialogService = dialogService;
         _navigationService = navigationService;
         _lockScreenService = lockScreenService;
         _comicsCollection = comicsCollection;
     }
}

As you can see, the public constructor of the ViewModel includes a parameter for every service and class that it needs to use: automagically, every interface will be “injected” (thus, the dependency injection name) with the concrete implementation declared in the container. This way, we’ll be able to use our services and classes without having to create a new instance each time. For example, the sample usage of the ComicsCollection class we’ve previously seen can be converted simply in:

public async void RefreshComics() 
{
    await _comicsCollection.GetData();
}

Is there another way to resolve a class using dependency injection? Yes, and we can introduce it by speaking about how Views and ViewModels are connected together in the Windows Phone project. The used approach is called ViewModelLocator, which is a class that acts as a middle man between the Views and the ViewModels: it simply exposes every ViewModel as a property, which is, in the end, defined as DataContext of every view. You can find the ViewModelLocator implementation in the Services folder and it’s called ViewModelLocatorService. This service offers a sample of another way to get a reference to a class using dependency injection:

public class ViewModelLocatorService
{
    // IoC container
    private readonly IContainer _container;

    /// <summary>
    /// Initializes a new instance of the <see cref="ViewModelLocatorService" /> class.
    /// </summary>
    public ViewModelLocatorService()
    {
        _container = new Container();
    }

    /// <summary>
    /// Gets the reference to a Comics_ListViewModel.
    /// </summary>
    public IComics_ListViewModel Comics_ListViewModel
    {
        get { return _container.Resolve<Comics_ListViewModel>(); }
    }

    /// <summary>
    /// Gets the reference to a Comics_DetailViewModel.
    /// </summary>
    public IComics_DetailViewModel Comics_DetailViewModel
    {
        get { return _container.Resolve<Comics_DetailViewModel>(); }
    }
}

As you can see, we have a public property for each ViewModel: if we want to add new pages to our application, we should keep an eye also on this class, since we’ll have to add a new property for every view. And here comes the manual usage of the dependency injection’s container: since, in this case, we can’t resolve the class automatically (we explicity need to create a new instance of the ViewModel) we ask to the container for the instance that has been registed at startup using the Resolve<T> method, where T is the ViewModel’s type we want to require. This way, since we’re not manually creating a new instance but we’re asking to the container to get the registered one, all the ViewModel’s dependencies (remember all the services and classes that we’ve added as parameters of the ViewModel’s public constructor?) will be automatically satisfied.

In the end, the ViewModelLocatorService class is exposed by the application as global resource, so we can interact with it directly from XAML and assign every ViewModel to every View. If you open the App.xaml file you’ll see that the class is registered in the resources section:

<Application.Resources>
    <sys:String x:Key="AppName">Comics Tracker beta</sys:String>

    <!-- ViewModel locator -->
    <services:ViewModelLocatorService x:Key="ViewModelLocator"/>       
</Application.Resources>

If you open any View of the application, you’ll see that the DataContext property of the page is assigned to its specific ViewModel thanks to the ViewModelLocator resource we’ve previously defined:

<phone:PhoneApplicationPage 
x:Class="WPAppStudio.View.Comics_List"
DataContext="{Binding Path=Comics_ListViewModel, Source={StaticResource ViewModelLocator}}">

<!-- content of the page -->

</phone:PhoneApplicationPage>

It was a long ride

Yeah, it’s been a long ride: the project generated by Visual Studio can be complex and scary, especially if you aren’t an experienced Windows Phone developer, so it took a while to explain how things are working. In the next post, we’ll see how to start customizing our poject to add new pages and new data source. Stay tuned!

Posted in Windows Phone | Tagged , | 2 Comments