Problem with exporting View-Model class

Dec 14, 2010 at 8:42 PM

I am newbie in MEF and I get this problem.

My shell-view model look like this:

namespace Spirit.ViewModels
{
    using Caliburn.Micro;
    using System.ComponentModel.Composition;

    public interface IShellViewModel
    {
        void ShowLogOnView();
        void ShowMessengerView();
    }

    [Export(typeof(IShellViewModel))]
    public class ShellViewModel : Conductor<IScreen>, IShellViewModel
    {
        public ShellViewModel()
        {
            ShowLogOnView();
        }

        public void ShowLogOnView()
        {
            ActivateItem(IoC.Get<LogOnViewModel>());
        }

        public void ShowMessengerView()
        {
            ActivateItem(IoC.Get<MessengerViewModel>());
        }
    }

}

It activate/deactivate new view-model for example this:

 

namespace Spirit.ViewModels
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.Composition;
    using System.Linq;
    using System.Windows.Input;
    using Caliburn.Micro;
    using Models;
    using Pokec_Toolkit_InterfaceAssembly;
    using System.Windows;


    public  interface  ILogOnViewModel
    {
        string Nick { get; set; }
        string Password { get; set; }
        void LogOn(string nick, string password);
    }

    [Export(typeof(ILogOnViewModel))]
    public class LogOnViewModel : Screen, IDataErrorInfo,ILogOnViewModel
    {

        #region interfaces

        [Import]
        private IShellViewModel _shellViewModel;

        [Import]
        private IPokecConnection _pokecConn;

        #endregion

        #region Implementation of ILogOnViewModel

        public string Nick
        {
            get { return _user.Nick; }
            set
            {
                _user.Nick = value;
                NotifyOfPropertyChange("Nick");
            }
        }

        public string Password
        {
            get { return _user.Password; }
            set
            {
                _user.Password = value;
                NotifyOfPropertyChange("Password");
            }
        }

        public void LogOn(string nick, string password)
        {
            var account = _pokecConn.LogOn(nick, password);
            if (account != null)
            {
                MessageBox.Show(account.SessionId);


                _shellViewModel.ShowMessengerView();
            }
        }

        #endregion

        #region Private members

        private User _user;

        private Dictionary<string, bool> _validProperties;

        #endregion

        #region Private methods

        private void ValidateProperties()
        {
            if (_validProperties.Values.Any(isValid => !isValid))
            {
                CanLogOn = false;
                NotifyOfPropertyChange(() => CanLogOn);
                return;
            }
            CanLogOn = true;
            NotifyOfPropertyChange(() => CanLogOn);
        }
        #endregion

        #region protected methods


        protected override void OnActivate()
        {
            base.OnActivate();
            CanLogOn = false;
        }

        protected override void OnInitialize()
        {
            base.OnInitialize();

            //var mycatalog = new AssemblyCatalog(System.Reflection.Assembly.LoadFrom("Pokec_Toolkit.dll"));

            //var container = new CompositionContainer(mycatalog);
            //container.ComposeParts(this);

            //_connConsumer = container.GetExportedValue<PokecConnectionConsumer>();
        }


        #endregion

        #region Constructor

        public LogOnViewModel()
        {
            _user = new User();
            _validProperties = new Dictionary<string, bool> { { "Nick", false }, { "Password", false } };
        }

        #endregion

        #region Properties

        public bool CanLogOn { get; set; }

        #endregion

        #region Implementation of IDataErrorInfo

        public string Error
        {
            get { return (_user as IDataErrorInfo).Error; }
        }

        public string this[string propertyName]
        {
            get
            {
                string error = (_user as IDataErrorInfo)[propertyName];
                _validProperties[propertyName] = String.IsNullOrEmpty(error) ? true : false;
                ValidateProperties();
                CommandManager.InvalidateRequerySuggested();
                return error;
            }
        }

        #endregion
    }
}

When I try compile this code, I get this error:

 

"The composition produced a single composition error. The root cause is provided below. Review the CompositionException.Errors property for more detailed information.\r\n\r\n1) Could not locate any instances of contract Spirit.ViewModels.LogOnViewModel.\r\n\r\nResulting in: An exception occurred while trying to create an instance of type 'Spirit.ViewModels.ShellViewModel'.\r\n\r\nResulting in: Cannot activate part 'Spirit.ViewModels.ShellViewModel'.\r\nElement: Spirit.ViewModels.ShellViewModel -->  Spirit.ViewModels.ShellViewModel -->  AssemblyCatalog (Assembly=\"Spirit_MEF, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null\")\r\n\r\nResulting in: Cannot get export 'Spirit.ViewModels.ShellViewModel (ContractName=\"Spirit.ViewModels.IShellViewModel\")' from part 'Spirit.ViewModels.ShellViewModel'.\r\nElement: Spirit.ViewModels.ShellViewModel (ContractName=\"Spirit.ViewModels.IShellViewModel\") -->  Spirit.ViewModels.ShellViewModel -->  AssemblyCatalog (Assembly=\"Spirit_MEF, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null\")\r\n"

 

I think problem is with exporting LogOnViewModel:

    [Export(typeof(ILogOnViewModel))]
    public class LogOnViewModel : Screen, IDataErrorInfo,ILogOnViewModel
    {
    }


and in the way how I create new view-model in shell-view-model:

   ActivateItem(IoC.Get<LogOnViewModel>());

Any advance? Thank for help