代码之家  ›  专栏  ›  技术社区  ›  yoav.str

MVVM事件-在MVVM体系结构中,它们应该如何发生以及在何处发生?

  •  1
  • yoav.str  · 技术社区  · 14 年前

    假设我有一个主窗口xmal:

    <Window x:Class="MVVMTUTRIALS.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:views="clr-namespace:TestMvvm444.Views"
    Title="Window1" Height="300" Width="400" Loaded="Window_Loaded">
    <Grid>
        <views:CustomersList x:Name="CustomersList"/>
        <views:CustomerBoughtList x:Name="CustomerBoughtList"/>
    </Grid>
    </Window>
    

    我想要一个在CustomersList(点击cerrtian raw)中调用规则的事件

    1.活动应该在哪里?在主窗口思考是合理的吗?

    有人能指导我怎么做吗?

    我认为我误解的核心是两个UserControl(s)如何相互通信以及如何与视图模型通信

    谢谢你阅读和做笔记。

    2 回复  |  直到 14 年前
        1
  •  1
  •   Tim Cooper    13 年前

    有多种方法可以解决这个问题。下面是用伪代码表示的一对。首先,协调视图模型:

    public class CustomersViewModel : ViewModel
    {
        public event EventHandler<EventArgs> SelectedCustomerChanged;
    
        public ICollection<Customer> Customers
        {
            get ...
        }
    
        public CustomerViewModel SelectedCustomer
        {
            get ...
            set ...
        }
    }
    
    public class CustomerPurchasesViewModel : ViewModel
    {
        public CustomerViewModel Customer
        {
            get ...
            set ...
        }
    
        public ICollection<PurchaseViewModel> Purchases
        {
            get ...
        }
    }
    
    public class MainViewModel : ViewModel
    {
        private CustomersViewModel customers;
        private CustomerPurchasesViewModel customerPurchases;
    
        public MainViewModel(CustomersViewModel customers, CustomerPurchasesViewModel customerPurchases)
        {
            this.customers = customers;
            this.customerPurchases = customerPurchases;
    
            // push changes in selection to the customer purchases VM
            this.customers.SelectedCustomerChanged += delegate
            {
                this.customerPurchases.Customer = this.customers.SelectedCustomer;
            };
        }
    }
    

    public class CustomersViewModel : ViewModel
    {
        public ICollection<Customer> Customers
        {
            get ...
        }
    
        public CustomerViewModel SelectedCustomer
        {
            get ...
            set
            {
                ...
                eventHub.Publish(new CustomerSelectedMessage(value));
            }
        }
    }
    
    public class CustomerPurchasesViewModel : ViewModel, ISubscriber<CustomerSelectedMessage>
    {
        public CustomerViewModel Customer
        {
            get ...
            set ...
        }
    
        public ICollection<PurchaseViewModel> Purchases
        {
            get ...
        }
    
        private void Receive(CustomerSelectedMessage m)
        {
            this.Customer = e.Customer;
        }
    }
    
    public class MainViewModel : ViewModel
    {
        private CustomersViewModel customers;
        private CustomerPurchasesViewModel customerPurchases;
    
        public MainViewModel(CustomersViewModel customers, CustomerPurchasesViewModel customerPurchases)
        {
            this.customers = customers;
            this.customerPurchases = customerPurchases;
        }
    }
    
        2
  •  -1
  •   Mark Bostleman    14 年前

    主窗口的视图模型可以有两个公共列表属性,用户控件可以绑定到这两个属性。然后,在视图中更改选择时,可以在视图模型列表中检测到该选择,并对其他列表执行任何需要执行的操作。视图中没有事件处理。