如果接口位于不同的程序集中,则SimpleIoc.Default.Register在IsInDesignModeStatic处失败 [英] SimpleIoc.Default.Register fails at IsInDesignModeStatic if the Interface is in different assembly
问题描述
在ViewModelLocator
类的静态构造函数中,如果接口在其他项目中,则SimpleIoc.Default.Register<T>();
在IsInDesignModeStatic
模式下将失败.结果,MainWindow.xaml
设计器在设计时为空.
In the static constructor of ViewModelLocator
class the SimpleIoc.Default.Register<T>();
fails at IsInDesignModeStatic
mode, if the Interface is in a different project. As a result the MainWindow.xaml
designer is empty at design time.
我已经提出了一个简单的解决方案来对其进行测试.我所做的唯一更改是将DataItem
类和IDataService
界面移到Domain项目中.
I've made a simple solution to test it. Only changes that I've made is to move the DataItem
class and the IDataService
interface into the Domain project.
我找到了一种解决方法:在ClientWpf项目中将链接添加到IDataService.cs.
I've found a walk-around: Add a link to the IDataService.cs in the ClientWpf project.
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>();
}
这篇关于如果接口位于不同的程序集中,则SimpleIoc.Default.Register在IsInDesignModeStatic处失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!