Tag Archives: XAML

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.

Creating a Group Style

An ItemsControl can work with grouped data in two ways. It can:

  • Bind to grouped data, and display the items from all the groups, without displaying the items in specific groups.
  • Bind to the groups, so that the control displays the groups and the items within each specific group.

Of course, there are different ways to group the data, as well. The data source might be a list of groups, where each group contains the items in that group.

The data source might also be a grouped data structure that you get by using a grouped LINQ query. In this case, there isn’t a distinct name for the subgrouping, so you must rely on the behavior of the ItemsControl to manage the display of the groupings.

As you’ve seen, by default, even if you bind to a grouped data source, the ItemsControl displays no grouping information. In order to add this behavior, you must explicitly specify how you want the ItemsControl to display its grouped data. To make that happen, you must specify the GroupStyle property of the ItemsControl, and provide a GroupStyle element that describes how the grouping should work, visually. In this article, we’ll take a deeper look into creating a Group Style.

Creating a Group Style

The GroupStyle element generally specifies a header template for each group within the grouped data source. You can modify the arrangement of items within the group by setting the Panel property of the GroupStyle object. You may want to specify how the group itself looks, and to do that, modify the GroupStyle’s ContainerStyle property. It’s in this property that you would specify a border, or a specific font. If you want to hide empty groups, make sure you set the GroupStyle’s HidesIfEmpty property to True.

In the sample that you’ll examine, you’ll find markup like that shown in the Figure below, which shows the markup for a ListView bound to grouped data, with all the child elements collapsed. Note the ListView.GroupStyle element, which contains a GroupStyle element that defines the group style. The GroupStyle element contains elements that describe the HeaderTemplate, Panel, and ContainerStyle properties of the GroupStyle.

Ch10_BlogThe sample’s ListView bound to grouped data

The HeaderTemplate property contains information about the header for each group, as shown in the Figure below. This header displays the Key property of the group, with black text on a gray background.

The HeaderTemplate element describes the header for each group

The Panel property (see the Figure below) defines the control that forms the group content. In this case, the group content is contained in a VariableSizedWrapGrip whose orientation is horizontal.

The Panel property defines the control that displays each group’s content

The ContainerStyle property defines the style for the group container, as shown in the Figure below. In this example, the group container has a 2-pixel-wide dark gray border, and margin of 3 pixels all the way around.

The GroupStyle property

Displaying grouped animal data, using the animal type (Domestic, Exotic,Farm, Wildlife) as the grouping field, looks like the Figure below.

windows 8 The grouped data


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.

Windows 8 Using XAML: ItemsControl Styles

As with every other control you use in Windows 8 applications, both the ListView and GridView controls (that is, both ItemsControls) are based on control templates that determine the manner in which these controls appear.

For example, Microsoft supplies default styles that affect the way each ItemsControl appears. They supply two different sets of styles, one for the Light theme, and one for the default Dark theme. This concept applies to every XAML control—that is, every XAML control is based on a template that defines all the styles for various parts of the control.

It’s crucial to investigate the control styles for ItemsControls, because (for reasons that aren’t particularly clear) the default styles for the Light theme simply don’t work with ItemsControls! As you’ll see, the selected item in an ItemsControl, when you use the Light theme, appears with a dark purple background and black text. This makes any text in the item unreadable. Luckily, it’s easy to override the default styles. >/p>

All of the styles that affect the display of every XAML control are documented, including ListView and GridView. Microsoft has provided, as part of its developer documentation, a list of all of the control templates and their default styles, as well as instructions on which styles to override to change the look of each control. All you need to do is override any of the styles in your application’s App.xaml file (or in a resource library referenced from App.xaml).

You can find the styles for ListView controls (not the ListView items, but the control itself) here:

http://go.appdev.com/?id=8AFA

You can find the styles for GridView controls (not the GridView items, but the control itself) here:

http://go.appdev.com/?id=8AFB

When you browse to either of these pages, you’ll find a list of styles that you can override, and the entire control template. The Figure below shows the available styles you can override for the ListView control. These styles apply only to group headers.

Item styles are more interesting, because you can override them immediately to modify the look of an ItemsControl. ListViewItem styles, pertinent to both the ListView and GridView controls, are documented here:

http://go.appdev.com/?id=8AFC

The documented ListViewItem styles, shown in the Figure below, allow you to override any part of the ListView items in your applications.

ListViewItem styles

There is, unfortunately, no real documentation on how each style affects the display of the ListViewItem, so it’s up to you to experiment with the various styles until you find the one that changes the attribute that you care about. In any case, to modify the behavior of a ListView Item, copy the necessary style into App.xaml and modify its value, effectively overriding the default behavior of the style.

Note that the documentation includes separate styles for Light and Dark themes. The items in the documentation describe the default settings. You can override any individual style (or group of styles) to modify the look of the control.

ldn-expertkgetzThis post is an excerpt from the online courseware for our Windows 8 Using XAML: Views, Resources, and Toasts course 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.

Windows 8: Adding Items from a File System

windowslivewriter_customplacementwithinanitemscontrolinwpf_ee53_image_6

In order to take full advantage of an ItemsControl, you must be able to bind it to an existing collection of data. Most of the time, you won’t add items to an ItemsControl individually. Instead, you’ll most likely bind the ItemsControl to some sort of data source. Any data that can be stored and retrieved as a list or array of items is fair game for display in an ItemsControl.

Data in an ItemsControl can come from any collection of data, such as an inmemory data structure, the results of calling a Web service, or data retrieved from a database. It’s really simple to bind an ItemsControl to any collection of data in memory: You set the ItemsSource property to the collection of data, and the ItemsControl does the rest of the work.

Adding Items from File System

What if you wanted to bind ItemsControl to some real data? The simplest place to get “real” data is from the file system, and you can easily bind an ItemsControl to information from the file system, with a little effort.

Although it’s not difficult to bind to information from the file system, it does require a little more effort than you might expect. In order to retrieve any information, you’ll need to create an instance of the FileInformationFactory class, which provides several use methods:

  • GetFilesAsync: Retrieves a collection of FileInformation objects. The method does its work asynchronously, but returns the full file list.
  • GetVirtualizedFilesVector: Retrieves a virtualized vector of IStorageInformation objects. By virtualizing its results, this method makes it possible for an ItemsControl to effectively display a huge amount of data. Scrolling through the control causes the data source to retrieve more data.
  • GetVirtualizedFoldersVector: Works like the GetVirtualizedFilesVector method, but retrieves only folderinformation.
  • GetVirtualizedItemsVector: Works like the GetVirtualizedFilesVector method, but retrieves both file and folder information.

NOTE: The IStorageInformation class acts as a base class for information about files and folders.

In order to bind to data retrieved from the file system, you will also need to follow these steps:

  1. Optionally, create a QueryOptions object whose properties describe the requested sort order and filtering. If you don’t specify a QueryOptions instance, you will retrieve data in natural order including all the filesand/or folders.
  2. Retrieve a reference to a specific folder, or use a known folder (an item in the KnownFolders class).
  3. Create an instance of the FileQuery class, calling the CreateFileQuery method of a storage folder, optionally supplying the QueryOptions object you created previously.
  4. Create an instance of the FileInformationFactory class, passing the FileQuery you created previously, and optionally, other parameters.
  5. Set the ItemsSource property of an ItemsControl to the results of calling one of the FileInformationFactory’s methods, such as GetVirtualizedFilesVector.


ldn-expertkgetzT
his post is an excerpt from the online courseware for our Windows 8 Using XAML: Views, Resources, and Toasts course 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.

Windows 8 Using XAML: Choosing a ListView or GridView Control

Windows 8 ListView and GridView controls both allow you to display data, but they display their collections of data differently. The following article takes a look at both controls and how they are used.

Choosing a ListView or GridView

In order to display multiple data items, you can select the ListView or GridView control. Both controls display collections of data, making it easy to present items from a list, or any other bound data source. Both controls provide similar functionality, although they display their data slightly differently. Both controls derive from the base ItemsControl parent class, and both inherit the functionality of the base control. Because both controls inherit from the same base class, they share a large number of properties. As a matter of fact, the controls are so similar, they’re often both referred to using the name ItemsControl (the base class), rather than saying “ListView and GridView controls”.

TIP: To be honest, the ListView and GridView controls are so similar, you can think of them as a single control. The major difference between the two is the manner in which they lay out their items, and it’s easy to change the layout so each control emulates the other.

The ListView control displays its data stacked vertically, much like a standard list box. Use this control to display an ordered list of data, like a list of email messages, a list of contacts, or search results. It’s also useful in master-detail layout scenarios—you can display the list of master items in a ListView control, and the details in some other kind of control. You can control every aspect of the layout of data in this control, including how each data item appears in the list, using templates.

The GridView control displays its data stacked horizontally in rows (although you can alter this behavior and have it displayed in columns first, as well). You use this control when you need more space for rich visualization of each item to be displayed. As with the ListView control, you can use templates to control the layout of each item, displaying the data in any manner you need.

There are several ways to display data in an ItemsControl. You can:

  • Add items directly to the Items collection property, in XAML or in code.
  • Bind the ItemsSource property to a data source.

Applications often use both a ListView and a GridView control bound to the same set of data, so you can alter the view based on the current screen layout. For example, you might use a GridView control in full-screen layout, but a ListView when in Snapped layout. The first Figure below shows a sample application using the GridView control, and the second Figure below shows the same application, in Snapped view. The trick here is to show and hide the two controls, based on the current layout.

using a GridView control

using a ListView control


Thumbnail for 559T
his post is an excerpt from the online courseware for our Windows 8 Using XAML: Views, Resources, and Toasts course 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.