我正在尝试将使用StructureMap的IOC的WPF示例转换为使用AutoFac的Silverlight
这被证明是非常困难的
我已经定义了一个静态BootStrapper类
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()
{
}
} 这是在App.xaml.cs的Application_Startup中初始化的
private void Application_Startup(object sender, StartupEventArgs e)
{
BootStrapper.Build();
this.RootVisual = new MainPage();
}我已将其中一个视图的DataContext设置为使用我的BootStrapper
DataContext="{Binding Path=FlexContractStructureViewModel,
Source={StaticResource classes:BootStrapper}}" 但是我得到错误:找不到具有名称/关键字类的资源:BootStrapper
我正在使用状态对要添加的App.xaml进行更改
但我不能这样做,因为ObjectDataProvider不被识别
我已经尝试了下面的等价物,但没有成功
<bs:BootStrapper xmlns:bs="clr-namespace:SLDashboard2.Classes" x:Key="BootStrapper"/>我认为这可能与BootStrapper静态有关?但是我不想不断地创建新的容器
有人能帮帮忙吗?
保罗
发布于 2012-08-27 04:33:26
不对。你不应该在你的IoC中注册你所有的ViewModels吗?然后将它们注入到构造函数中。它们永远不应该是静态的,我通常不会在视图中使用静态资源作为我的数据上下文
https://stackoverflow.com/questions/12124961
复制相似问题