The other day I started moving all my event handlers in my application from the View’s code-behind file to the ViewModel. I accomplished this using command binding. This was all easy enough – until I came to my Close method.
You see, in the MVVM (Model – View – View Model) design pattern, the ViewModel – a class that typically contains the values you want to display in your View – shouldn’t know anything about the View. It’s OK (and quite necessary!) for the View to be aware of the ViewModel, however.
So let’s take a look at the Close button’s event handler as it originally existed in code-behind:
private void Close_Click(object sender, RoutedEventArgs e)
In the Model-View-ViewModel design pattern, we make frequent use of the INotifyPropertyChanged interface. It is the Interface that defines the necessary methods to Notify the UI when a Property has Changed. As you can see, its a very well-named Interface. 🙂
I’ve collected a few variations of the implementation and placed them all into a single ViewModelBase class. I didn’t write any of these myself – rather, I found them strewn about the internet. I’ve documented where I got some of them; others were (and probably still are) readily available from a multitude of sites. I’m making my collection available here, and I’ll give a few examples of how to use it. I’ll also explain the “evolution” my use of this interface has undergone.
To keep things organized, I usually create a folder named MVVM in my solution to house this class. You’ll want to update the namespace in this class to match your project; eventually I’m sure I’ll just compile it into a DLL once its evolution has ceased. Continue reading
One of the issues with multi-threading in WPF is the inability for processes on a separate thread from the user interface to directly access objects in the UI. The .NET Timer object does not run in the same thread as WPF’s UI, so it therefore cannot directly update the UI. Instead, the Timer must post UI updates to the dispatcher of the UI thread, using the Invoke or BeginInvoke methods. However, an easier method exists for implementing a Timer that updates the UI – the DispatcherTimer.
The DispatcherTimer runs on the same thread as the UI. Therefore, using the DispatcherTimer instead of the traditional Timer object allows UI updating without the need for the Invoke or BeginInvoke methods.
The DispatcherTimer works in almost the same way as the traditional Timer:
- Create an instance of the DispatcherTimer
- Assign a TimeSpan to the DispatcherTimer’s Interval property
- Assign an event handler to the DispatcherTimer’s Tick event
- Start the timer
It really is that easy! Let’s take a look at some sample code. All of the following code can be placed in your DataModel. Continue reading
I recently completed an application that made use of the WebClient object’s DownloadString method to obtain a JSON string. In my development environment retrieval of this data often took upwards of 30 seconds. If you’ve ever incorporated a time-consuming process in your application, you may have noticed that your user interface becomes unresponsive while the process is running.
Why does this happen? Because without special consideration, this time-consuming process is running on the same thread that handles updating of the UI. “How do I keep my UI responsive during a time-consuming process?”, you may ask. This can be easily accomplished through the use of the BackgroundWorker object. In this article, we’ll discuss the BackgroundWorker object, some of its available event handlers, and one of its methods. Continue reading
I used to run my website in the Drupal framework. The default theme packaged with that installation used the default Drupal logo (a fierce-looking drop of water), which I used for several months. I concentrated my efforts on other parts of the site – building content, functionality installation, etc.
I’ve since ported my site over to the WordPress framework, and was ready for a little personalization. As I’ve mentioned before, my wife is an artist. I asked her to create a logo for me – check it out! It’s me, hiding behind a laptop emblazoned with my URL.
It’s become my website logo (in my site header), my favicon.ico (the little logo that appears in your browser tab), and my gravatar (a logo that remains consistently me across gravatar-enabled websites).
What do you think?
Earlier today I was raving about the simply elegant plug-in Anti-Spam. When writing that post, it occurred to me that I specified two pieces of information that might change as time passes.
If you’ve read my post on dynamic ages in my About Me page, then it might be obvious that I like to keep things dynamic. That is to say, I don’t like to use static values unless those values are simply a snapshot in time. So I thought I’d add a little functionality to my site that would:
On my wife’s site, we were getting hit from numerous (15+) spam messages a day. We figured this was an acceptable number to just handle manually, but quickly grew tired of taking the time to moderate these comments, marking them as trash.
We tried various CAPTCHA plugins but none were very graceful. From difficult-to-read CAPTCHA strings to stark, unstyled “You have entered an invalid CAPTCHA” message, none of the CAPTCHA plugins we implemented really worked for us.
Then I found this plugin. It’s so simple in its implementation that it’s almost laughable. There’s beauty in what it does, though. It’s not overly complicated, it doesn’t affect the aesthetic of the site AT ALL, and most importantly, it just works.
I’ve had this plugin installed for over 5 years, 6 months and have only received 16 spam comments. I give it five stars all day long. Continue reading
[showCamera width=”300″]I was looking for a way to show my local IP network camera stream in my site (and my wife’s site). Sure, it was easy enough to find the necessary code on Google and embed in a post through the text editor, but I wanted something more.
I can’t expect my wife to write her posts without the Visual editor (which would have butchered the HTML required to show the stream), so I decided to convert the camera-generating HTML code to a PHP function and put it in our sites’ functions.php files. Why would I want to do this?
Recently my brother asked me if my daughters’ ages in my About Me page were static or if they would change as we moved through time. The answer is that they’re the result of PHP functions and are therefore dynamic and will change as time passes.
You see, my site allows me to embed PHP code. In a nutshell, PHP allows me to create HTML dynamically. I wrote a PHP function that calculates the number of years/months (or months/days in certain situations) since a given birthdate. I added this function to my site’s custom functions.php file, and registered it with the WordPress framework so that I can access it with a shortcode. Because I want to be able to use this function multiple times with various birthdates, I wrote it to accept an argument called “birthdate”. In my About Me page, anytime I want somebody’s age, I simply insert:
In an effort to clean up the URLs my WordPress-based site generated, I wanted to implement an ISAPI rewrite. You see, because I host my site on IIS 6, turning on WordPress’ Permalinks required an “/index.php/” in all of my URLs. I wanted a way to eliminate the “/index.php/” portion of the URL. If I were hosting this site on an Apache server, it would be a no-brainer as Apache has the mod_rewrite module available, and would be able to serve URLs without the /index.php/ out of the box. According to http://httpd.apache.org, the mod_rewrite module “uses a rule-based rewriting engine, based on a PCRE regular-expression parser, to rewrite requested URLs on the fly.” It is this module that allows the intricate URLs created by WordPress to be presented in a friendly, cleaner manner – allowing http://jkshay.com/index.php/implementing-an-isapi-rewrite-for-iis to be accessed with a URL such as http://jkshay.com/implementing-an-isapi-rewrite-for-iis-6.