Reading Binary Tile Map Data on Windows Phone

Scott Walker · Sep 28, 2012

When building a tile‑mapping engine for Windows Phone, one of the first challenges is reading binary data from the device. Unlike full .NET Framework applications, Windows Phone does not support the System.Runtime.Serialization.Formatters.Binary namespace, which means traditional binary serialization is not available.

To work around this limitation, Windows Phone provides the TitleContainer class, which allows you to open streams from the application's Content folder. This makes it possible to load map files, textures, and other binary assets packaged with the application.

Unsupported Namespace

The following namespace cannot be used on Windows Phone:


using System.Runtime.Serialization.Formatters.Binary;

Because of this restriction, binary data must be read manually using BinaryReader.


Reading Map Data Using TitleContainer

The example below demonstrates how to load a binary map file from the Content/Maps directory and parse each tile into a MapSquare object.


try
{
    Stream path = TitleContainer.OpenStream("Content/Maps/" + map);

    using (BinaryReader br = new BinaryReader(path))
    {
        for (int x = 0; x < MapWidth; x++)
        {
            for (int y = 0; y < MapHeight; y++)
            {
                mapCells[x, y] = new MapSquare(
                    br.ReadInt32(),   // Background Layer
                    br.ReadInt32(),   // Interactive Layer
                    br.ReadInt32(),   // Foreground Layer
                    br.ReadString(),  // CodeValue
                    br.ReadBoolean()  // Passable
                );
            }
        }
    }
}
catch (Exception ex)
{
    // Handle errors (file missing, corrupt data, etc.)
}

Why This Works

  • TitleContainer.OpenStream() provides safe access to packaged content files.
  • BinaryReader allows manual parsing of integers, strings, and booleans.
  • This approach avoids unsupported serialization APIs on Windows Phone.

This pattern is essential for Windows Phone game development, especially when working with tile maps, level data, or any custom binary format.

C# Windows Phone

Comments (0)

Please sign in to comment.