Windows phone 7 MVVM灯光IoC未绑定?我错过了什么?

Windows phone 7 MVVM灯光IoC未绑定?我错过了什么?,windows-phone-7,mvvm,inversion-of-control,mvvm-light,Windows Phone 7,Mvvm,Inversion Of Control,Mvvm Light,我正在开发一个Windows7手机应用程序。我创建了几个带有接口的服务类,但每次我尝试浏览这些视图时,它们都会崩溃 我将项目设置为在emulator加载后立即加载其中一个视图(通过WMAppManifest.xml) 我有类似的东西 public interface IGpsService { void StartGps(); GeoPosition<GeoCoordinate> CurrentPostion(); } publi

我正在开发一个Windows7手机应用程序。我创建了几个带有接口的服务类,但每次我尝试浏览这些视图时,它们都会崩溃

我将项目设置为在emulator加载后立即加载其中一个视图(通过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;
    }

}
国际奥委会是否只绑定查看模型或其他内容?这就是为什么它不能像我的代码中那样工作的原因吗

我混合使用代码隐藏和MVVM,因为使用代码隐藏更容易

错误消息

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)

您将服务直接注入视图,而不是viewmodel。视图不是使用SimpleIoc创建的,因此不知道在构造函数中解析IGpsService引用的位置

如果您想这样做,最好将IGpsService注入到viewmodel中,并将其作为属性公开。将DataContextChanged事件添加到视图中,当它触发时,从viewmodel获取IGpsService

编辑:

//AddProductPrice视图

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

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

问题其实不是DI的问题,而是MVVM灯的工作方式。它希望视图在转换表单和依赖项注入之前就在那里。如果你想把东西直接注入到视图中,那么你可以考虑使用Prism(但是如果有更多的脚手架,它会更重)

错误信息是什么?Opps忘记发布了。现在完成。你能举个例子吗?所有的国际奥委会也会遇到同样的问题吗?嗯,所以改用Nnject并不能解决任何问题。MVVM灯是如何设计的。我猜你展示的是推荐的通过服务的方式?比如说,如果多个虚拟机需要相同的服务,它们是共享同一个实例还是拥有不同的实例?
   // 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();
        }
    }
<UserControl
DataContext="{StaticResource  Locator.AddProductPriceVm}">
public AddProductPrice()
    {
        InitializeComponent();
        DataContextChanged+=DataContextChanged
    }
        void DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            var context=sender as AddProductPriceVm;
            if(context!=null)
                _myGpsService=context.GpsService;
        }
public class AddProductPriceVm
{
public AddProductPriceVm(IGpsService gpsService)
{
   GpsService=gpsService;
}
public IGpsService GpsService{get;set;}
}