将参数从视图模型发送到另一个视图模型的构造函数 [英] send parameter from view-model to constructor of another view-model

查看:78
本文介绍了将参数从视图模型发送到另一个视图模型的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在wpf中使用caliburn micro和MEF,但我遇到了这个问题.

I use caliburn micro and MEF in wpf and I have this problem.

我创建了shell-view-model:

I create shell-view-model:

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

根据我创建的视图模型并在新的视图模型中显示

From view-model I create and show in new view-model

[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()只能是字符串.

如何解决这个问题?

推荐答案

在这种情况下,我不会使用IoC类,因为这是服务定位器反模式的示例,因此不建议使用. Rob在他的Caliburn.Micro文档中提到了这一点.您还可以阅读 http://blog.ploeh.dk/2010/02/03 /ServiceLocatorIsAnAntiPattern.aspx 可以很好地描述反模式.

I wouldn't use the IoC class in this context, as this is an example of the service locator anti-pattern, and isn't recommended. Rob mentions this in his Caliburn.Micro documentation. You can also read http://blog.ploeh.dk/2010/02/03/ServiceLocatorIsAnAntiPattern.aspx for a nice description of the anti-pattern.

相反,我将使用抽象工厂模式,并将工厂(的抽象)传递到您的shell视图模型中.它可以具有创建新的登录视图模型和Messenger视图模型的方法.在该工厂的具体实现中,您可以手动实例化这些视图模型,并只传递它们所需的信息.

Instead, I would use an abstract factory pattern, and pass (an abstraction of) a factory into your shell view model. This can have methods to create a new logon view model, and messenger view model. In the concrete implementation of this factory, you can instantiate these view models by hand, and just pass in the information they require.

我还将从登录视图模型中删除对外壳视图模型的引用.而是使用外壳程序可以订阅的标准.NET事件,或查看在Caliburn.Micro中实现的事件聚合器(可在codeplex站点上找到一个示例),它是介体设计模式的实现.这样可以确保您的视图模型之间实现良好的去耦.

I would also remove the reference to the shell view model from the logon view model. Instead, either use standard .NET events which the shell can subscribe to, or have a look at the event aggregator implemented in Caliburn.Micro (a sample is available on the codeplex site), which is an implementation of the mediator design pattern. This will ensure nice decoupling between your view models.

这篇关于将参数从视图模型发送到另一个视图模型的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆