giovedì 22 agosto 2013

Windows Store Apps Succinctly - Free eBook

Some days ago I started to develop an application for Windows Store.
I love to develop in C#, and after some pages in MSDN documentation, I searched for an offline free e-book to read with my tablet.
I found a great resource from Syncfusion:

Windows Store Apps Succinctly

This book is written by John Garland and you can download 185 pages in PDF or Kindle format.

Before starting, please remember: if you are a beginner in development, this ebook is not for you.

Why is it a good choice? Because it's simple and fast to read. It starts from core concepts until you get to the deployment. Then at the end of this book, you are ready to fully develop your first application.

Main Chapters
  1. Core Concepts: the introduction to Windows Store apps, WinRT and the Windows Runtime with a simple "Hello World" sample.
  2. XAML, Controls, and Pages: all you need to know about XAML, from Namespace declarations, to Animations and Data Binding. The chapter shows all essential controls for the user interface and explains how to work with Pages and Frames.
  3. Application Life Cycle and Storage: one of the most important chapters. The Windows Store apps life cycle is not the same as the desktop apps, then you need to understand all steps for the the best user experience. This chapter continues with Data Storage. It explains how to work with Application Data (local, roaming, temporary), User Data (file and folder picker) and Data Storage (files/folder and some useful links to LiveConnect and SQLite). 
  4. Contracts and Extensions: the Windows 8 Charms: search, share, print, app settings, etc.. The chapter ends with handling file types and protocols.
  5. Tiles, Toast, and Notifications: yes..the big feature of Windows Phone, now in Windows 8. If you want to release a great app, you really need a good Live Tile. This chapter covers all types of Tiles and how to schedule the updates. Then it talks about the Toast Notifications and ends with a sample of Push Notification.
  6. Hardware and Sensors: an overview of all sensors (like compass, gyroscope, accelerometer and gps) and how to interact with Camera. The user love those features, then use them in your app.
  7. Deployment: your application is done, let's publish it on Windows Store! Understand the Store prices and accounts; learn how to use the Windows Application Certification Kit; add the trial mode and the In-app purchase; configure the PubCenter and the Ads.

Pro
  • Free! :)
  • Easy and fast to read
  • XAML + C#
  • All development cycle
  • A lot of samples and tips
  • Kindle format
  • Not for development beginners

Cons
  • No NFC samples
  • No gestures samples

Now enjoy your reading... thanks Syncfusion.

FYI another good free resource is Metro Studio: a customizable collection of icon templates.

mercoledì 21 agosto 2013

Restore NuGet Packages


To restore NuGet packages you need only few steps:

1- In Visual Studio Options --> Package Manager -> Check "Allow NuGet to download missing packages during build".


2- In the Solution --> Enable NuGet Package Restore.


3- Build your project.

That's all!

giovedì 15 agosto 2013

String format in Xaml

When you need to concatenate some strings in a TextBlock you can use the tag like Run.

Silverlight 4 has introduced an useful feature to make this more easy: StringFormat.
Now you can put more informations in a single element.

The code is very simple:
<TextBlock Text="{Binding Username, StringFormat='Hello \{0\}'}" />
<TextBlock Text="{Binding Temperature, StringFormat=\{0\}°}" />

and it works also with numbers and dates:
<TextBlock Text="{Binding Value, StringFormat=\{0:n2\}}" />
<TextBlock Text="{Binding Value, StringFormat=\{0:c2\}}" />
<TextBlock Text="{Binding Date, StringFormat=f}" />

You can check more details in the Kunal's blog

sabato 29 giugno 2013

Windows Phone 8 - Map and Clusters

This code example demonstrates how to dynamically group pushpins in the map control.
There is a lot of code for Windows Phone 7, then I merged all what I need to create a project for WP8.


First of all you need some namespace declaration: for map control and for pushpins from WP Toolkit.

xmlns:map="clr-namespace:Microsoft.Phone.Maps.Controls;assembly=Microsoft.Phone.Maps"
xmlns:maptk="clr-namespace:Microsoft.Phone.Maps.Toolkit;assembly=Microsoft.Phone.Controls.Toolkit"

You need also two templates: one for a standard pushpin and the other for the cluster.
<phone:PhoneApplicationPage.Resources>
 <DataTemplate x:Key="PushpinTemplate">
  <maptk:Pushpin GeoCoordinate="{Binding GeoCoordinate}" Content="{Binding}" />
 </DataTemplate>
 <DataTemplate x:Key="ClusterTemplate">
  <maptk:Pushpin GeoCoordinate="{Binding GeoCoordinate}" Content="{Binding Count}"/>
 </DataTemplate>
</phone:PhoneApplicationPage.Resources>

ClustersGenerator is the core of the project. It's a static class that accepts in input
  • Map control
  • Pushpins collection
  • Cluster DataTemplate.
public ClustersGenerator(Map map, List<Pushpin> pushpins, DataTemplate clusterTemplate)
{
 _map = map;
 _pushpins = pushpins;
 this.ClusterTemplate = clusterTemplate;

 // maps event
 _map.ResolveCompleted += (s, e) => GeneratePushpins();

  // first generate
 GeneratePushpins();
}

Every map event launches the pushpins elaboration, but first to explain GeneratePushpins method, let's introduce another class: PushpinGroup.
PushpinGroup represents a standard pushpin or a cluster, and exposes a GetElement method to return them. If the group is a cluster, it needs to get only the first pushpin GeoCoordinate and the content is a group of all pushpins.
public class PushpinsGroup
{
 private List<Pushpin> _pushpins = new List<Pushpin>();
 public Point MapLocation { get; set; }

 public PushpinsGroup(Pushpin pushpin, Point location)
 {
  _pushpins.Add(pushpin);
  MapLocation = location;
 }

 public FrameworkElement GetElement(DataTemplate clusterTemplate)
 {
  if (_pushpins.Count == 1)
   return _pushpins[0];

  // more pushpins
  return new Pushpin()
  {
   // just need the first coordinate
   GeoCoordinate = _pushpins.First().GeoCoordinate,
   Content = _pushpins.Select(p => p.DataContext).ToList(),
   ContentTemplate = clusterTemplate,
  };
 }

 public void IncludeGroup(PushpinsGroup group)
 {
  foreach (var pin in group._pushpins)
   _pushpins.Add(pin);
 }
}

The GeneratePushipins function creates clusters based on map ViewPort and a constant named MAXDISTANCE. An extension method convert pushpin GeoCoordinate to a ViewPort Point. That is used to get the distance from other points. If this distance is less then the MAXDISTANCE, the pushpin become a part of cluster.
private void GeneratePushpins()
{
 List<PushpinsGroup> pushpinsToAdd = new List<PushpinsGroup>();
 foreach (var pushpin in _pushpins)
 {
  bool addGroup = true;
  var newGroup = new PushpinsGroup(pushpin, _map.ConvertGeoCoordinateToViewportPoint(pushpin.GeoCoordinate));

  foreach (var pushpinToAdd in pushpinsToAdd)
  {
   double distance = pushpinToAdd.MapLocation.GetDistanceTo(newGroup.MapLocation);

   if (distance < MAXDISTANCE)
   {
    pushpinToAdd.IncludeGroup(newGroup);
    addGroup = false;
    break;
   }
  }

  if (addGroup)
   pushpinsToAdd.Add(newGroup);
 }

 _map.Dispatcher.BeginInvoke(() =>
 {
  _map.Layers.Clear();
  MapLayer layer = new MapLayer();
  foreach (var visibleGroup in pushpinsToAdd.Where(p => _map.IsVisiblePoint(p.MapLocation)))
  {
   var cluster = visibleGroup.GetElement(this.ClusterTemplate) as Pushpin;
   if (cluster != null)
   {
    layer.Add(new MapOverlay() { GeoCoordinate = cluster.GeoCoordinate, Content = cluster.Content, ContentTemplate = cluster.ContentTemplate});
   }
  }
  if (layer.Count > 0)
   _map.Layers.Add(layer);
 });
}

The extension method GetDistanceTo is the algorithm to calculate the distance between two points:
public static double GetDistanceTo(this Point p1, Point p2)
{
 return Math.Sqrt((p1.X - p2.X) * (p1.X - p2.X) + (p1.Y - p2.Y) * (p1.Y - p2.Y));
}

Instead IsPointVisible returns true if the point is visible in the map, otherwise false:
public static bool IsVisiblePoint(this Map map, Point point)
{
 return point.X > 0 && point.X < map.ActualWidth && point.Y > 0 && point.Y < map.ActualHeight;
}

Now in your MainPage.xaml, you only need to pass all pushpins to the ClusterGenerator and it will do all work for you.

var clusterer = new ClustersGenerator(map, pushpins, this.Resources["ClusterTemplate"] as DataTemplate);

You can download all code here.

With this article I won TechNet Guru Contribution June 2013 - Windows Phone.

venerdì 28 giugno 2013

Windows Phone - Caliburn Micro and App.xaml error

When I add Caliburn Micro to a new project, I have always some error in App.xaml.
With Caliburn 1.5.1 I found a new problem: "Object Reference not set to an instance of an object".

This error don't prevent the project build, but it seems related to the xaml.
That's why the RootFrame into designer is null.

The fix is easy! Just open your Bootstrapper and change
container.RegisterPhoneServices(RootFrame);
with..
if (!Execute.InDesignMode)
 container.RegisterPhoneServices(RootFrame);