1

I am using the example from Josh Smith. He has a WorkspaceViewModel which display a list of CustomerViewModels. He is using the same ViewModel for displaying all Customers and for editing one single customer.

Is this a good practice? If I have a list of CustomerViewModels I dont need the SaveCommand or the CloseCommand or some IsSelected Flag.

Is it better to have a seperate EditCustomerViewModel? But how to deal with Workspace related stuff? For example:

public class Workspace : ViewModel
{
    public ICommand CloseCommand;
}

public class AllCustomers : Workspace
{
    public ObservableCollection<CustomerViewModel> CustomerViewModels;
}

// Option A (one ViewModel for display and edit):
public class CustomerViewModel : ? 
{
    public string CustomerName;
    public ICommand SaveCommand;    
}

Or seperation:

// Option B:
public class CustomerViewModel : ViewModel 
{
    public string CustomerName;
}

public class EditCustomerViewModel : Workspace
{
    public CustomerViewModel CustomerViewModel;
    public ICommand SaveCommand;
}

// Option C (CustomerViewModel does not need CloseCommand but EditCustomerViewModel does):
public class CustomerViewModel : Workspace 
{
    public string CustomerName;
}

public class EditCustomerViewModel : CustomerViewModel
{   
    public ICommand SaveCommand;    
}

Edit: I try to clarify my problem. In the CustomerViewModel in the example of Josh Smith, he has Commands for closing and saving a customer. In the AllCustomerView he has a GridView that binds to a ObservableCollection of CustomerViewModels. But in the GridView the both commands are not necessary. In the GridView I can ignore both commands, but is that a good design?

4

1 回答 1

0

我不太确定您的意思,因为快速浏览该文章表明他同时使用了列表和视图/编辑视图模式,分别称为AllCustomers­ViewModelCustomerViewModel。这肯定是推荐的做法,而不是使用具有多个职责的单一视图模型。

这两个视图模型都继承自WorkspaceViewModel其中添加了他的工作区功能。所以,总而言之,如果你想构建类似的东西,我会遵循他的模式。您还应该认真考虑 MVVM 框架,例如Caliburn.Micro,它添加了简单的基于约定的视图组合以及屏幕生命周期。

于 2013-01-14T13:49:08.950 回答