Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. RoutedUICommands vs ICommand. When & Why

RoutedUICommands vs ICommand. When & Why

Scheduled Pinned Locked Moved WPF
wpfcsharpvisual-studiocomarchitecture
11 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Vincent Beek

    Hope the title is OK. Didn't know how to put it any better. I am looking here for some background information, opinions if you will. I know how to code them. But I can't get my head around why sometimes things are done one way and sometimes the other. If you read WPF books (mostly) they teach you to implement RoutedUICommands. So in short, to have a static class with commands, commandbindings in XAML and some codebehind. If you read some of the MVVM articles on this site or this article http://msdn.microsoft.com/en-us/magazine/dd419663.aspx[^]. They implement classes with ICommand interface and/or a RelayCommand : ICommand class and put some command handling in the ViewModel So, when and why, pros and cons, of using one or the other. This is what I hope to hear from you.

    S Offline
    S Offline
    SledgeHammer01
    wrote on last edited by
    #2

    You should always write WPF code the MVVM way. It just produces cleaner, more organized and more efficient code. WPF and MVVM are really a match made in heaven. Once you get your MVVM framework situated, MVVM isn't all that difficult. People who don't want to take a couple of months to learn it and set up a framework are the only ones who make a big deal about how "hard and complicated" it is. If you try write a WPF the non MVVM way, you'll end up with a mess of spaghetti code.

    V realJSOPR 2 Replies Last reply
    0
    • S SledgeHammer01

      You should always write WPF code the MVVM way. It just produces cleaner, more organized and more efficient code. WPF and MVVM are really a match made in heaven. Once you get your MVVM framework situated, MVVM isn't all that difficult. People who don't want to take a couple of months to learn it and set up a framework are the only ones who make a big deal about how "hard and complicated" it is. If you try write a WPF the non MVVM way, you'll end up with a mess of spaghetti code.

      V Offline
      V Offline
      Vincent Beek
      wrote on last edited by
      #3

      Very well, I agree that MVVM is a good pattern. But when not using RoutedUICommand class you loose the bubbling and tunneling, right? This wil not get me into problems?

      S 1 Reply Last reply
      0
      • V Vincent Beek

        Very well, I agree that MVVM is a good pattern. But when not using RoutedUICommand class you loose the bubbling and tunneling, right? This wil not get me into problems?

        S Offline
        S Offline
        SledgeHammer01
        wrote on last edited by
        #4

        RelayCommand is really kind of required for MVVM. It allows you to define the CanExecute & Execute handlers as part of the RelayCommand object itself. With other types of command implementations, you need to add them to the CommandBindings collection which you don't really have access to in MVVM since the CommandBindingsCollection is part of the window and not the VM. You don't need bubbling and tunneling in VMs. Its mostly handling clicks and such.

        V 1 Reply Last reply
        0
        • S SledgeHammer01

          RelayCommand is really kind of required for MVVM. It allows you to define the CanExecute & Execute handlers as part of the RelayCommand object itself. With other types of command implementations, you need to add them to the CommandBindings collection which you don't really have access to in MVVM since the CommandBindingsCollection is part of the window and not the VM. You don't need bubbling and tunneling in VMs. Its mostly handling clicks and such.

          V Offline
          V Offline
          Vincent Beek
          wrote on last edited by
          #5

          Hm, make good sense to me. Thx. :thumbsup:

          S 1 Reply Last reply
          0
          • V Vincent Beek

            Hm, make good sense to me. Thx. :thumbsup:

            S Offline
            S Offline
            SledgeHammer01
            wrote on last edited by
            #6

            Oh, one other thing I can clear up for you... bubbling and tunneling is really only useful in controls IMO. You may have thought about using them to communicate between a child view and the parent, but that is really not the recommended pattern in MVVM. Most frameworks will include some sort of messenger service that basically lets you send async messages around to anybody who cares to listen for that specific message.

            1 Reply Last reply
            0
            • S SledgeHammer01

              You should always write WPF code the MVVM way. It just produces cleaner, more organized and more efficient code. WPF and MVVM are really a match made in heaven. Once you get your MVVM framework situated, MVVM isn't all that difficult. People who don't want to take a couple of months to learn it and set up a framework are the only ones who make a big deal about how "hard and complicated" it is. If you try write a WPF the non MVVM way, you'll end up with a mess of spaghetti code.

              realJSOPR Offline
              realJSOPR Offline
              realJSOP
              wrote on last edited by
              #7

              SledgeHammer01 wrote:

              If you try write WPF the non MVVM way, you'll end up with a mess of spaghetti code.

              FTFY...

              ".45 ACP - because shooting twice is just silly" - JSOP, 2010
              -----
              You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
              -----
              "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997

              S 1 Reply Last reply
              0
              • realJSOPR realJSOP

                SledgeHammer01 wrote:

                If you try write WPF the non MVVM way, you'll end up with a mess of spaghetti code.

                FTFY...

                ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                -----
                You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                -----
                "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997

                S Offline
                S Offline
                SledgeHammer01
                wrote on last edited by
                #8

                Seriously? WPF seems way cleaner then MFC or Winforms. Sounds like you need a better MVVM framework :).

                V 1 Reply Last reply
                0
                • S SledgeHammer01

                  Seriously? WPF seems way cleaner then MFC or Winforms. Sounds like you need a better MVVM framework :).

                  V Offline
                  V Offline
                  Vincent Beek
                  wrote on last edited by
                  #9

                  better framework, like?

                  S 1 Reply Last reply
                  0
                  • V Vincent Beek

                    better framework, like?

                    S Offline
                    S Offline
                    SledgeHammer01
                    wrote on last edited by
                    #10

                    The best framework is one you write yourself :) No joke. This is exactly what I did. There are a lot of popular frameworks out there: MVVM Light, Cinch, etc, but I've never been a fan of grabbing a bunch of open source libraries and getting them to work together. By writing it yourself, you learn MVVM and you know how stuff works inside. Imagine if you used Cinch and found some issue? Sascha is very active on CodeProject, but you never know what could happen down the road. Personally I like real light-weight stuff. So I pretty much have a full light-weight MVVM framework + light-weight DI container and don't have any of the Silverlight fluff that other libraries have.

                    V 1 Reply Last reply
                    0
                    • S SledgeHammer01

                      The best framework is one you write yourself :) No joke. This is exactly what I did. There are a lot of popular frameworks out there: MVVM Light, Cinch, etc, but I've never been a fan of grabbing a bunch of open source libraries and getting them to work together. By writing it yourself, you learn MVVM and you know how stuff works inside. Imagine if you used Cinch and found some issue? Sascha is very active on CodeProject, but you never know what could happen down the road. Personally I like real light-weight stuff. So I pretty much have a full light-weight MVVM framework + light-weight DI container and don't have any of the Silverlight fluff that other libraries have.

                      V Offline
                      V Offline
                      Vincent Beek
                      wrote on last edited by
                      #11

                      I am not a fan of just grabbing a framework

                      either. At least you have to know the concept of what is happening in there. Otherwise when you will run into bug of the framework you will get stuck.
                      I am now in the process of making this decision. I know MVVM is the way to go. But I think I will start with my own implementation to see before I get involved in a framework.

                      Do you know this framework?
                      Catel - Part 0 of n: Why choose Catel?[^]

                      It’s a relative newcomer. What do you think of it?

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups