C# 如何使用 MVVMLight SimpleIoc?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13795596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:49:05  来源:igfitidea点击:

how to use MVVMLight SimpleIoc?

c#wpfinversion-of-controlmvvm-lightwindows-store-apps

提问by Youngjae

I'm revamping my software which has messy Messenger.Default(...)bits.

我正在改进我的软件,它有一些乱七八糟的东西Messenger.Default(...)

Is there any cheat sheet to know MVVMLight SimpleIoc usage (not general IoC description)?

是否有任何备忘单可以了解 MVVMLight SimpleIoc 的用法(不是一般的 IoC 描述)?

采纳答案by Faster Solutions

SimpleIoc crib sheet:

SimpleIoc 婴儿床单:

1) You register all your interfaces and objects in the ViewModelLocator

1)您在 ViewModelLocator 中注册所有接口和对象

class ViewModelLocator 
{ 
    static ViewModelLocator() 
    {         
        ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);          
        if (ViewModelBase.IsInDesignModeStatic) 
        {              
            SimpleIoc.Default.Register<IDataService, Design.DesignDataService>();          
        }          
        else         
        {              
            SimpleIoc.Default.Register<IDataService, DataService>();          
        }          
        SimpleIoc.Default.Register<MainViewModel>();                  
        SimpleIoc.Default.Register<SecondViewModel>(); 
    }      


    public MainViewModel Main 
    {  
        get  
        {      
            return ServiceLocator.Current.GetInstance<MainViewModel>();  
        } 
    }
} 

2) Every object is a singleton by default. To resolve an object so that it's not a singleton you need to pass a unique value to the GetInstance call:

2) 默认情况下,每个对象都是单例。要解析对象使其不是单例,您需要将唯一值传递给 GetInstance 调用:

SimpleIoc.Default.GetInstance<MainViewModel>(Guid.NewGuid().ToString());

3) To register a class against an interface:

3)针对接口注册一个类:

SimpleIoc.Default.Register<IDataService, Design.DesignDataService>();  

4) To register a concrete object against an interface:

4) 要针对接口注册具体对象:

SimpleIoc.Default.Register<IDataService>(myObject);     

5) To register a concrete type:

5) 注册一个具体类型:

SimpleIoc.Default.Register<MainViewModel>();   

6) To resolve an object from an interface:

6) 从接口解析对象:

SimpleIoc.Default.GetInstance<IDataService>();

7) To resolve an object directly (does buildup and dependency resolution):

7)直接解析一个对象(做构建和依赖解析):

SimpleIoc.Default.GetInstance<MainViewModel>();

8) MVVM makes doing design-time data really easy:

8) MVVM 使设计时数据变得非常容易:

if (ViewModelBase.IsInDesignModeStatic) 
{              
    SimpleIoc.Default.Register<IDataService, Design.DesignDataService>();          
}          
else         
{              
    SimpleIoc.Default.Register<IDataService, DataService>();          
}  

If you're in design-time mode it will automatically register your design-time services, making it really easy to have data in your viewmodels and views when working in the VS designer.

如果您处于设计时模式,它会自动注册您的设计时服务,这使得在 VS 设计器中工作时在您的视图模型和视图中拥有数据变得非常容易。

Hope this helps.

希望这可以帮助。