WPF Prism V2使用M-V-VM - 在运行时将视图添加到ViewModel中的区域

时间:2009-05-25 11:35:37

标签: wpf mvvm dependency-injection prism

希望非常简单,我第一次尝试使用M-V-VM在WPF上使用Prism V2,到目前为止,我发现一切都非常棒。 My Shell非常简单,Top控制台上的Ribbon控件,左侧是Help Desk票证的DataGrid,右侧是TabControl。

当用户从数据网格打开所选票证时,我希望票证在选项卡控件上作为选项卡打开。我知道为了做到这一点,我需要使用RegionManager添加然后激活View到该区域。但是从ViewModel这样做对我来说似乎不正确,虽然我可以使用DI(DepenecyInjection)做到这一点,它仍然在我的头脑中响起了关于给ViewModel一些关于View的知识。

要添加到这个,不同的模块也将添加其他视图(联系人,客户端等)到TabControl,我想使用DataTemplates让TabControl正确显示视图,任何人都可以给我任何指针这也是。

非常感谢 本

请完整答案,而不仅仅是链接。这就是StackOverflow的用途!

2 个答案:

答案 0 :(得分:1)

MVVM +服务=终极力量!

服务只是一个众所周知的接口,已在您的IOC容器中注册。当ViewModel需要在自身之外做某些事情时,比如说打开选项卡式文档,它就会使用该服务。然后根据特定程序的需要实现服务。

例如:

public interface IDocumentService
{
    void OpenDocument(IViewModel viewModel);
}

internal class DocumentService:IDocumentService
{
    public void OpenDocument(IViewModel viewModel)
    {
        // Implement code to select the View for the ViewModel,
        // and add it to your TabControl.
    }
}

{
    // Somewhere in your ViewModel...
    // Make sure you can get the IDocumentService
    IDocumentService docService = ioc.Get<IDocumentService>();
    docService.OpenDocument(new TicketViewModel());
}

答案 1 :(得分:0)

命令是执行此操作的方法 - 您将向自己发送命令,称为“RequestBringTicketIntoView”;它会冒泡到窗口,你可以在那里处理它。阅读Josh Smith的文章:

http://joshsmithonwpf.wordpress.com/2008/03/18/understanding-routed-commands/