我怎样才能避免在视图模型命令混乱? [英] How can I avoid command clutter in the ViewModel?

查看:183
本文介绍了我怎样才能避免在视图模型命令混乱?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个使用了不少的命令的应用程序,并且它们塞满了我的视图模型。 MVVM是新的给我,很抱歉,如果这个问题有点笨。有没有减少杂波的方法吗?例如在这里你可以看到杂乱的一部分。

 私人无效InitializeCommands()
{
LogoutCommand =新RelayCommand(注销);
OpenCommand =新RelayCommand(SetImage);
SaveCommand =新RelayCommand(SaveImage,SaveImageCanExecute);
UploadToFlickrCommand =新RelayCommand(UploadToFlickr);
CropCommand =新RelayCommand(SetCropMouseEvents);
RemoveRedEyeCommand =新RelayCommand(SetRemoveRedEyeMouseEvents);
TextInputCropCommand =新RelayCommand(CropFromText);
ReloadImageCommand =新RelayCommand(ReloadImage);
FlipYCommand =新RelayCommand(FlipY);
Rotate90RCommand =新RelayCommand(Rotate90R);
FlipXCommand =新RelayCommand(FlipX);
ToGrayscaleCommand =新RelayCommand(ToGrayscale);
ToSepiaCommand =新RelayCommand(ToSepia);
WindowClosingCommand =新RelayCommand(的windowClosing);
EffectsViewCommand =新RelayCommand(()=> CurrentToolView =新EffectsView());
AddTextCommand =新RelayCommand(()=> CurrentToolView =新AddTextView());
ResizeCommand =新RelayCommand(()=> CurrentToolView =新ResizeView());
CropViewCommand =新RelayCommand(()=> CurrentToolView =新CropView());
RedEyeCommand =新RelayCommand(()=> CurrentToolView =新RedEyeView());
RotateViewCommand =新RelayCommand(()=> CurrentToolView =新RotateView());
ExitCommand =新RelayCommand(()=> Application.Current.Shutdown());
FullscreenCommand =新RelayCommand(()=>
{
变种FS =新FullscreenView
{FullscreenImage = CurrentImage.LoadedImage};
fs.Show();
});
HandleDropCommand =新RelayCommand< D​​ragEventArgs>(E => OnFileDrop(这一点,E));
Messenger.Default.Register<使用者>(这一点,用户登陆,SETUSER);
Messenger.Default.Register< FlickrAccount>(这一点,AddedAccount,AddAccount);
Messenger.Default.Register<串GT;(这一点,INeedAUser,SendUser);
Messenger.Default.Register<串GT;(这一点,INeedAImage,SendImage);
}


解决方案

所以,你必须命令为:




  1. 文件操作(打开,保存,上传到闪烁)


  2. 窗口操作(全屏,关闭)


  3. 编辑(旋转,调整大小,颜色等)




考虑打了个比方FileCommands一个自定义类分组(作曲)相关的命令在一起。建立多层次结构(如果适用)。如果您的视图有一个分层的菜单,你可能会希望类似的命令层次。



然后,创建一个控制器每个命令组( 。例如FileController)和在控制器创建方法,将寄存器从FileCommands组与相关联的服务的命令



请参阅 http://waf.codeplex.com/ 示例应用程序(例如BookController.cs)有关如何真正实现控制器/视图模型映射的一些想法。但是请注意,这不是完全相同的情况(无断命令的成组)。


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

    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);
    }

解决方案

So you have commands for:

  1. File operations (Open, Save, Upload to Flicker)

  2. Window operations (Full screen, Close)

  3. Editing (Rotating, Resizing, Colour, etc)

Consider grouping (composing) related commands together in a custom class called for example FileCommands. Create a multi-level hierarchy if applicable. If you have a hierarchical menu in your view, You'll likely want similar Command Hierarchy.

Then, create one Controller per command group (for example FileController) and in the controller create method that will register the commands from the FileCommands group with the associated service.

See http://waf.codeplex.com/ sample applications (for example BookController.cs) for some ideas on how to actually implement Controller/ViewModel mapping. Note however that it's not exact same scenario (no breaking of Commands into groups).

这篇关于我怎样才能避免在视图模型命令混乱?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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