How can I write WPF effectively?

When I first learned about Microsoft, the new WPF desktop development environment, I thought that one of the biggest advantages of this would be its effectiveness. In the end, I already know which GUI controls and elements were the ones I wanted to use - I just need to hit them across all the pages and then connect all my bindings and I’ll finish. After that, I found out about the MVVM pattern, which promised a clean separation of concern in my WPF application.

I thought it would be great! I started creating several WPF applications and data entry applications with my team at work, and so I started checking working software with reliable but simple graphical interfaces, right?

Not so fast there, cowboy coder. My experience is that the WPF entry is SLOW.

Well, at least the way I do it. You see, after I have the image of my GUI on the board, I encode XAML using the controls I want. This part, as expected, quickly lays the whole window pretty quickly. After that, all the little things that you want these elements to do takes some time. It seems I always want to make this text bold in some cases; in these other cases, a red error field is displayed.

Then things work out: this binding does not work right here - I need to write a converter and adjust the layout for the correct values. Oops, I forgot that an additional NotifyPropertyChanged was added there. Oh, and I want to use a different template in this state compared to this, so I have to figure out what I can use to replace the templates in a specific situation. This data comes asynchronously, so I need to make sure that the right thing is happening in the right thread and that the property also receives NotifyChanged. Crap, when I maximize my window, it does not stretch, as I thought it should be, because its container height is not defined, but I do not want to determine it, so I need to do this parent. Oh, now I really want to make user control from this material here, so I better implement some dependency properties ...

Back and forth I walk, spending hours and days on things that just feel so small and small. In the near future I will begin to reduce the possibilities of usability and improvements in style and style, because it is too long.

Does anyone have any tips or principles that I should try to write WPF effectively?

+7
source share
5 answers

Write your own library or find an existing one.

WPF is great, but out of the box, some things are missing that speed up coding. I was tired of writing the same thing many times, so I created my own library, filled with things like converters, visual tree helpers, attached properties, user controls, etc., And since then my development time has accelerated significantly.

In addition to my own library, I also started using Microsoft Prism and the Galasoft MVVM Light Toolkit . Both contain useful objects that I use all the time, and are better than what I could write on my own. (The most commonly used are NotificationObject from Prism, RelayCommand from MVVM Light Toolkit and EventAggregator or Messenger from one depending on the project, etc.)

Another thing I did to speed up coding time was to use Visual Studio macros. For example, to create a property with a change notification, I write a personal property, Ctrl+E , Ctrl+R , which generates a public version, and then run a macro that automatically sets the PropertyChanged notification in the setter method.

I almost never change the setter methods from the default macro, and instead use the PropertyChanged event to handle any changes that should happen on the installer. This not only facilitates tracking application flow, but also significantly reduces the time it takes to view my public properties to change the setter method.

+1
source

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

  • Use the DockPanel as the default panel for your layout if you have no good reason.
  • Keep a folder with useful classes: the ViewModelBase class, which implements the INotifyPropertyChanged , RelayCommand , etc. class. You do not need to show imagination and try to make this a separate assembly that you create in your project; just write good enough versions and copy them into your new projects.
  • Get Resharper and use it. Use templates to create dependency properties, properties that change notification, and commands.
  • Find or build a good library for asynchronous task management.

I find that even for very simple applications, I work faster with WPF than with Windows Forms. For applications that are not very simple, there is no comparison.

For the most part, WPF applications are a lot of development work, because it’s harder to make the user interface functions cut out. You cannot just say, “Oh, this is impossible,” because it is possible (no matter what it is).

+4
source

I believe that the correct answer is not for WPF at all, but it may correspond to what you are looking for.

In most cases, when you want to use new technology, the time while you are inefficient, productive, and your decisions are not so impressive, but innovative or just not like the others.

Which will give you more efficiency, works with WPF itself.

This is more about project management than programming. After completing the project, your team, and you should go to some room and discuss:

  • Success stories.
  • Problems during development.
  • Advantages and disadvantages.
  • Crash in application architecture.
  • Communication problems in team and client.
  • ... etc.

If everyone shares their knowledge, the project manager or team leader will document the history of each project well, and finally, everyone will have a know-how.

In addition, it is important that you do not need to reinvent the wheel for each new project: if some template worked fine, do the same next time, even if this is not the best way to do it. And try to improve it, if possible.

Design patterns, technologies, paradigms, languages, companies, colleagues, and nothing are a silver bullet: Microsoft has stated that WPF is a step forward in developing Windows client development, and that is: a more modern approach to providing shinny user interfaces and a programming paradigm that fits to today's "desired" approaches, weakening the relationship between coders and designers, since WPF has XAML, which allows not only to separate problems, but also to divide professionals into areas (designers, UI programmers, business programmers ...).

Finally, as I said above, WPF will not be your silver bullet: learn from your own success and read a lot, see application examples, download open source solutions, listen to your colleagues, drink coffee and, after some headaches, someday in the near future you will use these technologies (and many others).

EDIT

I would like to add that a good way to use the know-how is to create a Visual Studio manual, so you can automate many tasks, such as creating managers, views, models, and other things, only in the sense that the team will do it manually.

For example, you can create a package of instructions for a WPF-like application, and you can automate the creation of a module. When you want to add a new module, the instruction package starts a process that adds all the necessary classes to start the development of this new module and can create a form sample that is already associated with a navigation manager, controller or any other (this is just an example).

An instruction package and T4 would be good tools for automating tedious or repetitive tasks in everyday tasks:

+2
source

I have been using WPF since 2008 and I can honestly say to do it right, and the cleanup takes longer than the same as WinForms. I wrote a lot more WPF than Winforms. At the same time - if I need a simple internal utility - it is ALWAYS Winforms. If I have something in the face of the client, it is always WPF. What for? Winforms are cheap and dirty, and you get a lot for free. What you don't get is the suitability and polish that WPF can provide. Flexibility with WPF does come at a cost, but in the long run it is worth it to use user-oriented software.

+1
source

Yes, WPF is a barrier, but it also has rewards. You are on the right track with a design pattern such as MVVM. It seems that you did not even get into the “reward” of dependency properties or event bubbles. But control over the user interface is great. Almost everything is content control. In forms, I always wrote user controls to get the user interface I wanted. In WFP, I never had to write a user control for the user interface and doubted that I would ever be. The syntax is new, but it is compact - I rewrote the Form application in WPF, and WPF has 1/3 lines and other functions. Read the entire WPF book just for the rationale - I like PRO WPF in C # 2010. You can also say that LINQ is complicated, but the person does it with just a few key touches. WPF is not something you just pick on the fly as your next application.

0
source

All Articles