在UWP中将IUnityContainer与Singleton类一起使用

时间:2018-09-24 07:46:24

标签: c# .net uwp

目前,我正在使用Prism进行UWP项目。我在使用DI和IUnityContainer遇到了一些问题。我想在我的UILogic项目中的任何位置在IUnityContainer中调用我的服务,而无需在ViewModel构造函数上声明它。我应该使用单例类来保存容器并调用它,而不是在构造函数中声明吗? 这是我的单例课程

 public class WebServiceHandler
{
    private static WebServiceHandler _instance;
    private static object syncRoot = new Object();
    private IUnityContainer myContainer;
    public IErrorMessageService ErrorMessageService;
    public IEventAggregator EventAggregator;
    public IRunInformationService RunInformationService;
    public static WebServiceHandler Instance
    {
        get
        {
            if (_instance == null)
            {
                lock (syncRoot)
                {
                    if (_instance == null)
                    {
                        _instance = new WebServiceHandler();
                    }
                }
            }
            return _instance;
        }

    }
    private WebServiceHandler()
    {
        try
        {
            myContainer = (UnityContainer)Application.Current.Resources["MyContainer"];
            ErrorMessageService = myContainer.Resolve<IErrorMessageService>();
            EventAggregator = myContainer.Resolve<IEventAggregator>();
            RunInformationService = myContainer.Resolve<IRunInformationService>();
        }
        catch { }
    }

}

0 个答案:

没有答案