Click here to Skip to main content
15,881,797 members
Articles / Desktop Programming / WPF
Alternative
Article

WPF Loading Wait Adorner

Rate me:
Please Sign up or sign in to vote.
4.89/5 (10 votes)
5 Dec 2016CPOL1 min read 17.5K   612   11   14
This is an alternative for "WPF Loading Wait Adorner"

Introduction

I was looking for a control to display a wait condition, and discovered https://www.codeproject.com/Articles/57984/WPF-Loading-Wait-Adorner by The control provided the basic capabilities I was looking for, but I found a few things that could be improved upon. The initial problem I had was that the sample used code behind, and I thought that I would have to create a DependencyProperty, but he actaully had created a DependencyProperty, he just did not use this capability in the sample, Hard to understand how to use a Control iwth the MVVM design pattern if you do not show use of the DependencyProperty in the sample. Another serious problem was the naming for the Control and its content.

Changes

  • In the sample, binding is not used to make the control visible. I updated the sample so that it uses binding.
  • Renamed the wait adorner (AdornedControl) to WaitAdorner and the LoadingWait UserControl to RotatingDotsWaitAdorner. I felt these were more descriptive names.
  • In the sample included a ToggleButton in AdornerContent for the WaitAdorner which will clear the wait. The IsChecked property is is bound to an IsWaiting property in a ViewModel that is also bound to a ToggleButton to initialize the wait, and the IsAdornerVisible property fo the WaitAdorner.
  • The "Start/Stop Waiting" ToggleButton is renamed "Start Waiting" and included within the WaitAdorner. The way to stop the waiting is the "Cancel" ToggleButton in the AdornerContent. ToggleButton controls are used to simplify the ViewModel. In actuality would probably use a Button in the AdornerContent, and use a Binding to an ICommand.
  • Did some refactoring, and included use of features available in C# 6.0.

History

2016-12-05: Initial Version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Clifford Nelson Consulting
United States United States
Has been working as a C# developer on contract for the last several years, including 3 years at Microsoft. Previously worked with Visual Basic and Microsoft Access VBA, and have developed code for Word, Excel and Outlook. Started working with WPF in 2007 when part of the Microsoft WPF team. For the last eight years has been working primarily as a senior WPF/C# and Silverlight/C# developer. Currently working as WPF developer with BioNano Genomics in San Diego, CA redesigning their UI for their camera system. he can be reached at qck1@hotmail.com.

Comments and Discussions

 
GeneralMy vote of 1 Pin
VEMS6-Dec-16 15:04
VEMS6-Dec-16 15:04 
AnswerRe: My vote of 1 Pin
Clifford Nelson8-Dec-16 8:18
Clifford Nelson8-Dec-16 8:18 
GeneralRe: My vote of 1 Pin
VEMS8-Dec-16 9:48
VEMS8-Dec-16 9:48 
GeneralRe: My vote of 1 Pin
WPF Programmer9-Dec-16 10:38
WPF Programmer9-Dec-16 10:38 
GeneralRe: My vote of 1 Pin
VEMS9-Dec-16 11:29
VEMS9-Dec-16 11:29 
AnswerRe: My vote of 1 Pin
WPF Programmer12-Dec-16 5:17
WPF Programmer12-Dec-16 5:17 
To Quote:

In MVC, you have a triangular relationship between the components. That is: The Controller owns the View and the Model. The View relies on the definition of the Model. The Model needs to fulfill the requirements of the View. Think of a Hub (controller) and spoke architecture (view and model)
In MVVM, think of that triangle flattening out with each component only knowing about one other in the chain. That is: View->ViewModel->Model
The Model is unaware of anything up the stack. The ViewModel is only aware of the Model The View is only aware of the View Model - it is unaware of the Model.
Why is this important?
This is the core of the original question.
The main purpose is further abstraction of your architecture. This will typically lead to a bit more code, but fewer points of contact between objects. Fewer points of contact are important because this leads to more agile code. The more coupling/contact Class A has with Class B, the more impact a change in Class A will have. Reducing the impact of change is one of the key benefits of good architecture.
To fully understand this, it's helpful to ponder about what the components really represent. What is a View, a Controller, a ViewModel, or a Model? Are they literal definitions, or more of an abstract concept?
In my experience, it has been more beneficial to consider the Model to be a cluster of classes/objects which deal with the construction and persistence of data. It's not just a plain-old-object with properties. It's a class which performs data fetches, data saves, a factory which constructs plain-old-objects. It's a facade layer which provides a clear API into the data. Should this facade layer be referenced directly from the View?
In my opinion, it should not. In MVC, that answer is also "no". The Controller fetches data from the Model. In that regard, MVC and MVVM achieve the same goal. Where the two architectures differ is how the data and the view are linked.
Like the Model, the View can be a collection of classes which in coordination with one another, render a presentation view. This could consist of a View Controller + View in the case of mobile platforms (View Controller on iOS, Activity on Android). In a lot of cases, you need a class to load a view document into memory and update view properties. There's a lot of work to do here. In MVC, the Controller quickly becomes a 'kitchen sink' class - a sort of dumping grounds for anything related to the current user context.
When you multiply this over dozens of potential views within your application, you end up with a lot of deep dependancies between your back-end Model code and your front-end View code. With large Controller classes, these dependencies aren't immediately apparent.
Flattening out your dependencies
MVVM flattens out the dependencies. This creates focus. What is focus? The ability to work on a single piece of functionality without the distraction of all other dependencies. Now you can start writing unit tests on code that was previously deemed untestable.
The View Model acts as a facade between the View and the Model. The View Model caters to the needs of the View - technically the View should own the View Model. If the View requires data from multiple sources, the View Model encapsulates the composition of separate data sources into a single, unified, de-normalized object. If the view needs to call back into the Model or other destinations, the View Model provides hooks and routes the appropriate call.
Consider how a networking patch-panel works. At first glance, this seems redundant - why not simply wire your ethernet from point A to point B. But with experience, you'll understand that a patch panel provides you with a key piece of abstraction which allows you to alter the routes of Point B without affecting Point A. This is what your View Model is doing.
Now that you have a clean abstraction between your View and Model, the consequence should be that your View/Controller is only concerned with presentation. This means it shouldn't be dealing with localization or formatting - it gets data and presents data. Your View Model is an ideal place to put these sort of pre-view data massaging. Let's say you need to filter data based on a criteria. Again, the View Model is knowledgable about the Model data (your View is not) and is a great place to put this sort of code.
Once you start organizing your application requirements in this fashion, your View/Controller code becomes cleaner, and when something needs to change, the implications are more obvious, which leads to fewer bugs.
Testability
One final note on testability: By flattening out dependencies, it makes it easier to inject mock dependencies into your tests. It makes testing easier and more concise. Your View Model becomes something that you can define clear test cases against.
GeneralRe: My vote of 1 Pin
VEMS12-Dec-16 5:31
VEMS12-Dec-16 5:31 
AnswerRe: My vote of 1 Pin
Clifford Nelson13-Dec-16 11:39
Clifford Nelson13-Dec-16 11:39 
GeneralRe: My vote of 1 Pin
VEMS12-Dec-16 5:32
VEMS12-Dec-16 5:32 
AnswerRe: My vote of 1 Pin
Clifford Nelson13-Dec-16 7:05
Clifford Nelson13-Dec-16 7:05 
GeneralRe: My vote of 1 Pin
VEMS13-Dec-16 7:11
VEMS13-Dec-16 7:11 
GeneralRe: My vote of 1 Pin
Clifford Nelson13-Dec-16 11:08
Clifford Nelson13-Dec-16 11:08 
GeneralRe: My vote of 1 Pin
VEMS13-Dec-16 12:13
VEMS13-Dec-16 12:13 
AnswerRe: My vote of 1 Pin
Clifford Nelson14-Dec-16 5:08
Clifford Nelson14-Dec-16 5:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.