Visualizzazione post con etichetta Caliburn. Mostra tutti i post
Visualizzazione post con etichetta Caliburn. Mostra tutti i post

venerdì 28 giugno 2013

Windows Phone - Caliburn Micro and App.xaml error

When I add Caliburn Micro to a new project, I have always some error in App.xaml.
With Caliburn 1.5.1 I found a new problem: "Object Reference not set to an instance of an object".

This error don't prevent the project build, but it seems related to the xaml.
That's why the RootFrame into designer is null.

The fix is easy! Just open your Bootstrapper and change
container.RegisterPhoneServices(RootFrame);
with..
if (!Execute.InDesignMode)
 container.RegisterPhoneServices(RootFrame);

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);
 }
}