Telerik UI for Windows Phone by Progress

The Telerik Data Storage ships with the installation of the Telerik UI for Windows Phone suite. This article will explain how to add the Data Storage reference to your application and then, how to use it to build data-oriented applications.

Installing the Data Storage

To install the Telerik Data Storage, you have to download the Telerik UI for Windows Phone installator from you account on the Telerik website.

Referencing the Data Storage in Your Project

To add the Telerik Data Storage to an app, do the following:

  1. Once the Telerik UI for Windows Phone suite is installed, you can open a new or existing project, right-click the References folder and select Add Reference from the context menu.

  2. In the Reference Manager dialog click on 'Browse' and navigate to the Telerik UI for Windows Phone installation folder. Locate the 'Binaries' folder inside, then go to 'WindowsPhone'. In this folder, besides all standard controls assemblies you will notice the 'ARM' and 'x86' folders. Since the Telerik Data Storage for Windows Phone uses a native (C++) implementation of its core layer, it needs to be built either against x86 or ARM. The x86 version is only intended for debugging purposes with the Windows Phone emulator. The release version of your application should be using the ARM build of the Data Storage component. You need to add a reference to the following assemblies in order to be able to use the Data Storage component:

    • Telerik.Storage_WP.winmd
    • Telerik.Storage.Extensions.dll

Using the Data Storage in your projects

Important

To deploy an application successfully on any machine, you must select the right Solution Platform since the Data Storage component uses native code that is CPU architecture dependent and must be rebuilt for all architectures (x86, ARM, x64). Since all Windows Phone devices are based on the ARM processor architecture you will need to build your Windows Phone application against it. That is done by opening the Visual Studio Configuration Manager and choosing the ARM platform instead of AnyCPU for your project.

The initial steps to use the Data Storage component are as follows:

  1. Create an object of type Context – this way a database file will be created if it doesn't exist, or will be opened for further CRUD operations. The context object keeps the internal state of data schema and data that should be persisted (deleted, inserted or changed). During the construction of this object you can specify the name and location of the database using the enumeration.

    Note

    To create and use the Context object include the following namespace: Telerik.Storage.Extensions.

    CopyC#
    public enum DatabaseLocation { Local = 0, Roaming, Temporary, Fullpath}
    var context = new Context("CarsDB");
  2. Define entity classes that will represent the objects in the database. For the example we will create a Car class and a CarOwner class.

    CopyC#
    public class CarOwners
    {
      [Key]
      public long OwnerID{ get; set; }
      public string Name { get; set; }
      public int Age { get; set; }
      public bool Alive { get; set; }
      public DateTime DateBorn { get; set; }
    }
    
    public class Cars
    {
      [Key]
      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public long CarID { get; set; }
      public string Model { get; set; }
      public DateTime YearOfModel { get; set; }
      public string RegistrationNumber { get; set; }
      public long OwnerFK { get; set; }
    }
  3. Call any member of the context object for CRUD operations. Here we will insert some entries in the database:

    CopyC#
    context.Insert<CarOwners>(new CarOwners() { OwnerID = 1, Name="Paul Grohe", Age=33,    Alive = true, DateBorn=new DateTime(1980,7,23), });
    context.Insert<CarOwners>(new CarOwners() { OwnerID = 2, Name = "Alex Claude", Age = 33, Alive = true, DateBorn = new DateTime(1980, 7, 23), });
    context.Insert<CarOwners>(new CarOwners() { OwnerID = 3, Name = "Stephen Dred", Age = 33, Alive = true, DateBorn = new DateTime(1980, 7, 23), });
    
    //Note: CarID primary key field is autogenerated incremental
    context.Insert<Cars>(new Cars() { Model = "BMW", RegistrationNumber = "ZD123DF", YearOfModel = new DateTime(2010, 1, 1), OwnerFK = 1 });
    context.Insert<Cars>(new Cars() { Model = "Lancia", RegistrationNumber = "WD4313FF", YearOfModel = new DateTime(2008, 1, 1), OwnerFK = 2 });
    context.Insert<Cars>(new Cars() { Model = "Audi", RegistrationNumber = "KD2233DD", YearOfModel = new DateTime(2011, 1, 1), OwnerFK = 3 });
    context.Insert<Cars>(new Cars() { Model = "Mercedes", RegistrationNumber = "PF123ML", YearOfModel = new DateTime(2012, 1, 1), OwnerFK = 1 });
    context.Insert<Cars>(new Cars() { Model = "Toyota", RegistrationNumber = "JD149KL", YearOfModel = new DateTime(2009, 1, 1), OwnerFK = 2 });
  4. Save (persist) changes in a database via the context object in order to persist the changed states in context to the database – this will update the records and the table schema.

    CopyC#
    context.SaveChanges();
  5. Close database before exit application or creation of new Context object.

    CopyC#
    context.CloseDatabase();

See Also