Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
345 views
in Technique[技术] by (71.8m points)

wpf - how to inject service to view Model in mvvm light

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 
{
    ...
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...