HTML5 with JavaScript and CSS3 on my Windows Phone

From some time now I have been thinking about starting to program in HTML5 with JavaScript and CSS3. I must admit that until now I have not found enough time or motivation to do so.

Yesterday, while reading one of the articles on John Sonmez’s blog (link here) I was reminded about Jennifer Dewalt. She has decided to learn how to code by creating 180 websites in 180 days (link here), with no days off. She is quite a source of inspiration: instead of thinking over and over how to start doing something one should JFDI.

I have decided to learn how to program in HTML5 with JavaScript and CSS3 by creating a set of small Windows Phone applications. Since I have a full time job I will not be able to work each day on this personal project, but I hope every week I will do something new.

 Create a “Hello World” Windows Phone Application

Download and install Visual Studio Express 2013 for Windows from here. I have downloaded and installed the DVD5 ISO Image.

After installing Visual Studio Express 2013 for Windows and starting it, you will be prompted to sign in with your Microsoft account (you could create one if you do not have it).

WP_VS_StartUp

In order to crate a new Windows Phone App go to FILE -> New Project…, from Installed->Templates->JavaScript choose Blank App (Windows Phone) and click OK.

WP_VS_Project

Based on the template we have chosen, Visual Studio will create the following structure:

WP_VS_Project_Created

 

In default.html add the following text to the body:

<body class="phone">
 <p>Hello World! (from Luck)</p>
</body>

Compile the solution by pressing F6.

To run the solution there are two options:

1. Use the Windows Phone Emulator installed with Visual Studio

From Visual Studio just use one of the emulators and click run or F5.WP_VS_Emulator

2. Use a physical device in case you have one (I use my Nokia Lumia for this)

The first step is to connect your Windows Phone to the computer via an USB Cable. The phone has to be unlocked.

From the start screen of the computer, run Windows Phone Developer Registration 8.1:

WP_Register_Phone

Follow all the registration steps. At the end of registration you should get the following message: Congratulations! You have successfully unlocked your Windows Phone.

Back to Visual Studio, on the Run menu select Device and hit run or F5.

 

At this point the first Windows Phone 8.1 application should have been created and executed. This is the result:

 

WP_VS_Run

Advertisements

Serialization – Part 1 – Binary Serializer

Serialization is the process of transforming an in-memory object or an object graph into a stream of bytes or text. Deserialization is the opposite process of transforming a stream of bytes or text into an in-memory object or object graph.

.Net framework offers three serialization “engines”:

  • Binary serializer
  • Data contract serializer
  • XML serializer

Binary serializer

In order to create serializable types using binary serializer engine there are two options:

  • annotate the type with [SerializableAttribute] from namespace System
  • implement the ISerializabe interface from System.Runtime.Serialization namespace (this should be done when more control is required over serialization process, for example when one need to encrypt/decrypt a field of the object that is serialized/deserialized)

The binary serializer is fast, but its drawback is that it is tightly coupled withe the internal structure of the types. The binary serializer is primarily designed to produce binary data, but .Net offers formatters to produce XML as well.

.Net Remoting uses the binary serialization engine.

In the example below three types (Persons, Person and Address) are create and annotated with [SerializableAttribute]. A single instance of Address is created and it will be assigned to two different Person-s. An interesting aspect of the binary serialization is that the referential equality of the Address object will be preserved after serialization and deserialization:

using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
 
namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            var address = new Address
            {
                City = "Timisoara",
            };
 
            var persons = new Persons
            {
                M = new Person
                {
                    Name = "John",
                    Address = address,
                },
                F = new Person
                {
                    Name = "Jane",
                    Address = address,
                }
            };
 
            TestBinaryFormatter(persons);
        }
 
        static void TestBinaryFormatter(Persons persons)
        {
            Print("Before Serialization:", persons);
 
            IFormatter formatter = new BinaryFormatter();
            using (Stream stream = new MemoryStream())
            {
                formatter.Serialize(stream, persons);
                stream.Position = 0;
                Persons newPersons = (Persons)formatter.Deserialize(stream);
                Print("After Serialization: ", newPersons);
            }
        }
 
        static string ReferenceEquals(Address a1, Address a2)
        {
            return object.ReferenceEquals(a1, a2) ?
                "[== References to address objects are equal]" :
                "[!= References to address objects are NOT equal]";
        }
 
        static void Print(string message, Persons persons)
        {
            Console.WriteLine("{0} {1} {2}", 
                message,
                persons, 
                ReferenceEquals(persons.M.Address, persons.F.Address));
        }
    }
 
    [Serializable]
    public class Persons
    {
        public Person M { get; set; }
        public Person F { get; set; }
 
        public override string ToString()
        {
            return string.Format("[{0}] & [{1}]", M, F);
        }
    }
 
    [Serializable]
    public class Person
    {
        public string Name { get; set; }
        public Address Address { get; set; }
 
        public override string ToString()
        {
            return string.Format("{0} - {1}", Name, Address);
        }
    }
 
    [Serializable]
    public class Address
    {
        public string City { get; set; }
 
        public override string ToString()
        {
            return string.Format("{0}", City);
        }
    }
}

 

The output is:

Before Serialization: [John - Timisoara] & [Jane - Timisoara] [== References to address objects are equal]
After Serialization: [John - Timisoara] & [Jane - Timisoara] [== References to address objects are equal]
Press any key to continue . . .