0
votes

I try to use dependency injection to inject service into view model. try this code

ViewModelLocator

public class ViewModelLocator
    {
        public static MainViewModel _main;

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


            SimpleIoc.Default.Register<IAccountService, AccountService>();

            SimpleIoc.Default.Register<MainViewModel>();
            SimpleIoc.Default.Register<LoginViewModel>();
        }

        /// <summary>
        /// Gets the Main property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public IMainViewModel Main
        {
            get
            {
                return ServiceLocator.Current.GetInstance<IMainViewModel>();
            }
        }

        /// <summary>
        /// Gets the Main property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public LoginViewModel Login
        {
            get
            {
                return ServiceLocator.Current.GetInstance<LoginViewModel>();
            }
        }

        /// <summary>
        /// Cleans up all the resources.
        /// </summary>
        public static void Cleanup()
        {

        }
    }

MainViewModel

public interface IMainViewModel
    {

    }
    public class MainViewModel : ViewModelBase
    {
        private readonly IAccountService _accountService;

        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel(IAccountService accountService)
        {
            _accountService = accountService;
        }
    }

my LoginViewModel

public interface ILoginViewModel
{
    ICommand Authorize { get; set; }
}
public class LoginViewModel : ViewModelBase, IMainViewModel
{

    private IAccountService _accountService;

    public LoginViewModel(IAccountService _accountService)
    {
        this._accountService = _accountService;
        Authorize = new RelayCommand(() => CheckAuthorized(), () => true);
    }

    public ICommand Authorize { get; private set; } 

}

and this my LoginView

public partial class Login : UserControl
{
    ILoginViewModel _loginViewModel;
    public Login(ILoginViewModel _loginViewModel)
    {
        this._loginViewModel = _loginViewModel;
        InitializeComponent();
        DataContext = _loginViewModel;
    }
}

my question is, when i try to inject in my View, the object reference is null, how can i use DI in mvvm light?

1
I'm confused; the title of your question implies that you're trying to inject AccountService into LoginViewModel, but your comment at the end implies that you're trying to put it into LoginView. Which is it?Jason Baker
oh sorry my fault, i mean LoginViewModelnovian kristianto

1 Answers

2
votes

You are trying to inject ILoginViewModel into the Login view but seems like ILoginViewModel is not registered so you may need to register the same

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


        SimpleIoc.Default.Register<IAccountService, AccountService>();

        SimpleIoc.Default.Register<IMainViewModel, MainViewModel>(); //register view models
        SimpleIoc.Default.Register<ILoginViewModel, LoginViewModel>();
    }

thanks @Jason for pointing this out

perhaps by mistake LoginViewModel is implementing IMainViewModel instead of ILoginViewModel

so need to be corrected this way

public class LoginViewModel : ViewModelBase, ILoginViewModel
{
    ...
}

also implemtent IMainViewModel in MainViewModel to make it resolve via the container

public class MainViewModel : ViewModelBase, IMainViewModel 
{
    ...
}