Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I write WPF efficiently?

Tags:

wpf

When I first learned about Microsoft's then-new framework for developing desktop applications, WPF, I thought that one of the biggest benefits of it would be its efficiency. After all, I already know what the GUI controls and elements were that I wanted to use--I just have to slap them all on the page and then wire up all my bindings right, and I'll be done. After that, I learned about the MVVM pattern, which promised clean separation of concern within my WPF app.

I thought this was going to be great! I got into creating several different admin and data entry WPF apps with my team at work, and thus I began to crank out working software with robust but simple GUIs, right?

Not so fast, there, cowboy coder. My experience is that writing WPF is S-L-O-W.

Well, at least the way I do it. You see, after I have a picture of my GUI on a whiteboard, I code up the XAML with the controls that I want. This part, as expected, is fast--laying out the whole of a window is pretty quick. After that, its all the little stuff you want these elements to do takes awhile. I always seem to want to make this text bold in some cases; show a red error box in these other cases.

Then things unravel: this binding isn't working right over here--I have to write a converter and adjust the layout for the right values. Whoops, I forgot that extra NotifyPropertyChanged there. Oh, and I want to use a different template in this state vs. that, so I have to figure out what I can use to swap the templates in certain situation. This data is coming in asynchronously, so I need to make sure the right thing is happening on the right thread and that Property gets NotifyChanged as well. Crap, when I maximize my window, it doesn't stretch like I thought it would--must be because its container height isn't defined, but I don't want to define that, so I have to make it stretch in its parent. Oh, now I really want to make a user control out of this stuff over here, so I better implement some dependency properties...

On and on I go, spending hours and days on stuff that just feels so small and minor. I soon resort to cutting usability features and look-and-feel enhancements because this is taking just too darn long.

Does anyone have any tips or principles I need to try in order to write WPF efficiently?

like image 727
Patrick Szalapski Avatar asked Aug 24 '11 16:08

Patrick Szalapski


People also ask

Is WPF still in demand?

WPF is still one of the most used app frameworks in use on Windows (right behind WinForms).

Is WPF harder than WinForms?

It is simple to use WinForms as controls can be used easily, it's less time-consuming and less tricky as compared to WPF.

Does WPF have future?

Now this is a big thing coming from Microsoft where it is making a move from WPF to . NET Core. This move clearly explains that Microsoft sees a future in WPF and considers it as a UI framework for the . NET platform.

Which is faster WPF or WinForms?

You will NEVER have this problem with WPF, not only because it is hardware-accelerated, but also because it is Vector based, rather than Bitmap based. Conclusion: While winforms may perform better in the 1-control case, WPF is a clear winner at all levels for real-world Datacentric UIs.


1 Answers

A couple of things that have saved a lot of time for me:

  • Use DockPanel as your default panel for layout unless you have a good reason not to.
  • Keep a folder full of useful classes: a ViewModelBase class that implements INotifyPropertyChanged, a RelayCommand class, etc. There's no need to get fancy and try to make this a separate assembly that you build into your project; just write reasonably good implementations and copy/paste them into your new projects.
  • Get Resharper and use it. Use templates for creating dependency properties, properties that do change notification, and commands.
  • Find or build a good library for asynchronous task management.

I find that even for very simple applications I get more done faster with WPF than I did with Windows Forms. For applications that aren't very simple, there's absolutely no comparison.

For the most part, WPF applications are a lot of work to develop because it's harder to make the case for cutting out UI features. You can't just say, "Oh, that's not possible," because it probably is possible (whatever "it" is).

like image 56
Robert Rossney Avatar answered Oct 03 '22 07:10

Robert Rossney