c#-如果接口位于不同的程序集中,则SimpleIoc.Default.Register在IsInDesignModeStatic处失败
作者:互联网
在ViewModelLocator类的静态构造函数中,SimpleIoc.Default.Register< T>();如果接口在其他项目中,则在IsInDesignModeStatic模式下失败.结果,MainWindow.xaml设计器在设计时为空.
我提出了一个简单的解决方案进行测试.我所做的唯一更改是将DataItem类和IDataService接口移到Domain项目中.
我找到了一种解决方法:在ClientWpf项目中添加指向IDataService.cs的链接.
public class ViewModelLocator {
static ViewModelLocator() {
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
if (ViewModelBase.IsInDesignModeStatic) {
// It fails if the IDataService is in different assembly
// Delete the link of IDataService.cs from the ViewModel folder...
SimpleIoc.Default.Register<IDataService, Design.DesignDataService>();
}
else {
SimpleIoc.Default.Register<IDataService, DataService>();
}
SimpleIoc.Default.Register<MainViewModel>();
}
...
}
解决方法:
if (ViewModelBase.IsInDesignModeStatic) {
// put these lines here:
if (SimpleIoc.Default.IsRegistered<IDataService>()) {
SimpleIoc.Default.Unregister<IDataService>();
}
SimpleIoc.Default.Register<IDataService, Design.DesignDataService>();
}
else {
SimpleIoc.Default.Register<IDataService, DataService>();
}
标签:mvvm-light,c 来源: https://codeday.me/bug/20191031/1977752.html