MVVM Light IoC没有绑定?我错过了什么?

时间:2013-07-16 16:19:28

标签: windows-phone-7 mvvm inversion-of-control mvvm-light

我正在使用Windows 7手机应用程序。我用接口制作了几个服务类,但每次我尝试浏览这些视图时它们现在都崩溃了。

我设置我的项目,一旦模拟器加载(通过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是否仅绑定到View Models或其他东西?这就是为什么它不能正常工作,因为我的代码背后了吗?

我正在使用混合的代码和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)

在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();
        }
    }

1 个答案:

答案 0 :(得分:0)

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

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

编辑:

// AddProductPrice View

<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和依赖注入之前存在。如果你想直接将东西注入到视图中,那么你可以看一下使用Prism(但是它有更多的脚手架更重)。