venerdì 28 giugno 2013

Windows Phone - Caliburn Micro and Telerik

When you use Caliburn Micro you always have a file Bootstrapper like this:

public class Bootstrapper : PhoneBootstrapper
{
 PhoneContainer container;

 protected override void Configure()
 {
  container = new PhoneContainer();
  
  container.RegisterPhoneServices(RootFrame);

  container.PerRequest<MainPageViewModel>();
  
  AddCustomConventions();
 }

 static void AddCustomConventions()
 {
 }

 protected override object GetInstance(Type service, string key)
 {
  return container.GetInstance(service, key);
 }

 protected override IEnumerable<object> GetAllInstances(Type service)
 {
  return container.GetAllInstances(service);
 }

 protected override void BuildUp(object instance)
 {
  container.BuildUp(instance);
 }
}

I love to use Windows Phone controls from Telerik and I always add RadPhoneApplicationFrame in my applications, but how to add it in the Bootstrapper?

You need only some lines of code.


private PhoneApplicationFrame rootFrame;
private new PhoneApplicationFrame RootFrame
{
 get
 {
  if (this.rootFrame == null)
   this.rootFrame = new RadPhoneApplicationFrame();

  return this.rootFrame;
 }
 set
 {
  this.rootFrame = value;
 }
}

protected override PhoneApplicationFrame CreatePhoneApplicationFrame()
{
 return this.RootFrame;
}
  

I created a public property to instantiate the RadPhoneApplicationFrame and overrided the CreatePhoneApplicationFrame to return the custom frame.

Here the new Bootstrapper:

public class Bootstrapper : PhoneBootstrapper
{
 PhoneContainer container;

 private PhoneApplicationFrame rootFrame;

 private new PhoneApplicationFrame RootFrame
 {
  get
  {
   if (this.rootFrame == null)
    this.rootFrame = new RadPhoneApplicationFrame();

   return this.rootFrame;
  }
  set
  {
   this.rootFrame = value;
  }
 }

 protected override void Configure()
 {
  container = new PhoneContainer();
  
  container.RegisterPhoneServices(RootFrame);

  container.PerRequest<MainPageViewModel>();
  
  AddCustomConventions();
 }

 protected override PhoneApplicationFrame CreatePhoneApplicationFrame()
 {
  return this.RootFrame;
 }

 static void AddCustomConventions()
 {
 }

 protected override object GetInstance(Type service, string key)
 {
  return container.GetInstance(service, key);
 }

 protected override IEnumerable<object> GetAllInstances(Type service)
 {
  return container.GetAllInstances(service);
 }

 protected override void BuildUp(object instance)
 {
  container.BuildUp(instance);
 }
}

Nessun commento:

Posta un commento