Chapter 12. Resources

One of the most important concepts in any kind of programming is code reuse. Subroutines, functions, scripts, classes, inheritance, loops, and many other programming constructs let you reuse code in one way or another. For example, if your program needs to perform the same task in many places, you can create a subroutine that performs the task and then call it from those places.

This kind of code reuse has several advantages, including:

  • You only need to write and debug the code once.

  • If you need to modify the code later to make changes or fix a bug, you only need to do it in one place.

  • When you fix bugs or make other changes to the code, you don't need to worry about keeping the changes in multiple parts of the code in synch.

  • If you need to do something similar in another program, you may be able to copy the routine that performs the action.

  • You need to write less code, which makes the program easier to read and more reliable. That in turn reduces cost.

Just as routines and functions let you reuse code in languages such as C# and Visual Basic, resources let you reuse XAML code. They let you define values that you can then use from many places in a XAML file.

Because XAML files are graphical, resources tend also to be graphical. They define objects and values that represent control properties that you can then apply to many controls to give them similar appearance or behavior.

This chapter describes resources and explains how you can define them and refer to them in ...

Get WPF Programmer's Reference: Windows Presentation Foundation with C# 2010 and .NET 4 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.