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
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