How can I avoid team clutter in ViewModel?

I am creating an application that uses quite a few commands, and they clutter up my viewing model. MVVM is new to me, so sorry if this question is a little stupid. Is there a way to reduce clutter? For example, here you can see part of the mess.

private void InitializeCommands() { LogoutCommand = new RelayCommand(Logout); OpenCommand = new RelayCommand(SetImage); SaveCommand = new RelayCommand(SaveImage, SaveImageCanExecute); UploadToFlickrCommand = new RelayCommand(UploadToFlickr); CropCommand = new RelayCommand(SetCropMouseEvents); RemoveRedEyeCommand = new RelayCommand(SetRemoveRedEyeMouseEvents); TextInputCropCommand = new RelayCommand(CropFromText); ReloadImageCommand = new RelayCommand(ReloadImage); FlipYCommand = new RelayCommand(FlipY); Rotate90RCommand = new RelayCommand(Rotate90R); FlipXCommand = new RelayCommand(FlipX); ToGrayscaleCommand = new RelayCommand(ToGrayscale); ToSepiaCommand = new RelayCommand(ToSepia); WindowClosingCommand = new RelayCommand(WindowClosing); EffectsViewCommand = new RelayCommand(() => CurrentToolView = new EffectsView()); AddTextCommand = new RelayCommand(() => CurrentToolView = new AddTextView()); ResizeCommand = new RelayCommand(() => CurrentToolView = new ResizeView()); CropViewCommand = new RelayCommand(() => CurrentToolView = new CropView()); RedEyeCommand = new RelayCommand(() => CurrentToolView = new RedEyeView()); RotateViewCommand = new RelayCommand(() => CurrentToolView = new RotateView()); ExitCommand = new RelayCommand(() => Application.Current.Shutdown()); FullscreenCommand = new RelayCommand(() => { var fs = new FullscreenView {FullscreenImage = CurrentImage.LoadedImage}; fs.Show(); }); HandleDropCommand = new RelayCommand<DragEventArgs>(e => OnFileDrop(this, e)); Messenger.Default.Register<User>(this, "UserLogin", SetUser); Messenger.Default.Register<FlickrAccount>(this, "AddedAccount", AddAccount); Messenger.Default.Register<string>(this, "INeedAUser", SendUser); Messenger.Default.Register<string>(this, "INeedAImage", SendImage); } 
+8
c # command viewmodel mvvm
source share
3 answers

So, you have commands for:

  • File Operations (Open, Save, Upload to Flicker)

  • Window operations (full screen, closed)

  • Editing (rotation, resizing, color, etc.)

Consider grouping (linking) related commands together in a custom class called, for example, FileCommands. Create a layered hierarchy, if applicable. If you have a hierarchical menu in your view, you will most likely need a similar command hierarchy.

Then create one controller for each group of commands (for example, FileController) and in the method of creating a controller that will register commands from the FileCommands group with the corresponding service.

See http://waf.codeplex.com/ sample applications (e.g. BookController.cs) for some ideas on how to actually implement Controller / ViewModel mapping. Note, however, that this is not an exact scenario (without splitting the teams into groups).

+5
source share

Use Caliburn Micro. For a button with the name name = "Exit", the only thing needed in the ViewModel is a public method called "Exit".

And without convetion bindings:

 <Button Content="Remove" cal:Message.Attach="[Event Click] = [Action Remove($dataContext)]" /> 

Then in the ViewModel add a method called Remove, and in this example, the DataContext is passed to the method.

+1
source share

Your ViewModel is designed to be the glue between the view and the model. This means that if you cannot repeat the model as a whole, it will always consist of a listing of β€œadhesive lines”.

The only mess I can imagine is you can get rid of it if you don't need the XXXCommand properties literally; in this case, you can create a collection of properties, such as (pseudocode)

 private void createCommands() { var commands={ "Logout"=>new RelayCommand(Logout), "Exit"=>new RelayComand( ()=>Application.Current.Shutdown() ), .... }; foreach( var key,cmd in commands ){ glue(key,cmd); } }; 

There is no other reason to maintain links to the objects you created, except that they glue them with the correct View insert.

But then again, why not use the Idiot Property for this? Again: the amount of clutter, as I see it, is quite limited.

0
source share

All Articles