Monthly Archives: July 2013

Overview of Transact-SQL (T-SQL)

Transact-SQL is the SQL Server implementation of SQL, a standard codified
by the American National Standards Institute (ANSI) and also adopted by the
International Organization for Standardization (ISO). No single vendor has
fully implemented every part of the ANSI/ISO standard, and each vendor has
added its own proprietary extensions to the language, so you’ll find plenty of
things in Transact-SQL (T-SQL) that you won’t find in other database
products. The standard is updated roughly every three or four years, and so is a
moving target that no SQL product ever fully implements.

The SQL language came about as a result of the work that Dr. E. F. Codd did
in the 1960s on his Relational Database Model. The first version of the
language was known as SEQUEL. It was then completely rewritten in the
seventies, and eventually became known as SQL for Structured Query
Language, because it turned out that the acronym SEQUEL had already been
trademarked. The original “sequel” pronunciation has stuck to this day–SQL
Server is still widely referred to as sequel server, although some purists insist
that the language name should be pronounced ess-que-ell. However you
pronounce it, the SQL standard has been relatively well received and is the
most widely supported standard today.

Transact-SQL (T-SQL)

Transact-SQL is Microsoft’s implementation of the SQL language in SQL
Server. The language implements a significant subset of the features of
standard SQL, as well as some very useful extensions to the ANSI standard
that add procedural capabilities, which makes Transact-SQL more like a
programming language. There are control-of-flow features, such as IF…ELSE
syntax and WHILE loops, as well as support for variables, parameters, and
user-defined functions. Like other programming languages, Transact-SQL
supports built-in functions for manipulating strings, numbers, and date/time
information, and for returning system information.

Although Transact-SQL has programming language features, you’d never want
to use Transact-SQL to replace a general purpose programming language. It

has no user interface and its programming constructs are very limited. The
main advantage to programming in Transact-SQL is that your routines execute
on the server and are highly efficient for set-based data operations. Transact-
SQL provides the building blocks for all your views, stored procedures, userdefined
functions, and triggers. When you perform as much processing as
possible in Transact-SQL, performance improves because less data has to
traverse the network for processing on the client.

T-SQL has two broad sets of statement types: data definition language (DDL)
and data manipulation language (DML) statements. DDL statements let you
create and update database and server objects, while DML statements let you
work with the data itself to create, retrieve, update, and delete rows of data.
These four operations are so common in relational databases that they are often
collectively referred to as CRUD operations.

Microsoft adds a number of new T-SQL features in every new version of SQL
Server, some of which are proprietary to that product, while others are
implementations of the features in the SQL standard. This chapter focuses on
the fundamentals of the T-SQL language.

NOTE

SQL Server hosts the .NET Common Language Runtime (CLR),
which allows you to write code in any supported .NET language,
such as C# or Visual Basic. You can use Visual Studio to write
programs that are then compiled into assemblies and loaded into
SQL Server. CLR procedures are designed for code that is
processor-intensive, such as complex mathematical or string
manipulation. The CLR is not designed to replace Transact-SQL,
which is always the best choice for data access and for set-based
operations. Any time a CLR procedure performs data access, it
uses Transact-SQL “under the covers.”

ldn-expertdkielyThis post is an excerpt from the online courseware for our SQL Server 2012: Introduction to T-SQL course written by expert Don Kiely.

Don Kiely is a featured instructor on many of our SQL Server and Visual Studio courses. He is a nationally recognized author, instructor and consultant who travels the country sharing his expertise in SQL Server and security.

Using Code-Focused Rapid Application Development (RAD)

There are many visual designers in Visual Studio that allow you to drag-and-drop
controls onto the screen, in what is often referred to as Rapid Application
Development (RAD). However, most applications of any significance require
code that defines the logic of that application. Visual Studio recognizes this by
supporting Code-Focused RAD—tools that help you write code faster. This
section shows a few of the ways you can navigate code, and how to add
snippets and refactor code.

Navigating Code

Visual Studio has several features supporting code navigation, including Call

Hierarchy, Navigate To, Highlighting References, and Live Semantic Errors.
The last two are not really navigation features, but they do help you understand
code better.

Call Hierarchy

Call Hierarchy allows you to view callers and callees to a type member in your
code. You can open the Call Hierarchy window (see Figure 8) by selecting the
type member name you’re interested in and pressing Ctrl+K+T.

RAD

Call Hierarchy lets you view callers and callees to a type member.

The Call Hierarchy window also lets you navigate through code. To navigate
to a specific location, double-click on that location in the Call Hierarchy
window. You can open the branches of the tree by clicking an arrow to view
either Calls To or Calls From the location of the arrow.

Navigate To

Navigate To lets you quickly move to anywhere in your code by typing a
search term. Type Ctrl+, (comma) to display the Navigate To window
shown in the Figure below.

RAD1

The Figure above shows how Navigate To lets you search and move to code quickly.

After the Navigate To window appears, you can type a Search term and all the
matching members will appear in the Results window. You can double-click
any item, or select an item and click the OK button, and Visual Studio will
take you to the right location.

Highlighting References

The Highlighting References feature allows you to select an identifier and
Visual Studio highlights all the references to that identifier. Figure below shows
how this works.

RAD2

The Figure above. Highlight References shows the selected identifier and references.

In the Figure above, we selected the method identifier, Level2. Visual Studio
highlighted the reference to that identifier inside the Main method.

Live Semantic Errors

Live semantic errors show you problems with your code as you type. You can
see compiler errors and warnings without building your code. Figure 11 shows
what a Live Semantic Error looks like.

RAD3

The Figure above. Live Semantic Errors show you problems as you type.

The example in the Figure above is missing an end quote, which is a compiler error.
The Live Semantic Error appears as a squiggly line under the location of the
error. For warnings, you receive a green squiggly line.


JoeMayoThis post is an excerpt from the online courseware for our C# 2012: Visual Studio 2012 IDE
course written by expert Joe Mayo.

Joe Mayo is an author, independent consultant, and instructor specializing in Microsoft .NET and Windows 8 technology. He has several years of software development experience and has worked with .NET since July 2000.

Introduction to Views

You can use a SQL SELECT statement to read data from one or more tables in
a relational database. This is such a common operation that SQL provides a
way to save a SELECT statement as a persistent database object, called a view.
A view is simply a saved SELECT statement, which you can also think of as a
virtual table because the view object is used like a table. The view does not
create a copy of the underlying data. Instead, when you use a view SQL Server
dynamically reads that data from the underlying tables. A view can select data
from a single table or multiple tables, and can even span multiple databases
and servers.

You can query views the same way you query tables – you SELECT from a
view and get a result back. You can also perform updates against views,
although with some limitations that you’ll learn about later in this chapter. You
can also create nested viewsviews that contain other views.
A view is not a stored procedure, even though there are some similarities. Both
can return a result set, but a view has no parameters and there is no procedural
logic, just a SELECT statement, although the statement can be as complicated
as you care to make it. Nor does a view have all the features of a user-defined
function.

For a feature that is so simple in concept, views pack a lot of power. Let’s dive
into them and see how they can provide a handy way to access data. You’ll
also learn abou

t their limitations, to help you decide when it is better to use
SELECT statements directly. Let’s start with a discussion about the advantages
of views and quickly look at the security issues of views.

Advantages of Views

You can use views to provide data to client applications. Some of the
advantages include:

  • You can give Columns in views friendly names to simplify databasestructures so that users can easily query them.
  • You can use views to customize data with different filters and sorts for different users or different applications.
  • The end user or client application doesn’t have to know anything about the complexity, join syntax, or aggregates in the query underlying the view.
  • You can omit unnecessary or sensitive data from the view. In other words, you don’t have to include all the columns in the view results from the tables that comprise the view’s source data.
  • Views can provide a backward compatible interface to emulate a table that no longer exists or that has had its schema modified.
  • You can export data to another application using a view.
  • You can use views to partition data based on whatever criteria you specify.>

A view is the sort of database object that, once you understand how they work
and the difference between them and SELECT statements, you’ll start finding
a wide variety of ways to put views to use.

Views and Security

One of the major reasons to use a view instead of executing SELECT
statements directly is to avoid granting permissions to users on the underlying
tables and other database objects. You can grant a user or role permission on a
view alone, thereby limiting a user’s access to the raw data. As long as the
owner of the view and the underlying tables are the same principal, the user
will have access to the data even without explicit rights to the underlying data.

Another way to describe this is that you can revoke all permissions to tables

and grant permissions on views that select only certain columns or rows from
those tables. Users will then be able to work with the views based on the
permissions granted to the views. This enables more granular control over
exposure of your data, letting you limit data modification so that it conforms to
the view criteria.

ldn-expertdkiely

This post is an excerpt from the online courseware for our SQL 2012 Developer: Views course written by expert Don Kiely.

Don Kiely is a featured instructor on many of our SQL Server and Visual Studio courses. He is a nationally recognized author, instructor and consultant who travels the country sharing his expertise in SQL Server and security.

Adding Toast Launch Parameters

When users click or tap your toast notification, Windows loads your
application, and users expect your application to have information about the
context of the notification and display the appropriate information. For
example, if users see a notification that a new email has arrived, they expect
that clicking the notification will display the new email message.

To make this happen, you can and should specify the Launch property of the
toast content, when you create the toast notification. Doing this allows
Windows to pass the value of the Launch property to the event argument for
the application’s OnLaunched event, in the argument’s Arguments property.

What happens next? That’s up to you. Retrieve the Arguments property of the
OnLaunched event’s parameter, and take specific action based on the value.
You can simply pass the value along, or act on it. The sample that follows adds
a little extra code so that if the Arguments property of the parameter to the
OnLaunched event isn’t null, it passes the value along to MainPage.xaml as it
loads it. Code in the page’s OnNavigatedTo event displays the parameter it
was passed.

NOTE

If the toast notification doesn’t have its Launch property set, and if
the app is already running, Windows won’t fire the OnLaunched
event. The event always occurs if the app isn’t currently running.

Try It Out!

Follow these steps to demonstrate passing parameters from the toast to a
running application.

1. In Visual Studio, verify that you have the same sample project loaded as in
the previous demonstration, XMLToastDemo. (If you skipped that
exercise, you can start with the project in the XMLToastDemo_ex07
folder.)

2. In the Solution Explorer window, double-click MainPage.xaml.

3. In the StackPanel element, immediately before the closing element, add
the following markup:

 

Style=”{StaticResource ItemTextStyle}”/>

4. In the Solution Explorer window, right-click App.xaml and select View
Code from the context menu.

5. In the OnLaunched override, note the LaunchActivatedEventArgs
parameter—this parameter contains an Arguments property that contains
the Launch property specified in the toast notification.

6. In the OnLaunched override, immediately above the code that calls
Windows.CurrentActivate, add the following code:

7. In MainPage.xaml.cs, in the OnNavigatedTo override, add the following
code:

8. In the CreateScheduledNotification method, immediately below the code
that sets the Alt property of the image, add this code:

9. Save and run the sample.

10. Add a scheduled notification, and then return to Visual Studio and select
DEBUG|Stop Debugging (so the app is no longer running).

11. Wait for the notification, and then click it. This should load the
application, and display the parameter value (Sample parameter) in the
text block at the bottom of the controls.

12. Press ALT+F4 to shut down the application.

NOTE

This technique works if the application is still running, as well. It’s just slightly more interesting if it’s not running when the notification appears.

ldn-expertkgetz

This post is an excerpt from the online courseware for ourWindows 8 Using XAML: Views, Resources, and Toastscourse written by expert Ken Getz.

Ken Getz is a Visual Studio expert with over 25 years of experience as a successful developer and consultant. He is a nationally recognized author and speaker, as well as a featured instructor for LearnNowOnline.

Scheduling Toast Notifications

You can schedule a toast to appear any time in the future—you supply a time
at which you want the toast notification to appear, and it shows up on demand.
Your application doesn’t even have to be running! In order to better manage
your toasts, however, you should set the Id property of the toast to a string of
up to 16 characters. Having an ID allows you to refer to the toast (and possibly
cancel it).

Note that all the previous examples have used a ToastNotifier instance, but
haven’t stored a reference to it for later use. The ToastNotifier class provides
several useful methods, and if you plan to work with toast notifications after
they’re created, you will need to create a specific reference to a ToastNotifier
instance to work with. Specifically, the ToastNotifier class provides these
members:

  • AddToSchedule, RemoveFromSchedule
  • Show (which you previously called), Hide
  • GetScheduledToastNotifications

To create a scheduled notification, start by creating an instance of the
ScheduledToastNotification class. To its constructor, pass the XML content of
a ToastContent instance—you can use the GetXml method to retrieve the
XML content. Next, create the ToastNotifier instance, and call its
AddToSchedule method, supplying the ScheduledToastNotification instance as
a parameter. In addition, supply the time at which you want to display the
notification.

NOTE

You cannot schedule more than 4096 notifications. Also note that
you cannot schedule a notification in the past (in other words, you
can’t break the inviolable laws of the space-time continuum).

Creating a Recurring Notification
To create a recurring notification, follow the exact same steps as for a
scheduled notification, except that you must also specify the snooze interval
(that is, how long to wait before reattempting the notification), which can be a
value between 60 seconds and 60 minutes. Also, specify the maximum occurrences
of the notification (a value between 1 and 5).

TIP: Specify an Id property when creating scheduled and/or recurring toast
notifications. Doing so will make it easier to identify notifications when
reviewing or cancelling them.

ldn-expertkgetz

This post is an excerpt from the online courseware for ourWindows 8 Using XAML: Views, Resources, and Toastscourse written by expert Ken Getz.

Ken Getz is a Visual Studio expert with over 25 years of experience as a successful developer and consultant. He is a nationally recognized author and speaker, as well as a featured instructor for LearnNowOnline.

Investigating Toast Notifications

Toast notifications are simple notifications that appear in the upper-right
corner of the screen (upper-left corner for right-to-left languages); when they
appear, they play a default sound. Toast notifications allow apps to
communicate with users even if the user is using another app, on the Start
screen, or on the Windows desktop. Actually, toast notifications are most
useful when the user isn’t working with the app that raised the notifications—
clicking the toast redirects the user back to the app that raised the notification, and optionally (and really should) take the user to the specific part of the
application that needs attention. Toast notifications can originate from a
Windows Store app, from a desktop app, or from the cloud.

Investigating Toast Notifications

Toast notifications can contain text and, optionally, an image. Clicking the
toast returns you to the calling application. You cannot add any other user
interface elements (like buttons)—you can include text and maybe an image,
and that’s it!

Your toast notifications can play a sound as they display, and you can select
the sound from a list of available Windows sounds. The toast appears in the
upper-right corner of the screen, and can be activated, dismissed, or ignored by
the user.

If the user clicks or taps on the toast, Windows launches the associated
application. Users expect that the current view in the newly launched app
corresponds with the information on the toast—if the toast indicates a new
email message, for example, users expect that tapping the toast notification
displays the application with the new email message selected. To make this
possible, you can specify context information in the toast to pass through to the
application to set the context for the newly launched application.

Think of it this way: Toast notifications provide the only means of interrupting
the user’s activity in one app with information from another.

You should only use toast notifications for information that’s of high interest
to the user. Perhaps your application requires some sort of user opt-in: look at
a new email message, or respond to an IM message? These are both common
uses for toast notifications. Remember: because of settings, or because the user
is busy, the user may never see your toast notification.

Different Types of Toast Notifications

Windows 8 provides two distinct types of toasts. The first, the standard toast
notification, remains on the screen for seven seconds, and then disappears.
(The user can click or tap the notification to navigate back to the original app
during the time it’s on screen.) The toast displays a brief sound to alert the
user, although you can alter the sound, or play nothing at all. This type of toast
is best for things like a social media update, or an IM sign-in. Given the two
options, this is the type of toast you should use in most situations. (This is the
default toast type, as well.)

The second type of toast is the long-duration toast. It looks the same as the
standard toast, but it remains on the screen for 25 seconds, unless the user
dismisses it. You can optionally make the long-duration toast stay on screen
longer, and you can loop audio. Imagine that your application connects a VOIP
call, and you need to alert the user that there’s an incoming call. You need the
toast notification to continue ringing, and stay visible, until the caller hangs
up—that may take longer than 25 seconds. Use the long-duration toast
notification when you want to grab the user’s attention because a human is
waiting for the user’s attention.

In addition to the two basic toast types, you can also create scheduled and
recurring toast notifications. Toast notifications can be scheduled to appear at a
specific time, and this usage is helpful for alarms, calendar reminders, and
precise timing-based notifications. You can also display the toast notification
multiple times (although this usage can be irritating)—this increases the user’s
chance of seeing the toast notification. Scheduled toast notifications require
you to specify the date and time at which the toast should appear.



ldn-expertkgetzKen Getz is a Visual Studio expert with over 25 years of experience as a successful developer and consultant. He is a nationally recognized author and speaker, as well as a featured instructor for LearnNowOnline. This post is an excerpt from the online courseware for our Windows 8 Using XAML: Views, Resources, and Toastscourse written by expert Ken Getz.

Working with Image Resources

Although you cannot store images in resource files, they too represent
resources in your application. If you have images that contain text, you can
treat each localized image like a localized resource file, using the same folder
structure and file-naming scheme as you learned about for resource files. The
standard convention for localized images, however, is to place the “default” set
of images in the root Images folder, with localized images stored in subfolders
named for the corresponding language (es-mx, for example, or just es for all
Spanish-speaking locales).

But this isn’t the issue you’ll focus on in this section. Instead, this section
focuses on another issue facing developers when images make up part of the
application: image scaling. Your application must handle appropriately scaled
images for any screen resolution that Windows 8 supports. Visual Studio,
along with the Windows Runtime, makes this extremely easy, but you have to
know the tricks involved.

Most often, you will need to provide multiple versions of images to correctly
support different screen resolutions, which generally range from 1024×768 all
the way up to 2560×1440 or higher. (Imagine Windows 8 spread across four or
more large monitors, in a wall display!) An image that looks fine on a small
tablet will certainly not scale up to look decent at a super-high resolution.

This is just a fact of digital life: Without some sort of image scaling, as the
pixel density of the display device increases, the physical size of images seems
smaller. Windows scales the UI to match the size of the screen, and the scaling
is performed in known increments.

The Figure below, from the Microsoft documentation, demonstrates how different
screen sizes, and different pixels per inch (PPI) display. Clearly, the higherresolution
slate, at the same physical size, displays more pixels per inch and
therefore provides a clearer image.

PixelDensity

The Figure above. Compare pixel density on two displays of the same size.

Because of the differences in screen resolution and pixels per inch, Windows
scales every application for the current screen and its properties. In order to
ensure that images look their best at every screen resolution, you should
always provide three copies of every image in your application, sized three
ways:

  • 100%, when no scaling applied.
  • 140%, for 1920×1080 devices with a minimum dots per inch (DPI) of 174.
  • 180%, for 2560×1440 devices with a minimum DPI of 240.

Yes, this adds a great deal of work for you, but it ensures that your images
don’t appear distorted when Windows scales your applications.


ldn-expertkgetzThis post is an excerpt from the online courseware for ourWindows 8 Using XAML: Views, Resources, and Toastscourse written by expert Ken Getz.

Ken Getz is a Visual Studio expert with over 25 years of experience as a successful developer and consultant. He is a nationally recognized author and speaker, as well as a featured instructor for LearnNowOnline.

Reacting to a Language Change

Although it won’t happen every day, your application may need to react to the
situation in which a user changes the currently active language in Windows,
while your application is running. Although it’s possible to handle the event
that occurs in this situation, it’s important to note that the technique you’ll see
here allows you to programmatically update text—it won’t automatically
refresh user interface elements to which you have bound localized text using
the x:Uid property. (There may be a simple way to make this work, but the
Microsoft documentation certainly doesn’t mention it!)

Your job, then, involves reacting to an event that your application handles in
reaction to the user language changing. In the event handler, your code must
update all the user interface elements that contain localized text.

As mentioned earlier, Windows provides two means of accessing resources
programmatically. You can use the ResourceLoader class, as you have done previously; or you can use the ResourceManager class, which provides more functionality at the “cost” of being more difficult to use. You can certainly use the ResourceManager class to do everything you’ve done so far (for which you previously used the ResourceLoader class).

In order to retrieve a reference to the resource manager for the currently
running application, use the ResourceManager.Current property. You will also need to interact with the ResourceContext class, which encapsulates all the factors that might affect resource selection (such as the selected language, for example). The collection of factors that affect resource selection is an instance of the ResourceQualifiers class, and a ResourceContext’s QualifierValues property retrieves a ResourceQualifiers collection containing information about the context’s resource qualifiers.

The QualifierValues property of a ResourceContext object returns a collection
of type IObservableMap<K,V>, where K represents the type of the key, and V represents the type of values in the collection. This class raises its MapChanged event when any of its members changes, and this occurs when
the language changes. (Remember, the qualifiers represent all the characteristics that help the ResourceManager instance figure out which string to load when you supply a resource name, and the current language is certainly one of those qualifiers!)

Given all this information, your code must trap the MapChanged event of the
QualifierValues property of the ResourceContext object provided by the
DefaultContext property of the current ResourceManager. In other words, you
must handle this event:

That’s quite a mouthful! And, of course, there’s a wrinkle. There’s always a wrinkle. The MapChanged event occurs on a thread other than the user interface thread, and updating the user interface from a separate thread isn’t allowed. This certainly complicates updating the text on the page! You must ensure that the code that updates the page is called from the page’s thread.

In order to ensure the correct thread switch, your code should call the current
page’s Dispatcher.RunAsync method, which guarantees a thread switch to the correct thread. Of course, when using the RunAsync method, you must include the await keyword in the call; when you use the await keyword in the method call, you must modify the procedure declaration to include the async keyword (so the compiler knows to handle this procedure specially).

In addition, you can specify the priority for the asynchronous call to the code that updates the page’s text: The sample uses the CoreDispatcherPriority.Normal parameter in the call to the RunAsync method.

After adding the event handling code, you should call a procedure that you
create to update the text on the current page. Remember that your code runs,
allowing you to manually update text on the screen, but no updates occur
automatically—“bound” resources won’t update.

ldn-expertkgetz

This post is an excerpt from the online courseware for ourWindows 8 Using XAML: Views, Resources, and Toastscourse written by expert Ken Getz.

Ken Getz is a Visual Studio expert with over 25 years of experience as a successful developer and consultant. He is a nationally recognized author and speaker, as well as a featured instructor for LearnNowOnline.

Working with String Resources

Although lots of developers do it, it’s never a good idea to embed string resources directly into your application’s source code. Sure, it makes it easy to get the application written quickly, but extracting all the text of your application into a shared resource file makes it far easier to maintain the text over time, and hard-coding text in the application makes it nearly impossible to translate the application for multiple cultures.

The more experienced developer will, instead of hard-coding text in an application, extract the strings into a resource file, creating a single resource corresponding to each piece of translatable text in the application. Using string resources allows you to replace text with culturally-sensitive content, so that Windows can load text specific to the current or selected locale.

Therein lies the beauty of working with resources: Windows itself takes care of loading the appropriate resource information at runtime, assuming that the content is labeled correctly. As you’ll see as you work through the examples in this chapter, it’s up to you to appropriately label the resources, so that Windows can load the correct language resources at runtime. It’s also up to you to designate a default language (specified in the Package.appxmanifest file) so that Windows can have a “fallback” language, in case you haven’t supplied resources for the current user language.

In .NET Framework apps (as opposed to Windows Store apps), developers generally store resources using a “hub and spoke” topology. That is, the main application contains the default, “fallback” language info, and satellite assemblies contain only resources, with one language per codeless assembly.

Windows Store applications use a single resource file, called a Package Resource Index (or PRI) file. This PRI file stores the resources for all languages, cultures, and scaling factors (including images that need to be scaled for different screen resolutions, another use for resources). What if the Windows Runtime can’t find a particular resource for a particular language in the PRI file? In this case, Windows provides fallback rules that determine which resource to load that best fits the requirements.

Creating Resources

The techniques for creating and naming resources have changed between .NET Framework applications and Windows Store applications. In a desktop application, created using the .NET Framework, you place resources in either text or XML (.resx) files. You use the Resgen.exe application to compile the resources into binary format, and the .NET compiler embeds resources from the default/neutral culture into the main assembly. You use the Assembly Linker (AL.exe) to embed other resources into satellite assemblies. At runtime, you retrieve resources using the ResourceManager class.

In Windows Store applications, you create .resw files that contain the resources. These files are identical in format to the old .resx files, except that they contain only strings and file paths. You can create as many of these files as you need, for various languages that your application supports. At compile time, the compiler packs all the .resw files into a single PRI file using the MakePRI utility, and the PRI file is included with your app’s deployment package. At runtime, you can use either of two classes to work with your resources programmatically (examples later in this chapter use both of these classes):

  • For more complex activities, use the Windows.ApplicationModel. Resources.Core.ResourceManager class.

You’ll find it easiest to use Visual Studio 2012’s resource editor to create resources. This editor provides two advantages:

  • There’s no need to create the resource file manually, and to ensure that it contains valid XML content.
  • The resource editor handles the process of calling the MakePRI utility to compile the resource file, pack it into a PRI file, and include it in the deployment package.

In other words, use the editor. You’ll be glad you did.

Naming Resources

Windows supports a broad range of naming techniques for resources, to ensure that it can load the appropriate resources at runtime. In this chapter, you’ll investigate a few of the naming standards that work with Windows, although the support is actually broader than you’ll see here.

Selecting the appropriate resource at runtime is all about setting an appropriate file name at design time. Although a complete discussion of language and locale support is far beyond the scope of this chapter and its intended coverage of resource usage, it is important to understand how Windows handles language and locale naming. Microsoft provides a complete list of standard names for languages, and you’ll find that list here: http://go.appdev.com/?id=8AFG (see Figure 1). If you study the list carefully, you’ll see that for every language, the list provides a two-letter code representing that language (ar for Arabic, bg for Bulgarian, zh for Chinese, and so on, including en for English). In addition, each language possibly supports multiple specific locales, represented by a two-letter code after a dash (you can probably use your own memory of social studies to guess at the exact meaning of some of the codes—for example, ar-sa would seem to support Arabic as spoken in Saudi Arabia, while ar-eg would seem to support Arabic as spoken in Egypt.)

Figure above. The list of supported languages. When specifying a language, you can either select the two-letter name, or a full name as listed on this page.

When creating resources, start by creating a folder in your project named Strings. Inside that folder, create a folder for each language’s resources, using the language name retrieved from the Microsoft list. For example, in the US, the default language is generally en-us, so your application could contain a folder named Strings and a subfolder named en-us. Then, when it’s time to describe the particular set of resources you want to use, you can specify the resource using the culture name followed by the resource name and file extension, as in en-usResources.resw.

You also have the option of using the culture or locale as part of the file name (as opposed to the folder name). This allows you to place multiple locale’s resources in the same folder. You’ll see examples of this technique later in the chapter.


ldn-expertkgetzThis post is an excerpt from the online courseware for ourWindows 8 Using XAML: Views, Resources, and Toastscourse written by expert Ken Getz.

Ken Getz is a Visual Studio expert with over 25 years of experience as a successful developer and consultant. He is a nationally recognized author and speaker, as well as a featured instructor for LearnNowOnline.

Thousands of developers worldwide use LearnNowOnline to gain the technical skills they need to succeed on the job and advance their career.