wpf mvvm 使用命令在视图模型之间传递参数

发布于 2024-10-11 07:37:03 字数 530 浏览 1 评论 0原文

这是我第一次尝试 MVVM。我的应用程序的核心松散地基于 Josh Smith 的 msdn 文章。而且我也在使用 mvvm light 框架。

我有一个主窗口,其中包含命令列表区域和工作区区域,其中将用户控件/视图显示为选项卡项,每个用户控件都有一个相应的视图模型。主窗口还有一个包含我的命令列表的视图模型,工作区视图模型有一个基础工作区视图模型。

我的默认视图有一个 MappingSets 的主数据网格,它可以包含一个选定的项目。这些命令启动新的选项卡项,其中包含基于所选项目处理 MappingSet 详细信息的视图。我有一个 View/ViewModel,根据所使用的命令,应该返回一个用于创建没有现有数据的新 MappingSet 的 tabitem,或者一个包含要编辑的所选项目的详细信息的 tabitem,或者一个包含所选项目的详细信息的 tabitem,如新映射集的基础。

设置场景后,我没有设法解决的是命令相关的方式来传递参数,例如所选 MappingSet 对象的标识符,以在上述三种状态之一中实例化我的视图模型?例如,mvvmlight Messenger 适合这项任务吗?

This is my first attempt at MVVM. My application's core is loosely based Josh Smith's msdn article. And I am also using the mvvm light framework.

I have a main window containing a command list area and a workspace area which shows usercontrols/views as tabitems, each usercontrol has a corresponding viewmodel. The mainWindow also has a viewmodel containing my command list, and the workspace viewmodels have a base workspace viewmodel.

My default view has a master datagrid, of MappingSets, that can have one selected item. The commands launch new tabitems with views that handle MappingSet detail based on that selected item. I have a View/ViewModel that, depending on the command used should return either a tabitem for creating a new MappingSet with no existing data, or a tabitem containing the detail of the selected item for editing, or a tabitem containing detail the selected item as the base for a new MappingSet.

Having Set the scene, what I have not managed to work out is command dependent way to pass parameters, such as the identifier of the selected MappingSet object, to instantiate my viewmodel in one of the three states mentioned above? For instance would the mvvmlight messenger be appropriate for this task?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽容 2024-10-18 07:37:03

对于消息传递/事件聚合器来说,这是一个完美的场景。但是,您的消息链可能有点复杂。据我了解,您的主窗口包含命令列表(如菜单或功能区)。以下是我对事件链的看法。

  1. 您从数据网格中选择一个 MappingSet,这会导致触发 MappingSetSelected 消息(带有所选 MappingSet 的有效负载)
  2. 主窗口侦听该消息并存储当前选定的 MappingSet
  3. 当用户单击按钮“EditMappingSet”或“ CreateNewMappingSet”消息被触发(或者如果窗口负责创建新视图,它会自己创建它们)。

This is a perfect scenario for the messenger/eventaggregator. However, your message chain might be a bit convoluted. From what I'm understanding, your Main window holds a list of commands (like a menu or a ribbon). Here is how I see the chain of events.

  1. You select a MappingSet from the datagrid, this causes a MappingSetSelected message to be fired (with a payload of the selected MappingSet)
  2. The main window listens for that message and stores the currently selected MappingSet
  3. When the user clicks the button a "EditMappingSet" or "CreateNewMappingSet" message is fired (or if the Window is responsible for creating the new views, it creates them itself).
滿滿的愛 2024-10-18 07:37:03

如果只有三个选项,您可以将它们绑定到三个不同的命令,并在命令中传递您的自定义变量。

private RelayCommand _openMappingSetCommand;

//Command that one of your options is bound to
public ICommand ViewMappingSetOption1
    {
        get
        {
            if (_openMappingSetCommand == null)
            {
                _openMappingSetCommand = new RelayCommand(param => this.DoTabRequest("your parameter");
            }
            return _openMappingSetCommand ;
        }
    }


// Method that creates your viewmodel
private void DoTabRequest(parameterType parameter)
    {
        WorkspaceViewModel viewModel = null;

        if (viewModel == null)
        {
            viewModel = (WorkspaceViewModel)Activator.CreateInstance(typeof (viewModelType), parameter);
            this.Workspaces.Add(viewModel);
        }

        this.ActiveWorkspace = viewModel;
    }

然后在视图模型的构造函数中允许该参数,并根据该参数执行您需要的其他操作。

If there are only three options, you could have them binding to three different commands and within the commands do the passing of your self-defined variable.

private RelayCommand _openMappingSetCommand;

//Command that one of your options is bound to
public ICommand ViewMappingSetOption1
    {
        get
        {
            if (_openMappingSetCommand == null)
            {
                _openMappingSetCommand = new RelayCommand(param => this.DoTabRequest("your parameter");
            }
            return _openMappingSetCommand ;
        }
    }


// Method that creates your viewmodel
private void DoTabRequest(parameterType parameter)
    {
        WorkspaceViewModel viewModel = null;

        if (viewModel == null)
        {
            viewModel = (WorkspaceViewModel)Activator.CreateInstance(typeof (viewModelType), parameter);
            this.Workspaces.Add(viewModel);
        }

        this.ActiveWorkspace = viewModel;
    }

Then allow for that parameter on the constructor of your viewmodel and do whatever else you need based on that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文