반응형
MVVMLight SimpleIoc를 사용하는 방법?
나는 지저분한 Messenger.Default(...)
비트 가있는 소프트웨어를 개선하고있다 .
MVVMLight SimpleIoc 사용법을 알 수있는 치트 시트가 있습니까 (일반적인 IoC 설명이 아님)?
SimpleIoc 침대 시트 :
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) 모든 객체는 기본적으로 싱글 톤입니다. 싱글 톤이 아니도록 객체를 해결하려면 GetInstance 호출에 고유 한 값을 전달해야합니다.
SimpleIoc.Default.GetInstance<MainViewModel>(Guid.NewGuid().ToString());
3) 인터페이스에 대해 클래스를 등록하려면 :
SimpleIoc.Default.Register<IDataService, Design.DesignDataService>();
4) 인터페이스에 대해 구체적인 개체를 등록하려면 :
SimpleIoc.Default.Register<IDataService>(myObject);
5) 구체적인 유형을 등록하려면 :
SimpleIoc.Default.Register<MainViewModel>();
6) 인터페이스에서 객체를 해결하려면 :
SimpleIoc.Default.GetInstance<IDataService>();
7) 객체를 직접 해결하려면 (빌드 업 및 종속성 해결) :
SimpleIoc.Default.GetInstance<MainViewModel>();
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.
Hope this helps.
ReferenceURL : https://stackoverflow.com/questions/13795596/how-to-use-mvvmlight-simpleioc
반응형
'Program Club' 카테고리의 다른 글
개체를 XElement로 직렬화하고 메모리에서 역 직렬화합니다. (0) | 2021.01.10 |
---|---|
Google AdWords : 추적 전환 코드에 의해 추가 된 iframe 제거 (0) | 2021.01.10 |
ngpattern으로 자연 입력 숫자를 확인 (0) | 2021.01.10 |
매개 변수가있는 ZSH 별칭 (0) | 2021.01.10 |
Flutter에서 패키지 이름을 변경하는 방법은 무엇입니까? (0) | 2021.01.10 |