Category Archives: Windows 8

Building 2Day for Windows 8: under the hood

In this (long) post, I share some of the techniques I used to build 2Day for Windows 8 using C# and XAML.

2Day is a powerful todo-list app with synchronization capabilities. If you haven’t given it a try yet, grab it here for Windows 8 and here for Windows Phone.

placeit

Data access layer: SQLite (but not only)

No surprise here I’m using SQLite. Because I’m not big fan of SQL queries embedded deep in the C# code, I’m using sqlite-net (GitHub repo here) as the data access layer over SQLite. It’s very lightweight and provides a thin layer of abstraction. My business objects are decorated with attributes such as [PrimaryKey], [AutoIncrement], etc.

From the GitHub repository:

sqlite-net is an open source, minimal library to allow .NET and Mono applications to store data in SQLite 3 databases. It is written in C# and is meant to be simply compiled in with your projects. It was first designed to work with MonoTouch on the iPhone, but has grown up to work on all the platforms (Mono for Android, .NET, Silverlight, WP7, WinRT, Azure, etc.).

sqlite-net was designed as a quick and convenient database layer. Its design follows from these goals:

  • Very easy to integrate with existing projects and with MonoTouch projects.
  • Thin wrapper over SQLite and should be fast and efficient. (The library should not be the performance bottleneck of your queries.)
  • Very simple methods for executing CRUD operations and queries safely (using parameters) and for retrieving the results of those query in a strongly typed fashion.
  • Works with your data model without forcing you to change your classes. (Contains a small reflection-driven ORM layer.)
  • 0 dependencies aside from a compiled form of the sqlite2 library.

What I like: easy to setup, easy to use, no additional dependencies

SQLCompact (Windows Phone) vs SQLite (Windows 8)

The challenge I had here is that the Windows Phone version of 2Day relies on SQLCompact: back in the Windows Phone 7 days, this was the only option and I hadn’t the chance to upgrade so far to SQLite…

SQLCompact has a nice “automatic change tracking” feature: when you turn this on, your objects are tracked automatically by the data access layer (using the INotifyPropertyChanging interface). When you call Save() on the database, it knows exactly what objects must be updated. Pretty handy isn’t it ?

In the SQLite world, I created a small piece of code to mimicthis feature. The implementation is straightforward: I register change notification callsbacks (from INotifyPropertyChanged interface) and push updated objects in a list. When I want to save the changes, I just go over this list and send each update in the database.

Crash handler (and the invisible stack trace)

When 2Day has an unhanded exception, I try to achieve 3 goals:

  • mitigate the effect of the exception (decide whether the execution can continue or not)
  • inform the user “something” went wrong
  • log the maximum amount of information so that I can then fix the crash later on

The 8.1 update of WinRT introduced the ability to setup crash handler in a WinRT XAML app:

?View Code CSHARP
1
2
3
4
5
Application.Current.UnhandledException += this.OnUnhandledException;
 
private void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
}

 However, this technique has an important limitation: most of the time the stack trace will be null or empty (apparently due to the interop between the native and managed world). That’s a big deal because when the exception will be logged it will not have the stack trace. Good luck with that to debug a NullReferenceException occurring somewhere in your app…

The Windows Store Dev Center also some quality diagnostic tool. Basically, crashes of your app are sent automatically to Microsoft. You can read more about this here (picture from the blog post):

devcenter-crash-report

GridView and drag/drop support

The main page in 2Day make heavy use of the GridView control. Here are some highlights about its usage:

  • Basic options
    • IsItemClickEnabled = true
    • SelectionMode = multiple
    • CanDragItems = true
  • Items panel
    • ItemsWrapGrid to have pixel-based UI virtualization

I wrote a “DragDropManager” tool that listens to drag events on the GridView and provide this nice drag/drop experience to organize your tasks:

The collection databound to the GridView is a custom collection I was already using on the Windows Phone version. It manages groups and tasks with user defined sort options.

Message box and the “Access Denied” exception

The first versions of 2Day for Windows 8 had a bug that took me a while to fix. The exception message was “Access is denied”. I first thought it was related to I/O operation but it turns out it was related to the use of the message box !

Basically, you will get an “Access is denied” exception if you try to open a message box while one is already open. That sound silly to do that but I can assure you this kind happens once the app get bigger… and display message box in case of unhandled exception (as it was my case).

A nice blog post by fellow French Microsoft evangelist Eric Vernie provides a workaround here (in French). The idea is to use a SemaphoreSlim structure that we await when we try to display a message box. If 2 (or more) message boxes are shown, they will be shown one after the other.

Live tiles (and how to make it simple)

 I’m using the great Live Tile library that is available in the Windows 8 App tiles and badge sample. It makes very easy to consume a specific tile template in a type safe manner (instead of building a raw XML string).

For example in order to have this live tile:

livetile

Which require this XML:

1
2
3
4
5
6
7
8
9
10
<tile>
  <visual version="2">
    <binding template="TileSquare150x150Text01">
      <text id="1">Text Field 1 (larger text)</text>
      <text id="2">Text Field 2</text>
      <text id="3">Text Field 3</text>
      <text id="4">Text Field 4</text>
    </binding>  
  </visual>
</tile>

You can just write in C#:

?View Code CSHARP
1
2
3
4
5
var mediumTile = TileContentFactory.CreateTileSquare150x150Text01();
mediumTile.TextHeading.Text = "heading"
mediumTile.TextBody1.Text = "title1";
mediumTile.TextBody2.Text = "title2";
mediumTile.TextBody3.Text = "title3";

Cool, isn’t it ?

Code sharing with Windows Phone

No surprise here, code sharing with the Windows Phone version is achieved through Portable Class Libraries and Linked Files. However, because of the historical choice of SQLCompact in the Windows Phone version, the data model is not shared (but implemented twice).

Of course the whole XAML UI is implemented twice. In a certain way, that does make sense when we see how different the UI are on both platforms:

2day-win8-vs-wp8

Synchronization (using a Node.js powered backend running on Azure!)

2Day for Windows 8 introduced a new synchronization option for the users of the app: 2Day Cloud. The backend powering this new sync system is Azure Mobile Services (AMS).

ams

I’m using the following services of AMS:

  • Authentication with Microsoft Account
  • Single sign on, for Windows 8 device. You don’t have to even type your email or password
  • Data, to store tasks, folders and other useful information
  • Scripts, using a JavaScript (NodeJS) backend. I’m using this to filter each read query on the database to fetch only the items owned by specific users, storing update dates, etc.
  • Scheduler, to remove old items from the database in order to optimize its size

Both the Windows 8 and the Windows Phone version uses the C# Azure Mobile Services SDK, with 100% code sharing between the two (using linked files).

Miscellaneous control

Other controls or libraries used in 2Day for Windows 8:

  • My simple AppBarHint control (GitHub repo)
  • Callisto, a library built by Microsoft XAML team member Tim Heuer, used in 2Day to have flying popups (GitHub repo)
  • MarkedUp as the analytics engine in the Windows 8 version. The tool is free for up to 10, 000 unique users (see pricing details here) and extremely simple to add in the app:
?View Code CSHARP
1
MarkedUp.AnalyticClient.Initialize(WinConstants.MarkedUpId);

The Windows Phone version of 2Day uses Flurry but it will be soon updated to MarkedUp as well for coherence reason.

I hope you enjoyed this technical overview of 2Day for Windows 8. Please feel free to write a comment to share your thoughts or are questions about specific technical implementation details :-)

WinRT: introducing the AppBarHint control

Windows 8.1 is has a lot of improvements in the OS, for XAML and HTML developers but also changes some paradigms introduced with the release of Windows  8. One of them is to make more obvious UI elements that were hidden behind gestures users could miss.

If you take a look at the music app for example you will notice that the search bar is now always visible (in the top left corner):

xbox music 8.1

Whereas in the original version it was accessible only from the search charm (thus harder to find):

xbox music 8.0

In the mail app, many actions were hidden in the bottom application bar and the 8.1 version now displays a visual indicator that there is something hidden here:

app bar

With a bottom bar with “…” in the bottom-right corner:

app bar hint

In the upcoming version of 2Day for Windows 8, I decided to follow this path and I built a simple control I called the AppBarHint control for this exact purpose. The code is more than trivial but I thought it worth a blog post more because of the idea of using it rather than the actual code.

2day appbar hint 12day appbar hint 2

The control is actually just a Button with a custom template applied to it. The source code is available on GitHub. Here is the template applied to the Button:

1
*

Enjoy,

VS2013: upgrading a Windows Phone 7/8 and Windows 8 apps

In this post I will showcase issues I had while upgrading a Visual Studio 2012 solution containing a Windows Phone app (with both 7.8 and 8.0 versions) and Windows Store app to Visual Studio 2013.

As you probably already know, the Release Candidate version of VS2013 was released a few days ago and I decided it was the appropriate time to upgrade my 2Day todo-list application. Because 2Day for Windows will target the 8.1 release (coming October 18th, now available for MSDN subscribers) I needed to perform this upgrade before the release.

Windows Phone 7.x support

The first issue is simple and yet sad: Visual Studio 2013 is dropping support for Windows Phone 7.x app.  I guess it makes sense when given the fact the WP8 devices represent more than 65% of the total of Windows Phone device (see this AdDuplex blog post for more details).I can also confirm this trend that by looking at 2Day’s data for last week:

2Day devices chart

Since I released the first version of 2Day I submitted 14 updates. Starting from 2Day 1.4 I  released all updates for both WP7 and WP8 devices. It looks like the 2.1 update coming later this month will be the last one targeting WP7 devices.

Windows Phone support Conclusion: upgrade to VS2013 imply no more WP7 support. You can try to maintain 2 solutions but we will see in the next paragraph that there are other issues with Portable Class Libraries.

Portable Class Libraries (PCL)

2Day’s Visual Studio 2012 solution contains Portable Class Libraries to enable code reuse between Windows Phone and Windows 8 (it was also helping code sharing between WP7 and WP8 versions). In Visual Studio 2013, like we saw before, support for WP7 is now longer available, including for PCLs:

VS2013 PCL

So opening a solution that contains PCLs targeting WP7 will upgrade those PCLs and drop targeting of WP7.

Also, in order to target WP7 and have async/await support I had to use the Async BCL NuGet package:

BLC Async Support

Because the async support is now supported in the frameworks I target (Windows 8.1 and Windows Phone 8), I must remove those packages in order to prevent conflicts like the following:

Error 28 The type 'System.Threading.Tasks.Task<TResult>' exists in both 'c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETPortable\v4.0\Profile\Profile158\mscorlib.dll' and '...\packages\Microsoft.Bcl.1.0.19\lib\portable-net40+sl4+win8+wp71\System.Threading.Tasks.dll'

While doing those upgrades I also ended up target .Net 4.5, and it turns out the reflection API has some changes in the latest version of .Net.  If you’re like me you’re a fan of SOLID principles, you probably have some kind of Inversion of Control container using reflection… In my case the following code was not working anymore:

var constructor = typeof(T).GetConstructors().FirstOrDefault();

And I had to use the new reflection API:

var constructor = typeof(T).GetTypeInfo().DeclaredConstructors.FirstOrDefault();

You can read this complete post on the blog of the .Net team for more details about the evolution of the reflection API.

PCL Conclusion: be careful when you upgrade PCL projects to VS2013. Because WP7 support is gone, you don’t need the Async BCL NuGet package anymore. If you choose to target .Net 4.5 beware of possible breaking changes in the reflection API.

General conclusion: if like me your original plan was to target WP7, WP8 and Windows 8.1 withing a single Visual Studio solution, you’re in trouble. My decision is to drop support for WP7 devices for the next update of my app. I guess I could go with branching in TFS to keep compatibility but I don’t have time for that. Be also careful with PCLs and the new reflection API.