我在 wpf 中使用 caliburn micro 和 MEF,我遇到了这个问题。
我创建外壳视图模型:
public interface IShellViewModel
{
void ShowLogOnView();
void ShowMessengerView(PokecAccount account);
}
[Export(typeof(IShellViewModel))]
public class ShellViewModel : Conductor<IScreen>, IShellViewModel
{
public ShellViewModel()
{
ShowLogOnView();
}
public void ShowLogOnView()
{
ActivateItem(IoC.Get<LogOnViewModel>());
}
public void ShowMessengerView(PokecAccount account)
{
//send to constructor of MessangerViewModel paramter typeof PokecAccount(own class)
ActivateItem(IoC.Get<MessengerViewModel>(account));
}
}
从视图模型中我创建并显示在新的视图模型中
[Export]
public class LogOnViewModel : Screen, IDataErrorInfo, ILogOnViewModel
{
[Import]
private IShellViewModel _shellViewModel;
[Import]
private IPokecConnection _pokecConn;
private PokecAccount _account;
public void LogOn(string nick, string password)
{
_account = _pokecConn.LogOn(nick, password);
if (_account != null)
{
//create new view-model and show it, problem is send parameter to construtor of MessengerViewModel
_shellViewModel.ShowMessengerView(_account);
}
}
}
问题就在这里
//send to constructor of MessangerViewModel paramter typeof PokecAccount(own class)
ActivateItem(IoC.Get<MessengerViewModel>(account));
新的视图模型
[Export]
public class MessengerViewModel : Screen, IMessengerViewModel
{
private PokecAccount _account;
public MessengerViewModel(PokecAccount account)
{
_account = account;
}
}
问题在这里:
//send to constructor of MessangerViewModel paramter typeof PokecAccount(own class)
ActivateItem(IoC.Get<MessengerViewModel>(account));
参数 IoC.Get() 只能是字符串。
如何解决这个问题?