2013-07-16 1 views
0

나는 작업중인 Windows 7 전화 응용 프로그램이 있습니다. 인터페이스가있는 몇 가지 서비스 클래스를 만들었지 만, 매번이 뷰를 탐색하려고 할 때마다 현재 충돌합니다.MVVM Light IoC가 바인딩되지 않습니까? 내가 뭘 놓치고 있니?

I 설치 (WMAppManifest.xml을 통해)를 즉시 에뮬레이터로드 이러한보기 중 하나를로드 할 수 내 프로젝트

내가 가진이

public interface IGpsService 
    { 
     void StartGps(); 
     GeoPosition<GeoCoordinate> CurrentPostion(); 
    } 


public class GpsService : IGpsService 
{ 
    private GeoCoordinateWatcher gpsWatcher; 

    public GpsService() 
    { 
     gpsWatcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default) 
     { 
      MovementThreshold = 20, 
     }; 

    } 

    public void StartGps() 
    { 
     gpsWatcher.Start(); 
    } 

    public GeoPosition<GeoCoordinate> CurrentPostion() 
    { 
     return gpsWatcher.Position; 
    } 

} 

같은 내보기 모델 로케이터

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

     if (ViewModelBase.IsInDesignModeStatic) 
     { 
      SimpleIoc.Default.Register<IGpsService, Design.GpsDataService>(); 
     } 
     else 
     { 
      SimpleIoc.Default.Register<IGpsService, GpsService>(); 
     } 
     SimpleIoc.Default.Register<AddProductPriceVm>(); 
    } 

// AddProductPrice.xaml.cs

public AddProductPrice(IGpsService gpsService) 
    { 
     InitializeComponent(); 
    } 

IOC는보기 모델에만 연결되어 있습니까? 그게 내 코드 뒤에있는 것처럼 작동하지 않는 이유야?

뒤에서 코드를 사용하는 것이 훨씬 쉽기 때문에 뒤에서 코드와 MVVM을 혼합하여 사용하고 있습니다. NavigationFailed

// Code to execute if a navigation fails 
    private void RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e) 
    { 
     if (System.Diagnostics.Debugger.IsAttached) 
     { 
      // A navigation has failed; break into the debugger 
      System.Diagnostics.Debugger.Break(); 
     } 
    } 
+0

오류 메시지가 무엇입니까? –

+0

Opps는 그것을 게시하는 것을 잊었습니다. 지금 해. – chobo2

답변

0

에서

오류 메시지

MissingMethodException 
    at System.Activator.InternalCreateInstance(Type type, Boolean nonPublic, StackCrawlMark& stackMark) 
    at System.Activator.CreateInstance(Type type) 
    at System.Windows.Navigation.PageResourceContentLoader.BeginLoad_OnUIThread(AsyncCallback userCallback, PageResourceContentLoaderAsyncResult result) 
    at System.Windows.Navigation.PageResourceContentLoader.<>c__DisplayClass4.<BeginLoad>b__0(Object args) 
    at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark) 
    at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark) 
    at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) 
    at System.Delegate.DynamicInvokeOne(Object[] args) 
    at System.MulticastDelegate.DynamicInvokeImpl(Object[] args) 
    at System.Delegate.DynamicInvoke(Object[] args) 
    at System.Windows.Threading.DispatcherOperation.Invoke() 
    at System.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority priority) 
    at System.Windows.Threading.Dispatcher.OnInvoke(Object context) 
    at System.Windows.Hosting.CallbackCookie.Invoke(Object[] args) 
    at System.Windows.Hosting.DelegateWrapper.InternalInvoke(Object[] args) 
    at System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr pHandle, Int32 nParamCount, ScriptParam[] pParams, ScriptParam& pResult) 

다이스 당신은보기가 아닌 뷰 모델에 직접 서비스를 주입하고 있습니다. 보기가 SimpleIoc을 사용하여 생성되지 않으므로 생성자에서 IGpsService 참조를 해결할 위치를 알지 못합니다.

이 경우 IGpsService를 뷰 모델에 삽입하여 속성으로 노출하는 것이 가장 좋습니다. DataContextChanged 이벤트를 뷰에 추가하고 발생하면 viewmodel에서 IGpsService를 가져옵니다.

편집 : 문제는 정말 아니다

// AddProductPrice보기

<UserControl 
DataContext="{StaticResource Locator.AddProductPriceVm}"> 

/AddProductPrice.xaml.cs

public AddProductPrice() 
    { 
     InitializeComponent(); 
     DataContextChanged+=DataContextChanged 
    } 
     void DataContextChanged(object sender, DependencyPropertyChangedEventArgs e) 
     { 
      var context=sender as AddProductPriceVm; 
      if(context!=null) 
       _myGpsService=context.GpsService; 
     } 

// AddProductPriceVm

public class AddProductPriceVm 
{ 
public AddProductPriceVm(IGpsService gpsService) 
{ 
    GpsService=gpsService; 
} 
public IGpsService GpsService{get;set;} 
} 

DI 문제 MVVM Light의 작동 방식입니다. 그것은 permforms 및 의존성 주입 전에보기가있을 것으로 기대합니다. 보기에 물건을 직접 주입하려면 프리즘을 사용하면됩니다 (그러나 더 많은 스캐 폴딩을 사용하면 훨씬 더 무거워집니다).

+0

예제를 제공해 주시겠습니까? 또한 모든 Ioc가 이와 동일한 문제점을 겪고 있습니까? – chobo2

+0

hmm 그래서 Nnject로 전환하면 아무 것도 해결되지 않습니다. MVVM 라이트가 설계된 방법. 네가 보여준 것이 그때에 서비스를 전달하는 좋은 방법이라고 생각해? 여러 VM에 동일한 서비스가 필요한 경우 모두 동일한 인스턴스를 공유하거나 서로 다른 인스턴스를 갖고 있다고합시다. – chobo2

관련 문제