2015-01-20 4 views
4

MVVM Light를 사용하고 있습니다. 내 자원에 더 많은 가치 컨버터를 추가하면 내 응용 프로그램은 예외로 충돌 'System.InvalidOperationException'형식의 예외가 Microsoft.Practices.ServiceLocation.DLL에서 발생했지만 사용자 코드에서 처리되지 않은ServiceLocationProvider를 설정해야합니다.

추가 정보 : ServiceLocationProvider를 설정해야합니다. App.xaml.cs OnLaunched 이벤트에서

I이 선 I는 인수 ServiceLocatorProvider로 취하는 SetLocatorProvider 메소드가 볼 수있는 본 ServiceLocator에서

ServiceLocator.Current.GetInstance<MyViewModel>(); 
거기 충돌

.. 있다.

protected override async void OnLaunched(LaunchActivatedEventArgs e) 
    { 
     Frame rootFrame = Window.Current.Content as Frame; 

     if (rootFrame == null) 
     { 
      ... 
     } 

     if (rootFrame.Content == null) 
     { 
      ... 
     } 

     Window.Current.Activate(); 

     DispatcherHelper.Initialize(); 

     ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default); 

     ServiceLocator.Current.GetInstance<MyViewModel>(); 
    } 

편집이 : 여기에 전체 OnLaunched 이벤트 나 일자 웹 및 마이크로 소프트의 MSDN 페이지에서 아무것도 찾을 수 couldnt한다.

ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default); 

퍼팅 후의 예외 occures :

형 Microsoft.Practices.ServiceLocation.ActivationException '의 예외가 발생하지만 GalaSoft.MvvmLight.Extras.DLL 사용자 코드에 처리되지

추가 정보 : 캐시에 유형이 없습니다 : cMC.ViewModel.MyViewModel.

는 ViewModelLocator의 코드

public class ViewModelLocator 
{ 
    public ViewModelLocator() 
    { 
     ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default); 

     SimpleIoc.Default.Register<MyViewModel>(); 
    } 

    public MyViewModel MyVM 
    { 
     get 
     { 
      return ServiceLocator.Current.GetInstance<MyViewModel>(); 
     } 
    } 

    public static void Cleanup() {} 
} 
+1

:

public class ViewModelLocator { public ViewModelLocator() { } public static void SetAndReg() { ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default); SimpleIoc.Default.Register<MyViewModel>(); } public MyViewModel MyVM { get { return ServiceLocator.Current.GetInstance<MyViewModel>(); } } public static void Cleanup() {} } 

}는 App.xaml.cs를에서 그런

: 그래서 나는이 같은 ViewModelLocator 클래스를 수정 단일성 또는 ... ServiceLocator.SetLocatorProvider (() => new UnityServiceLocatorAdapter (container)) 메소드를 호출하여 공급자를 설정해야합니다. – Xeun

답변

1

당신은 LocationProvider (물론 오류 메시지는 매우 분명하다 ..) 설정되지 않습니다

을 당신은 ServiceLocator 선택의 IOC의 컨테이너를 줄 필요가 : 유니티를 사용하여이 예와 어댑터를 참조하십시오

static ViewModelLocator() 
    { 
     var container = new UnityContainer(); 
     ServiceLocator.SetLocatorProvider(() => new UnityServiceLocatorAdapter(container)); 

     container.RegisterInstance<ILoggingService>(new ConsoleLoggingService()); 
     container.RegisterInstance<IMessageBoxService>(new SimpleMessageBoxService()); 
     container.RegisterInstance<ITestSuiteService>(new TestSuiteService()); 
     container.RegisterInstance<IApplicationService>(new ApplicationService()); 
    } 

    /// <summary> 
    /// Gets the <see cref="BackstageAboutViewModel"/>. 
    /// </summary> 
    public BackstageAboutViewModel BackstageAboutViewModel 
    { 
     get 
     { 
      return ServiceLocator.Current.GetInstance<BackstageAboutViewModel>(); 
     } 
    } 
+0

잘 생성자 ViewModelLocator에서 라인 : "ServiceLocator.SetLocatorProvider (() => SimpleIoc.Default);". 이거 아닌가요? 이 코드를 App.cs의 OnLaunched 이벤트에 복사하면 MyViewModel이 캐시에서 발견되지 않는다는 예외가 발생합니다. –

+0

@ V.G. 보다 많은 코드를 제공해야합니다. 설정하지 않기를 기대했습니다. – Xeun

+0

이상하게도 4 개 이상의 값 변환기를 추가 할 때 문제가 발생합니다 .. –

4

내가 좀 그것을 알아 냈다.

또한 ViewModelLocator 생성자에서 발생하는 ViewModel을 등록 할 필요가 있지만 어떤 이유로 생성자가 나중에 실행됩니다. ServiceLocator처럼 사용할 수있는 IOC의 컨테이너에 대한 래퍼의 일종이기 때문에

...OnLaunched(...) 
{ 
... 
     DispatcherHelper.Initialize(); 

     ViewModelLocator.SetAndReg(); 

     ServiceLocator.Current.GetInstance<MyViewModel>(); 
... 
} 
+1

미안하지만 그 실수를 보지 못했다. SetAndReg 메서드를 사용하지 않고 정적 생성자를 사용하는 것이 좋습니다. 위의 예를 보면 App.xaml 에이 사실을 설명 할 수 있습니다. 당신의 컨트롤에 : – Xeun

+0

감사 뷰 모델 로케이터 생성자는 아직이다 만들 수 있습니다. 따라서 서비스 위치 지정자는 설정되지 않습니다. OnWindowCreated와 같은 다른 이벤트에서 원래 코드를 사용해보고 문제가 해결되는지 확인하십시오. 모든 시작 이벤트가이 링크를 확인하려면 https://msdn.microsoft.com/en-us/library/ie/windows.ui.xaml.application#events를 확인하십시오. –

+0

가능성이 무슨 일이 일어나고있는 조언을 의 DataContext = "{출처 = {정적 리소스 ViewModelLocator}, 경로 = TestButlerMainWindowViewModel 바인딩}"이것은 예정이다 가 될 "청소기" – joshwl2003