Mobile Zone is brought to you in partnership with:

Jerry Nixon is the Microsoft Developer Evangelist in Colorado. He has scary knowledge about Architecture, XAML, and SQL Server. He has terrifying knowledge about Windows Phone, the Kinect, and C#. And, he has simply ridiculous knowledge on Windows Desktop development in Windows 8. Jerry is a DZone MVB and is not an employee of DZone and has posted 104 posts at DZone. You can read more from them at their website. View Full User Profile

XAML Dev’s Favorite Snippet

12.25.2011
| 4759 views |
  • submit to reddit

Data binding in XAML is powerful. We can wire up forms without code-behind. However, for it to work, data bound objects must implement INotifyPropertyChanged. Without this interface, object property value changes are not reflected in the UI.

Objects implement properties two ways. The first method (use the snippet “prop”) is to simply create the property. We’ve all done it. It’s like this:

image

There is nothing wrong with this. We’ve used it for years. However, it doesn’t support XAML data binding. XAML data binding requires INotifyPropertyChanged. This approach (there is no snippet to use) is to raise an event when the chance occurs, like this:

image

Because we raise the event in the setter, we can’t use simplified syntax. And because there is so much typing, I am annoyed every time I write one. Why is there no snippet for this? It doesn’t matter. Let’s just make one!

Step 1 Create a New XML File

Visual Studio conveniently creates files without requiring you to save them first.

image

Step 2 COPY the Snippet Template

Give the XML file focus. Insert a snippet. The keyboard shortcut is CTRL+K, CTRL+B. From the menu select “Snippets” and “Snippet”. This will inject the snippet template.

image


You can update the orange areas and create your snippet.  For more information on creating your own snippet, look here. This is the basic template:

image


But I want to save you time. So, I write the snippet for you. You just need to copy it into your file. But, notice <Code Language=”CSharp”> This tells Visual Studio where to make the snippet available. If you make your own, remember to update this value.

Here is my snippet for you:

image
image
image

Get the real code here.

Step 3 Add the Snippet to Visual Studio

Once you have the code copied the snippet, save the file with a .snippet extension. Then use (CTRL+K, CTRL+B) to open the Snippet Manager.

image


  1. Change the Language to Visual C#
  2. Click Import and find your file
  3. Click Open and Finish
Congratulations

You now have the snippet and are ready to use it. To use your new snippet:

  1. Go to a C# file
  2. Type “propnote
  3. Type TAB+TAB
  4. Fill in the blanks, like this:

image


Source:  http://blog.jerrynixon.com/2011/11/xaml-devs-favorite-snippet.html


Published at DZone with permission of Jerry Nixon, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Robert Craft replied on Thu, 2012/01/26 - 5:50am

XAML is a declarative markup language. As applied to the .NET Framework programming model, XAML simplifies creating a UI for a .NET Framework application. You can create visible UI elements in the declarative XAML markup, and then separate the UI definition from the run-time logic by using code-behind files, joined to the markup through partial class definitions. XAML directly represents the instantiation of objects in a specific set of backing types defined in assemblies. This is unlike most other markup languages, which are typically an interpreted language without such a direct tie to a backing type system. XAML enables a workflow where separate parties can work on the UI and the logic of an application, using potentially different tools. Your article is very helpful to understand some basic concepts. I like it.

Java Collection

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.