27 August 2014

Simple workaround for “Design view is unavailable for x64 and ARM target platforms”

When you are developing for Windows Phone or Windows and you are faced with using a native component (for instance the Bing Maps control on Windows, or the SensorCore api on Windows Phone) you will loose your designers when you are testing, both in Visual Studio and in Blend, as they will only work for “AnyCPU”  or “x86” configurations.This is highly annoying, but there is a kind-of fix.

The trick is to create an extra configuration. I usually make a copy of “Debug”, which I call “DebugArm” I do this by right clicking on the solution, selecting “properties”, and then hit “Configuration Manager”

image

Then I make a copy of Debug, by selecting “New” in the “Configuration” drop down, and I make a copy of Debug, called DebugArm.

image

In that solution I set everything to ARM:

image

So far so good, and nothing new. Now select the Debug project configuration again in Visual Studio, hit “Save all” and compile. Then open the solution in Blend. You will notice of course the designers now work, but now you cannot deploy. Changing that configuration every time again is slow and cumbersome, and it won’t be picked up by Blend. So you have to restart that time and time again. This now, my friends, we will use to our advantage.

Go back to Visual Studio, select “DebugArm”. You can now deploy and test again, but your designers will of course go dead again. But like I said Blend does not pick up that configuration change. Apparently that is only read at startup. So that will keep the Debug configuration and the designer still will work! So now you can use Blend to do design changes, and Visual Studio to actually run and deploy, using two different configuration settings, without having to change al the time!

This switchemarole you have to do every time you start Visual Studio and Blend. Granted, it’s not ideal, but a lot less of a hassle than constantly having to change configurations, which is is slow, while now you only have to select a different Icon in the Task Bar. And this is how it should be done most of the time IMHO – Blend is for the UI, Visual Studio for code.

No code this time, as this is a tools-trick only post.

13 August 2014

Querying the Windows Phone 8.1 map when there are child objects over it

With the Windows Phone 8.1 SDK came (yet again) an improved Map Control. This brought, among other things, the MapIcon. In ye olde days, the map could only draw lines and polygons natively – when you had to draw icons you had to add child objects to the map itself, which were drawn by the UI thread and not by the map. This is slower and eats more resources.

imageThe new map still offers this possibility. And sometimes we need it, too, for a couple of reasons. First of all, a MapIcon can only display an image and an optional label. Second, MapIcons are drawn on the map using a ‘best effort’, which mean overlapping icons don’t get displayed at all and – worse – if you query the map, using the FindMapElementsAtOffset method, they are not found either.

So in some cases we just need to resort to drawing XAML elements by adding to the map’s Children collection – an option, which luckily has been improved tremendously as we now, for instance, can data bind these elements using the MapItemsControl, as explained by my smart fellow MVP Shawn Kendrot. Before 8.1, we needed the Windows Phone Toolkit for that.

But I noticed something very odd. I have been trained to use the MapTapped event to trap the location where the user tapped, as the Tapped event is being trapped by the map itself. It never fires. So I used MapTapped, and then FindMapElementsAtOffset to query the map;

But to my dismay I also discovered that the MapTapped event does not fire either when you tap on a location where a child object is displayed. Ergo – if you tap on the cross where the Windows Phone logo is displayed, nothing happens. So how am I to find out what’s underneath there?

After some trashing around I stumbled upon the answer – if you tap on a child element that’s on the map – not the MapTapped, but the Tapped event fires. “The event that never fires” comes to the rescue. In addition, the Tapped event on the child object itself fires. So I created the following method to query the map:

private void QueryMap(Point offset)
{
  foreach (var obj in MyMap.FindMapElementsAtOffset(offset))
  {
    Debug.WriteLine(obj.GetType());
  };
}
The regular method to trap MapTapped:
private void MyMap_OnMapTapped(MapControl sender, MapInputEventArgs args)
{
  Debug.WriteLine("MyMap.MapTapped occurred");
  QueryMap(args.Position);
}

And a method to trap Tapped when that occurs, and that also routes to the QueryMap method, with a little help from the GetPosition event method:

private void MyMap_OnTapped(object sender, TappedRoutedEventArgs e)
{
  Debug.WriteLine("MayMap.Tapped occurred");
  QueryMap(e.GetPosition(MyMap));
}

And because the events never fire simultaneously, you will see the map finds one PolyLine when you tap on a line just next to the symbol, will and it fill find two lines (the crossing of both) when you tap on the symbol- but never will you get a double hit from both events.

By attaching to the Tapped event of the Child object I can also detect if the symbol itself is tapped:

var childObj = new Image { 
  Source = new BitmapImage(new Uri("ms-appx:///Assets/wplogo.png")) };
MapControl.SetNormalizedAnchorPoint(childObj, new Point(0.5, 0.5));
MapControl.SetLocation(childObj, p5);
MyMap.Children.Add(childObj);
childObj.Tapped += ChildObj_Tapped;



void ChildObj_Tapped(object sender, TappedRoutedEventArgs e)
{
  Debug.WriteLine("ChildObj.Tapped occurred");
}

And thus we can find everything on the map, provided you attach the right methods to the right events. The MapTapped/Tapped combo of the map is an odd one but once you know how to do it – a few extra lines of code does it all.

A demo app demonstrating the whole concept can be found here.The only thing it does is write things to the debug console when you do something, so pay attention what happens there ;-)

06 August 2014

AngularJS + TypeScript: how to implement validation

Preface

AngularJs supports all kinds of simple client-side validation. The simplest is data-ng-required which basically says a UI element must be filled in before the form is valid. I am picking up where I left off in my previous post – I am going to make the the phone input screen have validation.

Adapting the view

First of all we are going to tweak the view a little. This is just standard AnguarJS stuff, nothing TypeScript specific here yet:

<div>
  <div>
    Phones
    <div data-ng-repeat="phone in listOfPhones">
      <div>Name: {{phone.name}}; screen size: {{phone.screenSize}}"</div>
    </div>
  </div>

  <button data-ng-click="phoneController.doLoadPhones()">Load phones from server</button>
  <br /><br />
  Add a new phone<br />
  <div data-ng-form="phoneform" class="css-form">
    <label for="name" style="width:40px">Name</label>
    <input id="name" type="text" data-ng-model="newPhone.name"
           data-ng-required="true" /><br />
    <label for="name" style="width:40px">Size</label>
    <input id="screensize" type="text" data-ng-model="newPhone.screenSize"
           data-ng-required="true" /><br />
    <button data-ng-click="phoneController.doSavePhone()"
            data-ng-disabled="!phoneController.canSave()">
      Save
    </button>
  </div>
</div>

I have underlined and written in red what is new:

  • A wrapper div with a form name and a css class
  • data-ng-required attribute, one on each field
  • A data-ng-disabled attribute on the button that will enable/disable it when the input is not valid.

Adapting the scope

Just like anormal AngularJS (based on JavaScript), the scope will now be extended with a property named “phoneform” – for that is how we called the form. So we need to adapt the IPhoneScope like this:

/// <reference path="../models/iphone.ts" />
module App.Scope {
    "use strict";

    export interface IPhoneScope {
        listOfPhones: Models.IPhone[];
        newPhone: Models.IPhone;
        phoneform: ng.IFormController
    }
}

And once again we now have the advantage of a typed object.

Adapting the controller

This is also very simple: we need to create the canSave method. So in the PhoneController we add:

public canSave(): boolean {
    return this.$scope.phoneform.$valid;
}

And that is basically most of what we need to do.

Adding some feedback css

Now to have some visual feedback to see if things are valid or not, we use (again) a standard AngularJS trick. In the view we have added the class “css-form” to the form. So we open the Site.css file in the content folder and add the following css:

.css-form input.ng-invalid.ng-dirty {
    background-color: #FA787E;
}

.css-form input.ng-valid.ng-dirty {
    background-color: #78FA89;
}

Testing the validation

If you run the app (after emptying your cache) you will notice you cannot click the save button initially:

image

If you enter phone name, the field will become greenish. If you type something in the screen size field and then delete it again, the field will become reddish, and the button is still disabled.

image

Only when both fields are filled in the save button is enabled

image

An interesting way to mess up validation – or a bug?

According to numerous samples I saw, you don’t need to write data-ng-required=”true”. Just entering data-ng-required (or plain ng-required) should work as well. It does – your field is invalid when it is empty. Trouble is, if you enter data and then delete it again, the field stays valid – and the form too, and you can hit the save button with one empty field. I have no idea if that is a bug in the version of AnguarJS I use, or if this recently has been changed – but please make sure you write data-ng-required=”true”

Demo solution can be found here

05 August 2014

AngularJS + TypeScript – using $resource to talk to a WebApi backend

Intro – multiple stacks to choose from

There are a number of ways to talk to a backend (i.e. communicate over JSON services) in Angular. I know at least of two built-in: the $http api, which basically gives you a bit raw, jQuery-like API, and the $resource API, which kind of builts on top of that, using a more formalized approach. Also, you can choose to forego the whole built-in stack and use something like Breezejs which has it own pros and cons. I used both successfully. And I don’t doubt there are more. But in this article I will concentrate on $resource.

Once again, I don’t claim ultimate wisdom nor do I claim this is the best way, but I describe a way that works and is complete.

Setting up the backend

I continue where I left off in my previous post, and proceed to add a model class in the Models folder. Mind you, this the folder in the root, not that in the app folder with TypeScript stuff in it. We are talking C#. Our application will be extended to maintain a list of phones.

namespace AtsDemo.Models
{
  public class Phone
  {
    public long Id { get; set; }

    public string Name { get; set; }

    public double ScreenSize { get; set; }
  }
}

imageThen add a WebApi controller in the Controllers folder. I right-clicked the Controllers folder, clicked “Add/Controller”, selected “Web API 2 Controller with read/write actions” and called it – very originally – “PhonesController”. I implemented only the Get and the Post method, to prove the communication actually works:

using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using AtsDemo.Models;

namespace AtsDemo.Controllers
{
  public class PhonesController : ApiController
  {
    private static List<Phone> phoneDb;

    private List<Phone> PhoneDb
    {
      get
      {
        if (phoneDb == null)
        {
          phoneDb = new List<Phone>
          {
            new Phone {Id = 1, Name = "Lumia 1020", ScreenSize = 4.5},
            new Phone {Id = 2, Name = "Lumia 1520", ScreenSize = 6.0},
            new Phone {Id = 3, Name = "Lumia 625",  ScreenSize = 4.7},
            new Phone {Id = 3, Name = "Lumia 930",  ScreenSize = 5.0}
          };
        }

        return phoneDb;
      }
    }
    // GET: api/Phones
    public IEnumerable<Phone> Get()
    {
      return PhoneDb;
    }

    // POST: api/Phones
    public void Post([FromBody]Phone value)
    {
      value.Id = PhoneDb.Max(p => p.Id) + 1;
      PhoneDb.Add(value);
    }
  }
}

As you can see this implements an ‘in memory database’ ;-) to list and store phones. To make sure WebApi honors the JavaScript convention of using camel casing, the following code needs to be added at the end of the Register method of the WebApi class in App_Start:

var jsonFormatter = 
  config.Formatters.OfType<JsonMediaTypeFormatter>().First();
jsonFormatter.SerializerSettings.ContractResolver = 
  new CamelCasePropertyNamesContractResolver();
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(
  new MediaTypeHeaderValue("text/html"));
The last statement is actually only to make WebApi default output JSON – this is not actually necessary, but it makes testing WebApi a bit easier. To get this to work, three extra usings will be necessary:
using System.Net.Http.Formatting;
using Newtonsoft.Json.Serialization;
using System.Net.Http.Headers;

If your run the project and then change the url in http://localhost:3987/api/phones you get the following data:

[{"id":1,"name":"Lumia 1020","screenSize":4.5},{"id":2,"name":"Lumia 1520","screenSize":6.0},{"id":3,"name":"Lumia 625","screenSize":4.7},{T"id":3,"name":"Lumia 930","screenSize":5.0}]

This is easiest in Chrome, as it has no qualms about displaying raw JSON in the browser. In Internet Explorer it works as well, but IE insist on saving the file to disk, refusing it to display for security reasons. The backend now works.

Creating model and interface

If you have installed the Web Essentials 2013 for Update 2 as I recommended in the first part of this series you can now do something fun, although with such a little model not very useful yet:

  • Right-click Phone.cs
  • Select Web Essentials (all the way to the top)
  • The select “Create TypeScript Intellisense file. This will generate a file “Phone.cs.d.ts” with the following contents:
declare module server {
    interface Phone {
        id: number;
        name: string;
        screenSize: number;
    }
}
Which gives a nice skeleton that we can use. I don't like client side models sitting next to C# code, so I create a folder "models" in my "app" folder and moved this file over to that location (for some reason that does not work in the Solution Explorer; I used the Windows Explorer to do that). I addition, I renamed the file to Phone.cs, made a class of it and put the class inside the App.Models namespace, so the net result is:
module App.Models {
    "use strict"; 
     export class Phone {
        id: number;
        name: string;
        screenSize: number;
    }
}
Now this is where is gets a little complicated and it had me reeling some time. If you want to use a resource to get models from and to a backend, you will need to define a resource on an interface, not on a class. According to this Stack Overflow example – the only one I have been able to find, although it is parroted on several other places (up to and including the errors that were – and still are – in it) – we need two interfaces like this:
module App.Models {
    export interface IPhone extends ng.resource.IResource<IPhone> {
        id: number;
        name: string;
        screenSize: number;
    }

    export interface IPhoneResource extends
       ng.resource.IResourceClass<IPhone> {
    }
}

This works fine – as long as you never need a concrete implementation of IPhone. But if you do, and you make your Phone class implement IPhone like this:

 export class Phone implements Models.IPhone

You will be rewarded with a compilation error.

image

Simply because an IPhone now not only needs to implement it’s three properties but also the whole kit and caboodle that comes with being a resource. 

What I came up with was to define actually three interfaces. First, we define a simple interface for the object itself:

module App.Models {
    "use strict"; 
     export interface IPhone {
         id: number;
         name: string;
         screenSize: number;
    }
}

Then we define a simple interface that only extends a resource on IPhone. I define these in the “resources” folder (and namespace) as I like to keep related stuff together. First, I define a resource definition interface

/// <reference path="../models/iphone.ts" />
 module App.Resources {
    "use strict"; 
     export interface IPhoneResourceDef
     extends ng.resource.IResource<Models.IPhone> {
     }
 }
And then we define the Resource Class on the resource definition
/// <reference path="iphoneresourcedef.ts" />
module App.Resources {
    "use strict"; 
    export interface IPhoneResource
    extends ng.resource.IResourceClass<Resources.IPhoneResourceDef> {
    }
}

Now you can still reference to an IPhone object and have a concrete implementation of it and have a resource. As you can see, having type safety comes at a price. Remember that interfaces in JavaScript don’t exist and that this code won’t translate to any JavaScript code. All you are basically doing is providing scaffolding for type checking at compile time. Pure JavaScript fans will say they can write a lot more with a lot less code. I think they are right. I assume rock star programmers with an IQ of 200 can also go a lot faster if they don’t write unit tests for .NET code as well. The rest of us, the mere mortals, need all the help we can get.

Adding a resource builder

Now this may also seem like a lot of overkill, but believe me – when you start adding more and more resources to your app, you will se a lot of repetitive code in your AppBuilder. Me se not like that, to quote a beloved ;-) Star Wars character. So I came up with this Factory to build services for me:

module App.Factories {
    "use strict"; 
    export class ResourceBuilder{

        static $inject = ['$resource'];

        constructor(private $resource: ng.resource.IResourceService) {
        }

        public getPhoneResource(): Resources.IPhoneResource {
            return  this.$resource('/api/phones/:id', { id: '@id' }, { 
            });
        }
    }
} 

So then we mosey over to the AppBuilder.ts to add this factory and the resource we are building with it:

this.app.factory("ResourceBuilder", ['$resource',
    ($resource) => new Factories.ResourceBuilder($resource)]);

this.app.factory("PhoneResource",
    ["ResourceBuilder",
        (builder: App.Factories.ResourceBuilder) => builder.getPhoneResource()]);

This is added just below the declaration of the Angular modules, and just before the only controller we currently have.

Testing the setup

Basically we are done now – the only thing is to add code to show that all this actually works. So here we go again, as explained in the first post:

  • You add a new scope interface
  • You add a new controller
  • You add a view
  • You add the controller to the app and update the router.

Scope

For a quick demo, I am going to put some objects directly on the scope, which you normally would not do to prevent issues with sub scopes:

/// <reference path="../models/iphone.ts" />
module App.Scope {
    "use strict";

    export interface IPhoneScope {
        listOfPhones: Models.IPhone[];
        newPhone: Models.IPhone
    }
}

Add a new controller

/// <reference path="../models/iphone.ts" />
/// <reference path="../models/phone.ts" />
/// <reference path="../resources/iphoneresource.ts" />
/// <reference path="../scope/iphonescope.ts" />
/// <reference path="../models/phone.ts" />

module App.Controllers {
    "use strict";
    export class PhoneController {
        constructor(private $scope: Scope.IPhoneScope, 
                    private phoneResource: Resources.IPhoneResource) {
            $scope.listOfPhones = [];
            $scope.newPhone = new Models.Phone();
        }

        public doLoadPhones(): void {
            this.phoneResource.query({}, (d: Models.IPhone[]) => this.onLoaded(d));
        }

        private onLoaded(d: Models.IPhone[]): void {
            this.$scope.listOfPhones = d;
        }
         
        public doSavePhone(): void {
            this.phoneResource.save(this.$scope.newPhone,
                () => this.saveCallback(),
                () => { alert('failure'); });
        }

        private saveCallback() {
            alert('success');
            this.$scope.newPhone = new Models.Phone();
            this.doLoadPhones();
        }
    }
}

Not really rocket science here – a method to load phones from the server using the resource “query” method, and a method to save a phone using the resource “save” method. By inheriting ng.resource.IResourceClass we got all this methods for free, in stead of that we had to implement that all by ourselves using the $http stack.

Interlude - ways to mess up TypeScript callbacks
"I have not failed. I've just found 10,000 ways that won't work" - Thomas A. Edison

Sometimes you make typos, or honest mistakes. TypeScript, although being very powerful, has, like every other programming language, it’s own unique set of particular devious pitfalls that sometimes makes you wonder if someone made them on purpose. Observe way the callback is called:

public doSavePhone(): void {
    this.phoneResource.save(this.$scope.newPhone,
        () => this.saveCallback(),
        () => { alert('failure'); });
}

This works as intended. Now the fun thing is, if you just forget the parentheses at the end, e.g.

public doSavePhone(): void {
    this.phoneResource.save(this.$scope.newPhone,
        () => this.saveCallback,
        () => { alert('failure'); });
}

This will compile flawlessly. Only - the callback never gets called. Even more fun is this one:

public doSavePhone(): void {
    this.phoneResource.save(this.$scope.newPhone,
        this.saveCallback,
        () => { alert('failure'); });
}

This will compile, and saveCallBack will even be called. Only you will notice you have lost the "this" inside saveCallback. Ain’t life fun sometimes? Please take care when making callbacks. Don't fall into the same traps I did.

Add a new view

Then we add a new view phonesView.html in views:

<div>
  <div>
    Phones
    <div data-ng-repeat="phone in listOfPhones">
      <div>Name: {{phone.name}}; screen size: {{phone.screenSize}}"</div>
    </div>
  </div>

  <button data-ng-click="phoneController.doLoadPhones()">Load phones from server</button>
  <br /><br />
  Add a new phone<br />
  <label for="name" style="width:40px">Name</label>
  <input id="name" type="text" data-ng-model="newPhone.name" /><br />
  <label for="name" style="width:40px">Size</label>
  <input id="screensize" type="text" data-ng-model="newPhone.screenSize" /><br />
  <button data-ng-click="phoneController.doSavePhone()">Save</button>
</div>

Nothing special here, a div to show loaded fields and two text boxes to input a new one.

Update the AppBuilder with the new controller and route

First, we add the new controller. Nothing that we haven't seen before.
this.app.controller("phoneController", [
    "$scope", "PhoneResource", 
    ($scope: Scope.IPhoneScope, phoneResource: Resources.IPhoneResource)
        => new App.Controllers.PhoneController($scope, phoneResource )
]);
Then we add the new route to the controller, and make it the new default route. Also nothing new
this.app.config([
    "$routeProvider",
    ($routeProvider: ng.route.IRouteProvider) => { 
        $routeProvider
            .when("/person",
            {
                controller: "personController",
                controllerAs: "myController",
                templateUrl: "app/views/personView.html"
            })
            .when("/phones",
            {
                controller: "phoneController",
                controllerAs: "phoneController",
                templateUrl: "app/views/phonesView.html"
            })
            .otherwise({
                redirectTo: "/phones"
            });
    }
]);

Running the test

If you start the web application, you will initially see this:

image

Then, if you hit “Load phones from server”, it will show:

image

Then suppose we add the 530:

image

and hit save, we will see the “success” alert pop up, and finally,

image

Success at last.

Conclusion

This took me quite some time to figure out, and I am not one to sing my own praise but this may just be the only complete – and running – sample of using Angularjs resources with TypeScript. Most of what I wrote here is out there somewhere on the web, in bits and pieces, and sometimes partially wrong. Anyway. This works. Running sample solution, as always, can be downloaded here.

Now my only hope is that I won’t get sued over violating some copyright rules by defining a TypeScript interface whose name resembles a popular brand of phones made by a company whose name resembles a common fruit ;-)