You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调试MVVMLight中CommonServiceLocator.ActivationException:导航服务注入异常

Troubleshooting NavigationService Injection in MVVMLight (VB.NET Conversion)

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 concrete NavigationService instead of the interface, which won't match your ViewModel's constructor dependency)
  • Botching the Lambda syntax (VB.NET uses Function() ... End Function instead 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 of SimpleIoc.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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 04:18:42