如何注入服务以在mvvm light中查看Model

时间:2014-06-06 02:56:54

标签: wpf mvvm mvvm-light

我尝试使用依赖注入将服务注入视图模型。试试这段代码

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;
        }
    }

我的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; } 

}

这是我的LoginView

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

我的问题是,当我尝试在我的View中注入时,对象引用为null, 我如何在mvvm灯中使用DI?

1 个答案:

答案 0 :(得分:2)

您正在尝试将ILoginViewModel注入Login视图,但似乎ILoginViewModel未注册,因此您可能需要注册相同的

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


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

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

感谢@Jason指出这个

可能是错误的LoginViewModel正在实现IMainViewModel而不是ILoginViewModel

因此需要以这种方式纠正

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

还在MainViewModel中实现IMainViewModel,使其通过容器解析

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