Archive for the ‘Visual Studio’ Category

How to share code among different Universal Windows apps

22/04/2014 1 comment

By this time, every Windows developer knows about Universal Windows apps, that enable building applications on the converged Windows 8.1 and Windows Phone 8.1 platforms.

A key concept of this new approach is the Shared Project, a particular kind of project that doesn’t have any binary output, but simply allows sharing source between converged apps.

The new Univeral app projects

The new Univeral app projects

Of course, we have the possibility to tailor the design to each device. We can also use conditional if and partial classes in the Shared Project to define platform specific code, that will be compiled within each app. In this way, we have a single common point between Windows 8.1 and Windows Phone 8.1 projects, but we can also write custom code for each app.

But what if we want to share code among different Universal Windows apps, because for example we want to redistribute our libraries? First of all, we can think about using Portable Class Libraries. This type of project has been improved in Visual Studio 2013 Update 2 and now, if we target Windows 8.1 and Windows Phone 8.1, we’ll be able to use all the common APIs. However, PCL produces a single binary that works as is in all the supported projects. Handling divergent APIs requires using higher-level abstractions, such as dependency injection or IoC containers. We can’t simply use conditional if to define platform specific code.

Luckily, there is another solution. Some days ago has been released a Visual Studio 2013 extension called Shared Project Reference Manager that allows to use the same concept of Universal app Shared Project with almost any C#, VB.NET, C++ or WWA/JavaScript projects. Let’s see how to use it.

First of all, create two Class Libraries, one for Windows and one for Windows Phone. They will contain any platform specific code, if necessary.

Libraries for Universal apps

Libraries for Universal apps

Then, we need to add a new Shared Project to the solution. This template has been added to the IDE by the Visual Studio extension mentioned before.

Shared Project

Shared Project

After that, right click on the References node on both the class libraries, select the Add Shared Project Reference command and, in the new window, click on the shared project we just created.

Shared Project Reference Manager

Shared Project Reference Manager

Now we can start creating any file in this new Shared project, as we do in the standard Shared Project created by the Universal app project template: everything we add to it will be automatically available in both the platform specific libraries.

Shared Project Library

Shared Project Library

In this way, we can develop and distribute libraries that allow code reuse, and thanks to the Shared Project all the common portion between Windows 8.1 and Windows Phone 8.1 can be written in a single place, with the ability to use conditional if to define custom behavior. Let’s see a simple example:

// In the project "UniversalLibrary.Shared
public class Utility
    public string GetGreetings()
        var greetings = "Hello from {0}!";

        greetings = string.Format(greetings, "Windows");

        greetings = string.Format(greetings, "Windows Phone");

        return greetings;

When compiled, the UniversalLibrary.Windows library will contain the code at line 9, while UniversalLibrary.WindowsPhone will contain the one at line 13. So, starting from a single code file, we have easily realized two class libraries.

Note that, in most cases, the Windows and Windows Phone projects remain empty, as they are needed only to generate the output libraries that can be added to our Universal apps or distributed.


My article about new XAML controls in Windows 8.1 on ioProgrammo

17/10/2013 Comments off

A new article of mine has been published on n°188 of ioProgrammo (November/December 2013). It is an introduction to the new XAML controls that come with Windows 8.1.

ioProgrammo November/December 2013

ioProgrammo November/December 2013

Categories: .NET, Visual Studio, WinRT

Windows Store apps Succintly: development made easy with Syncfusion

03/09/2013 2 comments

Providing a tool that allows to approach the Windows Store apps development quickly and easily: this is the goal of Windows Store apps Succintly, a free eBook from Syncfusion’s Succinctly Series, that in less than 200 pages shows everything you need to start programming app for Windows 8.

Windows Store apps Succintly eBook

Windows Store apps Succintly eBook

The book is written by John Garland and is available in both Kindle and PDF formats. It requires a knowledge of C# and the basics of XAML, necessary to address the various topics, that are presented in 7 chapters:

  1. Core Concepts (the Windows Runtime and the anatomy of a Windows Store app project)
  2. XAML, Controls, and Pages (how to create UI for Windows 8)
  3. Application Life Cycle and Storage (the new Application Execution Model and how to save and retrieve application files during Life Cycle events)
  4. Contracts and Extensions (Searching, Sharing, using Pickers, etc.)
  5. Tiles, Toasts, and Notifications
  6. Hardware and Sensors (using GPS, compass, gyroscope, accelerometer, and integration with Cameras and Microphones)
  7. Deployment (how to submit an app, trial mode, in-app purchases and Ads)

Each argument is clearly described and comes with useful code samples, so that the discussions has always a very handy connotation. There are also some cool tips that can be interested even for developers that already know all the topics, so everybody involved in Windows Store apps development should think about reading this book.

The kind of the book doesn’t allow to deepen the various arguments too much, but every topic is accompanied by links for in-depth analysis, thanks to which we can continue the study by ourselves.

In conclusion, if you are searching for an introduction to Windows Store apps development and you want to be cost-effective as soon as possible, Windows Store apps Succintly is the book for you. But remember that, even if you have are a skilled developer, this book can help with useful tips.

Categories: .NET, C#, Visual Studio, WinRT

ASP .NET 2012.2 Update issue with Azure Storage Library

21/02/2013 Comments off

There is an issue with the new ASP .NET MVC 4 templates that come with ASP .NET 2012.2 Update and the Azure Storage Library that is available on NuGet. Internet Application and WebAPI templates use the Microsoft.AspNet.WebApi.OData package from NuGet, that depends on Microsoft.Data.OData.5.2.0, while Azure Storage Library needs exactly the 5.0.2 version of this library.

So, if we try to add the package WindowsStorage.Azure, we’ll obtain the following error message:

OData Issue with Azure Storage Library

OData issue with Azure Storage Library

Let’s hope that a new version of this library will be published soon in order to solve the problem.