如果接口位于不同的程序集中,则SimpleIoc.Default.Register在IsInDesignModeStatic处失败 [英] SimpleIoc.Default.Register fails at IsInDesignModeStatic if the Interface is in different assembly

查看:356
本文介绍了如果接口位于不同的程序集中,则SimpleIoc.Default.Register在IsInDesignModeStatic处失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆