调试MVVMLight中CommonServiceLocator.ActivationException:导航服务注入异常
Hey there! It sounds like you're hitting a classic "works in C#, breaks in VB.NET" snag with MVVMLight's SimpleIoc—specifically getting the NavigationService injected into your ViewModels. Let's walk through the most common pitfalls when converting C# MVVMLight snippets to VB.NET and how to fix them:
1. Fix Your SimpleIoc Registration Syntax
VB.NET uses Of for generics instead of C#'s <> , and Lambda expressions have a distinct structure—this is where most conversion mistakes happen.
Correct VB.NET Registration Example:
' In your ViewModelLocator or app startup code Imports GalaSoft.MvvmLight Imports GalaSoft.MvvmLight.Ioc Imports GalaSoft.MvvmLight.Views ' First register the NavigationService (interface -> implementation) SimpleIoc.Default.Register(Of INavigationService)(Function() Dim navService As New NavigationService() ' Don't forget page mappings (critical for navigation functionality later) navService.Configure("DetailPage", GetType(DetailPage)) navService.Configure("HomePage", GetType(HomePage)) Return navService End Function) ' Then register your ViewModel (which depends on INavigationService) SimpleIoc.Default.Register(Of MainViewModel)()
Common mistakes here:
- Forgetting
Of INavigationService(registering the concreteNavigationServiceinstead of the interface, which won't match your ViewModel's constructor dependency) - Botching the Lambda syntax (VB.NET uses
Function() ... End Functioninstead of C#'s() => ...)
2. Verify Your ViewModel Constructor
Make sure your ViewModel's constructor is Public, accepts the INavigationService interface (not the concrete class), and you're letting SimpleIoc create the ViewModel (manual instantiation skips injection).
Correct ViewModel Constructor:
Imports GalaSoft.MvvmLight Imports GalaSoft.MvvmLight.Views Public Class MainViewModel Inherits ViewModelBase Private ReadOnly _navigationService As INavigationService ' Public constructor with INavigationService parameter Public Sub New(navService As INavigationService) _navigationService = navService End Sub ' Example command using the injected service Public ReadOnly Property NavigateToDetailCommand As ICommand Get Return New RelayCommand(Sub() _navigationService.NavigateTo("DetailPage")) End Get End Property End Class
Common mistake:
- Creating the ViewModel with
New MainViewModel()instead ofSimpleIoc.Default.GetInstance(Of MainViewModel)()—manual instantiation bypasses the IoC container entirely.
3. Check Registration Order
SimpleIoc resolves dependencies when a type is first requested. If you register your ViewModel before the NavigationService, it'll try to create the ViewModel without finding the required dependency and throw an error. Always register dependencies first, then the types that depend on them.
4. Confirm Namespace References
Double-check that you're referencing GalaSoft.MvvmLight.Views.INavigationService in both your registration code and ViewModels. Accidentally using a custom or mismatched INavigationService interface will break injection matching.
If you've gone through all these steps and still have issues, try enabling SimpleIoc's debug logging (if available) to pinpoint exactly where the resolution fails—it might reveal a subtle conversion quirk we missed!
内容的提问来源于stack exchange,提问作者Maf

