如何避免 ViewModel 中的命令混乱? [英] How can I avoid command clutter in the ViewModel?

查看:26
本文介绍了如何避免 ViewModel 中的命令混乱?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个使用很多命令的应用程序,它们使我的视图模型变得混乱.MVVM 对我来说是新的,如果这个问题有点愚蠢,很抱歉.有没有办法减少混乱?例如在这里你可以看到杂乱的一部分..

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

推荐答案

所以你有以下命令:

  1. 文件操作(打开、保存、上传到 Flicker)

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

窗口操作(全屏、关闭)

Window operations (Full screen, Close)

编辑(旋转、调整大小、颜色等)

Editing (Rotating, Resizing, Colour, etc)

考虑将相关命令组合(组合)到一个自定义类中,例如 FileCommands.如果适用,请创建多级层次结构.如果您的视图中有分层菜单,您可能需要类似的命令层次结构.

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.

然后,为每个命令组(例如 FileController)创建一个 控制器,并在控制器创建方法中将来自 FileCommands 组的命令注册到相关服务.

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.

请参阅 http://waf.codeplex.com/ 示例应用程序(例如 BookController.cs)关于如何实际实现 Controller/ViewModel 映射的一些想法.但是请注意,这不是完全相同的场景(没有将命令分成组).

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).

这篇关于如何避免 ViewModel 中的命令混乱?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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