gpt4 book ai didi

c# - 如果接口(interface)位于不同的程序集中,则 SimpleIoc.Default.Register 在 IsInDesignModeStatic 处失败

转载 作者:行者123 更新时间:2023-11-30 15:36:58 24 4
gpt4 key购买 nike

ViewModelLocator的静态构造函数中类 SimpleIoc.Default.Register<T>();IsInDesignModeStatic 处失败模式,如果接口(interface)在不同的项目中。结果 MainWindow.xaml designer 在设计时是空的。

我做了一个简单的解决方案来测试它。我所做的唯一更改是移动 DataItem类和 IDataService Domain 项目的接口(interface)。

Download here

我找到了解决方法:在 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>();
}

关于c# - 如果接口(interface)位于不同的程序集中,则 SimpleIoc.Default.Register 在 IsInDesignModeStatic 处失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13175446/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com