Paul
Paul

Reputation: 3283

Using AutoFac BootStrapper?

I am attempting to translate a WPF example of IOC using StructureMap into Silverlight using AutoFac

This is proving to be very difficult

I have got a static BootStrapper class defined

public class BootStrapper
{
  public static IContainer BaseContainer { get; private set; }

  public static FlexContractStructureViewModel FlexContractStructureViewModel()
  {
      return BaseContainer.Resolve<FlexContractStructureViewModel>();
  }

  public static void Build()
  {
      if (BaseContainer == null)
      {
          var builder = new ContainerBuilder();
          builder.RegisterAssemblyTypes();

          BaseContainer = builder.Build();
      }
  }

  static BootStrapper()
  {

  }

} 

This is initialised in the Application_Startup in App.xaml.cs

private void Application_Startup(object sender, StartupEventArgs e)
{
  BootStrapper.Build();

  this.RootVisual = new MainPage();
}

I have set the DataContext of one of my views to use my BootStrapper

DataContext="{Binding Path=FlexContractStructureViewModel, 
              Source={StaticResource classes:BootStrapper}}"          

But I get the error Cannot find a Resource with the Name/Key classes:BootStrapper

The book I am using states to make a change to the App.xaml to add

But I cant do that because ObjectDataProvider is not recognised

I have tried the equivalent below with no luck

<bs:BootStrapper xmlns:bs="clr-namespace:SLDashboard2.Classes" x:Key="BootStrapper"/>

I think this may be related to having BootStrapper static? But I dont want to be constantly creating new Containers

Can someone help please?

Paul

Upvotes: 0

Views: 727

Answers (1)

123 456 789 0
123 456 789 0

Reputation: 10865

Wrong. Shouldn't you be registering all your ViewModels in your IoC? and then you inject them to your constructors. They should never be static and I don't usually use static resources as my datacontext in my view

Upvotes: 1

Related Questions