在视图模型之间传递参数 [英] Passing parameters between viewmodels

查看:84
本文介绍了在视图模型之间传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用中,我有2次浏览.列表(GridView)和窗体.我正在按照此线程的建议更改视图: WPF MVVM切换器用户控件

In my app I have 2 views. List(GridView) and Form. I am changing views as proposed in this thread: WPF MVVM switch usercontrols

现在,在单击编辑"以显示带有编辑表单的新视图后,如何传递所选项目的ID时出现问题.

Now I have a problem how to pass id of selected item after click edit to show new View with the edit form.

一个简单的应用程序可以列出所有项目,添加新内容,删除和编辑.如何在MVVWM中做到这一点?


更新

Simple application to list all items, add new, delete and edit. How can I do this in MVVWM?


UPDATE

我只想创建一个简单的应用程序,其左侧的菜单为:

I just want to create a simple application, that has menu on the left with:

  1. 列表
  2. 添加新内容.

单击列表"时,它将显示带有列表和3个按钮的UC:添加",编辑",删除".单击添加后,对其进行编辑以显示带有特定值的表格的UC(在编辑时).我该如何实现?

When click List, it shows UC with list and 3 buttons: Add, Edit, Delete. After click add, edit it shows UC with Form with specific values (while editing). How can I achieve this??

推荐答案

据我了解,您想要这样的东西:

As far as I understand, you want something like this:

这样,当您单击Add时,它将显示以下内容:

So that when you click Add, it shows this:

对吗?

因此,您需要以下行为:

So you need the following behaviour:

  • 添加不需要任何ID.
  • 添加完成后,列表必须重新加载.
  • 编辑会收到列表的选定项目ID.
  • 添加完成后,列表必须重新加载.

我将假定您正在使用某个存储库.

I will assume that you are working with some repository.

我提出了以下MVVM结构:

I propose the following MVVM structure:

  • MainViewModel:主屏幕的DataContext.
    • BaseViewModel RightViewModel:显示在屏幕右侧的视图模型的容器.
    • ICommand ViewListCommand:通过创建ListViewModel的新实例并将其分配给BaseViewModel属性来显示列表.
    • ICommand AddNewCommand:通过创建AddViewModel的新符号并将其分配给BaseViewModel属性来显示addnew屏幕.
    • MainViewModel: The DataContext for the main screen.
      • BaseViewModel RightViewModel: The container for viewmodels shown on the right part of the screen.
      • ICommand ViewListCommand: Shows the list by creating a new instance of ListViewModel and assigning it to the BaseViewModel property.
      • ICommand AddNewCommand: Shows the addnew screen by creating a new isntance of AddViewModel and assigning it to the BaseViewModel property.

      然后:

      • ListViewModel:单击列表时,屏幕右侧的DataContext.
        • List<Items> Items:提供要显示的项目.
        • Item SelectedItem:将绑定到UI上的SelectedItem的属性.
        • ICommand EditCommand:该命令将获取所选项目并通知必须编辑的MainViewModel.
        • ListViewModel: The DataContext for the right part of the screen when List is clicked.
          • List<Items> Items: Provides the items to be shown.
          • Item SelectedItem: The property that will be binded to the SelectedItem on the UI.
          • ICommand EditCommand: The command that will get the selected item and notify the MainViewModel that has to be edited.

          因此,在某个时候,一个视图模型将收到一个子视图模型的通知(即列表将告诉main编辑某个内容).我建议通过使用以下事件来做到这一点:

          So at some point, a viewmodel will receive a notification from a child viewmodel (i.e. list will tell main to edit somethin). I recommend to do this by using events as:

          public class Item
          {
              public string Name { get; set; }
          }
          
          public class ItemEventArgs : EventArgs
          {
              public Item Item { get; set; }
          
              public ItemEventArgs(Item selectedItem)
              {
                  this.Item = selectedItem;
              }
          }
          
          public class BaseViewModel
          {
          
          }
          
          public class ListViewModel : BaseViewModel
          {
              public event EventHandler<ItemEventArgs> EditItem;
          
              public Item SelectedItem { get; set; }
          
              public ICommand EditItemCommand { get; private set; }
          
              public ListViewModel()
              {
                  this.EditItemCommand = new DelegateCommand(() => this.EditItem(this, new ItemEventArgs(this.SelectedItem)));
              }
          }
          
          public class EditViewModel : BaseViewModel
          {
          
          }
          
          public class MainViewModel
          {
              public BaseViewModel RightViewModel { get; private set; }
          
              public ICommand ViewListCommand { get; private set; }
          
              public MainViewModel()
              {
                  this.ViewListCommand = new DelegateCommand(() =>
                  {
                      // unhook possible previous events
                      var listViewModel = new ListViewModel();
                      listViewModel.EditItem += listViewModel_EditItem;
                      this.RightViewModel = listViewModel;
                  });
              }
          
              void listViewModel_EditItem(object sender, ItemEventArgs e)
              {
                  // unhook possible previous events
                  var editViewModel = new EditViewModel();
                  this.RightViewModel = editViewModel;
              }
          
          }
          

          xaml绑定不是必需的,因为它将非常向前.

          The xaml binding is not necessary since it will be quite forward.

          这是一个非常基本的示例,说明了我认为如何处理此类问题.重要的是要正确解开事件,否则您可能会遇到问题.

          This is a very basic example about how I think this kind of stuff can be handled. The important thing here is to properly unhook the events, otherwise you can run into problems.

          对于这类内容,您还可以查看反应式用户界面.

          For this kind of stuff you can also have a look at Reactive UI.

          这篇关于在视图模型之间传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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