navigate site menu

Start learning with our library of video tutorials taught by experts. Get started

Building Windows Store Apps Essential Training

Building Windows Store Apps Essential Training

with Walt Ritscher

 


Start creating apps to distribute to Microsoft Windows desktops, tablets, and phones. This course lays the groundwork for coding and distributing your very first app for Windows 8. First, explore the XAML and C# syntax and the tools for developing your code, including Visual Studio and Expression Blend. Then discover how to lay out your app's interface; add color, transforms, and page-to-page navigation; handle orientation changes for mobile devices; respond to events; and accept input via controls and touch gestures. Author and avid developer Walt Ritscher also shows how to configure Windows Tiles, which makes your app accessible, and charms, which allow you to search your app, change its settings, or share app data. Plus, learn advanced techniques for viewing, editing, and storing data and system files.
Topics include:
  • Understanding the Windows 8 ecosystem
  • Setting up Visual Studio 2012
  • Creating a new project
  • Using app templates to jump-start your project
  • Working with the device simulator
  • Debugging your app
  • Creating layout containers
  • Controlling the view states
  • Working with input controls like check boxes, sliders, and progress bars
  • Understanding the application life cycle
  • Sending notifications with the Toast API
  • Creating Live Tiles
  • Using data binding
  • Working with sensors and devices

show more

author
Walt Ritscher
subject
Developer, Desktop Apps
software
Windows 8
level
Intermediate
duration
8h 10m
released
Mar 28, 2013

Share this course

Ready to join? get started


Keep up with news, tips, and latest courses.

submit Course details submit clicked more info

Please wait...

Search the closed captioning text for this course by entering the keyword you’d like to search, or browse the closed captioning text by selecting the chapter name below and choosing the video title you’d like to review.



Introduction
Welcome
00:00(music playing)
00:04Hi! I'm Walt Ritscher and I want to welcome you to my Building Window Store Apps
00:09Essential Training Series.
00:11The apps that I'll be building during the course are intended to run on the new
00:15modern runtime known as WinRT.
00:16I'll start the series with an overview of this new system and show how to test
00:21your apps on actual and virtualized ARM hardware.
00:24Next, I'll take a look on how to use Visual Studio and Expression Blend to
00:28create and edit your applications.
00:31I'll cover layout panels, controls, and I talk about how to add images and media to your app.
00:38I'll have section on working with the application Lifecycle and another on
00:42Data and Data Binding.
00:44I'll cover application-wide like topics like live tiles, charms, and application contracts.
00:49Then I'll wrap up by showing you some interesting topics like working with the
00:53camera, the accelerometer, and other installed devices.
00:57I can't wait to get started. Let's go!
Collapse this transcript
Using the exercise files
00:00If you are a member of lynda.com, you have access to the exercise files used
00:05throughout this title.
00:06I've copied the exercise files to my desktop. Let's take a look.
00:10I'll open the exercise folder. The exercise files are arranged by chapters.
00:15I'll open up Chapter 6.
00:17Inside each chapter are the individual exercise files. I'll open Video 3.
00:22This video has a start and a final version of the project.
00:26I'll open the Start folder.
00:28When you open this folder, you'll see there is an .sln file.
00:31This is the file that you open in Visual Studio or Expression Blend.
00:34Since I have Visual Studio installed on this computer, I can double-click on the
00:38.sln file and it will launch Visual Studio.
00:42Here's the project loaded inside Visual Studio.
00:45Now that we've seen how to open the exercise files, let's get started.
Collapse this transcript
1. Getting Started
Understanding the Windows 8 ecosystem
00:00Microsoft allows us to confuse us with long-winded obtuse names.
00:04Occasionally, they come up with great names like Outlook or Silverlight, but
00:08often, the names look like something dreamed up in a legal committee.
00:12I'd like to start up by clarifying the names around the operating system.
00:15There are three names that you will hear: Windows 8, Windows RT and WinRT.
00:22Windows 8 is the next version of the Microsoft flagship operating system.
00:27If you have an app that works on Windows 7, it's probably going to work on
00:31Windows 8, as they kept backwards compatibility for desktop and console
00:35applications, but it has worthwhile improvements over the previous versions.
00:39Of course, it has a new interface, the start screen. Let's talk about some of the other improvements.
00:45It has a much improved boot cycle.
00:47On my computer, it takes about eight seconds to boot to the logon screen.
00:51Shutdown time is also snappy.
00:54There are a ton of security updates.
00:56For example, it now uses a secure boot to prevent rootkit infections, stopping
01:01the boot process cold, if it detects problems with the system files.
01:06Networking support is better.
01:07I'm happy that the Internet is available as soon as the login screen is visible.
01:11Microsoft finally gets touch right on its main operating system, it's built
01:16in, and it just works.
01:19There are lots of other improvements like better file copying, which you can
01:22learn more about Windows 8 on the lynda.com online training library.
01:27Here's a course on the library called Windows 8 Essential Training.
01:31Windows RT is also an operating system.
01:34It is a constrained version of Windows 8 that is optimized to run on ARM devices.
01:39If you have a Windows 8 phone, it's running Windows RT, same with the
01:43Microsoft Surface Tablet.
01:45You cannot buy a copy of Windows RT.
01:48It can only be installed on ARM devices by OEMs.
01:51It has a Windows desktop, but only a pre-approved list of applications are available.
01:56You cannot install any other desktop application, Adobe Photoshop, no,
02:02Intuit Quicken, no.
02:04There are thousands of apps available though, you just have to install them
02:08through the Windows Store.
02:09I want to make this clear, Windows RT is Windows 8.
02:14You can run all the usual Window utilities like the Command line, File
02:18Explorer, or Task Manager.
02:20Now we come to WinRT.
02:22No, this is not an abbreviation of Windows RT.
02:25It is a completely different part of the Ecosystem.
02:28It is the new low level runtime engine.
02:31It's unfortunate that Microsoft named two different parts with similar names.
02:36Windows RT is part of both Windows 8 and Windows RT.
02:40That means that if I create a Window Store App based on WinRT, it will run on
02:45Win32 and ARM devices.
02:48WinRT is like other well-known runtime engines like the Java Runtime or .NET.
02:53You can think of it as a host for you application code.
02:56The runtime sits between your compiled code and the low level operating system.
03:01If you are a .NET developer, you might be fooled into thinking that WinRT is based on .NET.
03:06That is not true.
03:08They are entirely different runtime engines.
03:10In fact, Windows 8 has both runtimes installed.
03:13WinRT was written to support a broad set of languages including scripts.
03:17It is truly integrated with the core "Windows API" Because it works directly
03:22with native OS Kernel, it is fast and efficient and also collaborates closely
03:27with DirectX, so you can be sure that if a pixel is drawn on the screen, it is
03:32done in the fastest way possible.
03:33Microsoft spent a lot of time and effort to ensure that WinRT runs fast on
03:37limited hardware and is battery friendly for mobile devices. Here's a surprise.
03:42WinRT is based on COM.
03:44If that brings back bad memories of registry hacks, gooids and interface trouble, don't fret.
03:50The WinRT COM specification has been modernized and it works more like .NET.
03:54It was deliberately made to work with multiple languages.
03:58It has new Metadata scheme, which means component definitions are no
04:01longer registry based.
04:02It uses the successful CLI metadata language invented for .NET to describe the
04:07code items defined inside each component.
04:10Therefore, each component is standalone and self describing.
04:14One of the most fascinating features of WinRT is projections.
04:18A language team that that wants to target WinRT must create a
04:21projection definition.
04:23This is basically a wrapper around the WinRT API that redefines how it
04:27appears to the language.
04:29Then tools like Visual Studio can use the projections to show tips and code
04:32help that seem language specific, even though the APIs are not written in the
04:37projection language.
04:39There are three projections available at the moment: C++, C# and JavaScript.
04:45Here's what some code looks like to call the retrieveFeedAsync method in the API.
04:51From the JavaScript perspective, you will notice that the method name on line 40
04:55has a lowercase "r" and that the rest of the code looks like JavaScript.
05:00If you are a C# developer, you will notice that on line 43 and it called
05:04RetrieveFeedAsync, it has a capital R and the syntax looks like you'd expect in a C# language.
05:11Finally, if you still write code in C++, here's what the language looks like
05:15from the C++ perspective.
05:17Look at line 33, here I'm calling retrieveTask and I'm using an angle bracket in front of that.
05:23Let's summarize what we've seen.
05:25There are two versions of Windows, one that's optimized for Win32 devices,
05:29that's Windows 8, and one that's optimized for ARM devices, that's Windows RT.
05:34WinRT is a new runtime engine that supports Windows Store Apps.
05:38These Store Apps can run on either version of the operating system.
05:42You'll learn more about Store Apps later in this course.
Collapse this transcript
Understanding Windows Store apps
00:00In this video, I'll try and clarify what makes a Window Store App a Windows Store app.
00:06Before I go any further, I should address the different names that Microsoft has
00:09applied to these apps.
00:11The original name for Store apps back in 2011 was Metro Apps.
00:17Due to legal issues, Microsoft changed the name to Windows Store Apps.
00:21Unfortunately, there were some interim names used before the final name was
00:25approved; as you can see from this list, there a number of these names.
00:30The side effect of alternate names is that the Internet is littered with
00:33articles using the older names.
00:34At the most basic level, a Window Store App is one that is built on the WinRT runtime.
00:41You may recall from an earlier video that WinRT is the new runtime built into Windows 8.
00:46Apps targeting WinRT run on both Win32 and ARM devices.
00:51From a developer's point of view, a Windows Store App has access to all the
00:54features exposed in the WinRT components.
00:57If you choose to build your Store App with C# and XAML, you should also have
01:02access to the .NET libraries.
01:04To be more accurate, you have access to a sub-set of .NET libraries that are
01:08approved for WinRT development.
01:10Windows Store Apps have a restricted installation model.
01:14If you are a developer, you install an app by getting a developer license from Microsoft.
01:19Once the developer license is installed, you open Visual Studio or Expression
01:24Blend and build your application.
01:26When you debug the app in Visual Studio, it automatically deploys it to your computer.
01:30There is also a deploy menu in Visual Studio that allows you to deploy the app
01:34to tablets and other devices.
01:36Before you can install on your tablet, you'll need to get a developer license
01:40for that device, too.
01:42Most Windows Store App are destined for the consumer market.
01:45Consumers install the app by visiting the Windows Store App on their computer
01:50and choosing to install your app.
01:52It's a simple painless process that makes installing and uninstalling apps a breeze.
01:56To get your app into the store, you need a Window Store account.
02:00Once you have the account, you submit your app to the Store and Microsoft
02:03takes care of the rest.
02:05If you are building an App for your company, then there are a couple of other
02:08ways you can install the app.
02:10Your company can create an enterprise store.
02:12You, then submit your app to this private store.
02:16Also, you can do what's called "sideloading."
02:18Essentially, this is a way to copy the application file to corporate computers.
02:22Note, for this to work, you need to purchase sideloading licenses for
02:26each employee computer.
02:28Apps are not installable in any other fashion.
02:31For example, providing a link to an exe download on your website is not an
02:35option, nor is building a custom installer package like an msi file.
02:40You can give other developers a copy of your application file, but that is not
02:44an approved way to install the app for consumers.
02:47This only works on computers with an installed developer license.
02:51For security and performance reasons, a Window Store App runs in a special sandbox.
02:56Your code can only access approved libraries.
02:59Microsoft verifies that your application complies with this restriction when
03:03your app goes through the approval process.
03:05If you try and sneak a call outside the approved libraries, your app is rejected
03:09from the market place.
03:10Microsoft also verifies that your app is safe to use during the same process.
03:15There are other tests supplied to your app during this phase.
03:18For example, Microsoft checks to see if you're using trade mark images that you don't own.
03:22It also looks for adult material and other age restricted content.
03:26Poorly written hardware drivers are a known problem area and prior releases at Windows.
03:30Bad drivers can bring performance to a crawl or open security holes in the system.
03:36Windows 8 includes a new driver model.
03:38The new model keeps your environment safe and performing well by restricting
03:41access to devices which have not been specifically produced with WinRT apps in mind.
03:46The good news, if the driver is installed and on the sanctioned list, it is easy
03:50to use from your application.
03:53Let's review the criteria that makes an app a Window Store App.
03:56It uses the WinRT runtime, it is deployed through the Windows Store or
04:00other approved channels and it works with the restricted drivers and from within a sandbox.
04:05WinRT exposes a common set of features that show up in all good Windows Store Apps.
04:10I've listed the features on this screen.
04:13I'll show you these features in other videos.
04:15Microsoft has done a lot of research into what makes it a good Window Store App
04:19and they've published a series of App of guidelines and UI suggestions.
04:22These guidelines were followed by Microsoft's own apps.
04:26Check out the default apps on the Windows computer, like the mail app or the
04:29weather app; they were designed according to these guidelines.
04:33These guidelines are optional, but highly recommended.
04:35I encourage you to read more about them at this Microsoft URL.
04:39The Guidelines cover a number of areas.
04:41Here are just a few: Touch, how to design a clean and open layout, working
04:47with landscape and portrait form factors, and roaming app settings across
04:51devices through the cloud.
04:53The implementation details are up to you.
04:55You'll see more about these features as you work through the course.
Collapse this transcript
Exploring features common to all Windows Store apps
00:00All Windows Store Apps have access to some common features like the Charms Bar and Snap mode.
00:06In this video, I will show you some of these common features.
00:10When you install a Window Store App, a tile is automatically added to the Start screen.
00:15When you press the Windows key on your keyboard or touch the Windows icon on the
00:20tablet, the Start screen appears.
00:22The Start screen contains tiles of various sizes.
00:25To launch an app, touch the tile or click the tile with your mouse.
00:29For example, I will touch the Store tile.
00:32All apps start in full-screen mode.
00:35Gone are the task bar in min-max buttons.
00:38When you create your app UI, remember that you have the entire screen to work with.
00:42I want to search for an app within the Store.
00:45In Windows 8, search is located in the Charms Bar.
00:48On the tablet, swipe in from the right side of the screen to show the charms.
00:53If you are using a keyboard, use the Windows key C combination.
00:58There are five standard charms available for all apps.
01:02All start at the bottom.
01:03The Settings Charm allows users to change settings for the app or the PC.
01:09You can add your own settings and help information to this charm.
01:15The Devices Charm, lists all the devices that are connected to this PC,
01:19both wired and wireless.
01:21Here, you can print from an app, sync with your phone, or stream you music to your Xbox.
01:27The Start Charm provides another way to get back to the Start screen.
01:31I'll touch the Start Charm once and I return to the Start screen.
01:35Now I'll bring up the Charm Bar again and touch the Start Charm a second time,
01:41which takes me back to the Store App.
01:45The Share Charm provides a way to share files and other info with people you
01:48know or send info to another app without leaving the app you are in.
01:53At the top of the Charms Bar is the Search Charm.
01:56I'll touch the Search Charm.
01:58The search text box appears as does the on-screen keyboard.
02:02I'll type the word "paint" and then touch the search button.
02:10The search result shows several apps available in the market place.
02:14What's important to note is that the app is getting to search words from Windows
02:17and looking to its own internal data and showing these results.
02:21You can do the same in your apps, showing whatever custom results make
02:25sense for you app data.
02:27This Fresh Paint app looks interesting, so I'll touch the yellow item.
02:32On this screen, I can look at screen shots of the app, click on the details, and
02:39read more about the application or look at User Reviews.
02:46I can also touch the Install button to install the app.
02:54The Store App now, shows information about the install process in the upper
02:58edge of the screen.
03:00Now the application is installed.
03:02I'll touch the Window icon and return to the Start menu.
03:06Remember that I said each installed app is a tile.
03:09The tile for Fresh Paint is at the end of the Start screen.
03:14That action selects a tile.
03:16See the check mark in the corner of the tile and it makes a context bar visible
03:20across the bottom of the screen.
03:23This context bar is called an "app bar" and any app can have one.
03:27As you can see, in the Start screen app bar, there are several action buttons.
03:31There is one button for uninstalling the app and others for changing the
03:34tile size and so on.
03:36Think what this means for your app.
03:38It's easy to install from the Store and painless to uninstall at anytime.
03:43I'll touch the Fresh Paint tile to launch the app.
03:47I will choose My Paintings, this February painting, and I'll touch
03:53this painting here.
03:56This takes me to the main UI.
03:58This is beautiful painting application.
04:00At the top and bottom of the screen are two app bars.
04:03They look fancier than the app bar on the Start screen, but it's the same API.
04:08To paint, I touch the desired brush, select a color, then touch the art board to
04:15dismiss the app bars.
04:17Now I drag my finger to paint on the screen. Very nice!
04:23To bring back the app bar, drag your finger in from the top or bottom of the screen.
04:29Windows Management is different on the tablet, let me start another app and show
04:33you the snap feature.
04:34I will press the window icon to return to the start screen.
04:37The weather app is included on the default Start screen.
04:40I will touch the tile to start this app.
04:44This is a straightforward app that shows weather information for any location you want.
04:48To change the locations, bring up the app bar and touch the Places button.
04:54I'll touch the Seattle icon to switch to the Seattle Weather Station.
04:59The Fresh Paint app is still available.
05:02I can switch to other apps by swiping in from the left side of the screen.
05:07I can switch back to the weather app by swiping in from the left side.
05:11You can snap an app to the side of the window by swiping slowly down from the
05:15top until a small version of the app is visible on the screen and dragging back
05:20to the left until a split bar shows.
05:23Then lift your finger from the screen.
05:25Now one-third of the screen is inhabited by the weather app.
05:29To add another app to the screen, I can left swipe and move the app to the larger area.
05:35Now I have two apps available on the screen.
05:38Notice how both apps have rearranged their UI.
05:41When writing your app, you must change your UI layout when you detect that your
05:44app is in snapped mode.
05:47Next, I will snap the paint app.
05:49Drag the splitter to the right until the splitter appears on the right side of
05:54the screen, then lift your finger.
05:57Again, note how both apps have changed their UI.
06:01Finally, to close an app, drag your finger in from the top of the screen until
06:05you see the small app rectangle, then keep dragging until you exit the screen at the bottom.
06:10I've closed one app, now I will close the other app with the same motion and I'm
06:16back to the Start screen.
06:18These features, snap mode, the app bar, the charms bar, and many more are baked into WinRT.
06:25Keep watching the course videos to see how to integrate them in your app.
Collapse this transcript
Windows Store Apps for the Silverlight or WPF developer
00:00XAML is without question the primary markup language to come out of the
00:04Microsoft Developer Division.
00:06No, I haven't forgotten about HTML.
00:09The Microsoft asp.net teams are fully invested in HTML5 support,
00:13but XAML is a markup language conceived by Microsoft and it is
00:17used in many of their different UI platforms.
00:19XAML is used in WPF to create UI for complex desktop applications.
00:25It's a key part of Windows Phone development.
00:27XAML made a big splash a few years ago when Microsoft released Silverlight as a
00:32web plug-in technology.
00:34You could create a fancy graphic UI for your website with Silverlight, very
00:38similar to Adobe Flash in this manner.
00:40Silverlight was enhanced to be more of a desktop platform in later versions.
00:44XAML is also used for creating UI for massive touch screen apps as part of the
00:49new PixelSense API; and of course, XAML is one of the mark-up languages available
00:55for Windows Store Apps development.
00:57If you already know XAML from WPF, Silverlight or other platforms, you can have
01:02a marked advantage over developers with no prior XAML experience, but you still
01:05have to learn the WinRT API features.
01:08It is full of new concepts and different ways to interact with your users.
01:12Even though WPF and Sliverlight get little marketing love from Microsoft, I feel
01:17that both technologies have a place in the development world.
01:20WPF is still the best way to create powerful complex applications that run
01:25on the user's desktop.
01:26Visual Studio, which is written in WPF, is a prime example of this type of app,
01:32full of menus, ribbons and tool windows for development tasks.
01:37My guess, if Microsoft was starting fresh with Office, they'd write it in WPF.
01:43WPF works on Windows versions from Windows XP to Windows 8.
01:48There are some differences in the XAML.
01:50WPF is the biggest, most complicated version of XAML.
01:55The version in WinRT is a subset of the older XAML implementation.
01:58Silverlight is still used in some development shops to build apps that run in
02:02the browser or in desktop mode.
02:04These apps will run in Windows 8 with one exception;
02:06they won't run in the WinRT IE browser.
02:10I have a confession to make.
02:12I love the Silverlight name, but somewhere deep inside the Microsoft campus,
02:16someone decided that it was time for a change.
02:19So the XAML specification was updated, a new runtime was created and the
02:24Silverlight name faded away.
02:25Whether you call it Silverlight or not this new version of XAML is a lot like
02:30the old Silverlight.
02:32Most of the features are identical.
02:34Data binding is largely unchanged, for example.
02:37New features were added to WinRT.
02:39This is necessary as WinRT is basically a mobile, touch-friendly system that
02:43carries the Windows name.
02:45There are some differences though.
02:46WinRT apps are native apps running on the Windows OS whereas, Silverlight runs
02:51in a browser plug-in.
02:52Both systems have their own dedicated runtimes.
02:55If you have XAML experience, you should be confident that your skills are ready
02:59to help you become a Windows Store app developer.
Collapse this transcript
Setting up your developer computer
00:00You've heard about Windows Store Apps and are ready to start learning this new UI Platform.
00:05But first, you need to install some tools on your developer computer.
00:08In this movie, I will show you how to set up your computer so that you can
00:12create your WinRT Applications.
00:13Windows Store Apps can only be developed on Windows 8.
00:17So the first thing you'll need to do is install Windows 8.
00:20In order to test your apps in a snap mode, you'll need a monitor with a minimum
00:24of 1366 x 768, resolution.
00:29That is a bare minimum, of course.
00:30For productivity purposes while running Visual Studio, I recommend
00:34higher resolutions.
00:35I personally run at 1920 x 1080 on three monitors.
00:40Touch is a central part of Windows Store Apps.
00:43While you can test touch interaction with a mouse, you will get better results
00:47with a touch screen.
00:48You can buy touch monitors for a reasonable price now-a-days.
00:52To truly test your app in real world conditions, you need to run the app
00:55on actual hardware.
00:57It's vital to test on various ArM Devices.
01:00One popular ArM Device is the Microsoft Surface.
01:03Any developer working in the Microsoft world should install Visual Studio.
01:07It is the premier development tool for creating apps that target Microsoft tools.
01:12For Windows Store Apps, you must use Visual Studio 2012.
01:16It contains tools that help you develop, test, and deploy your apps.
01:20There are many versions of Visual Studio available:
01:23Professional, Premium, and Ultimate.
01:26These versions cover a wide price range with the higher price versions
01:30containing more tools.
01:32Any version is suited for Store App development.
01:34You can find information and downloadable versions at this link location.
01:39There is also a free version of Visual Studio called Visual Studio Express
01:422012, for Windows 8.
01:45Even though it is free, it contains all the tools you need for Store App development.
01:49You can get your free copy at this URL.
01:52Regardless of which Visual Studio version you choose, you will need an hour or
01:55more to install Visual Studio.
01:58For many years, Microsoft promoted a special UX Design tool called
02:01the Expression Blend.
02:03It was available as a separate tool with separate cost and licensing.
02:07But that is no longer the case.
02:09Expression Blend is now part of the Visual Studio 2012 installation.
02:13It will be on your computer whether you want it or not.
02:16Personally, I find Blend to be a great tool for designing certain user interactions.
02:20For example, I find it invaluable for creating animations and storyboards.
02:26Blend is fully integrated with the Visual Studio project file formats.
02:29I'll show you how to use Blend as we go through the course.
02:32Microsoft has rebranded their live ID system.
02:36It's now known as a Microsoft account.
02:38Windows 8 takes full advantages of these accounts.
02:42If you log in to your Windows with your Microsoft account, you'll get automatic
02:45synching of your email, pictures, Skydrive files and account settings.
02:50If you have an email address at hotmail, live.com, or outlook.com, you already
02:56have a Microsoft account.
02:57If not, you can create one in a few minutes or register an existing email with
03:03the Microsoft account system.
03:05You might be asking yourself, What is all the fuss about having a
03:08Microsoft account?"
03:09Well, in order to create a Windows Store App with Visual Studio, you must have a
03:13valid developer license.
03:15To get your developer license, you must use your Microsoft account.
03:19Also, if you plan on selling your apps in the Microsoft Store, you will need to
03:23create a Microsoft Store account.
03:25Your Store account is linked to your Microsoft account, too.
03:28Let me show you how to get a developer license.
03:31The easiest way to get your license is to use Visual Studio to create a Windows Store App.
03:35When you try to build your app it will automatically prompt you to apply for your license.
03:40To open a Visual Studio, I'm going to press the Windows key on my keyboard.
03:45And then I'm going to type vis for Visual Studio.
03:49And then I'll click on this Visual Studio 2012 tile.
03:53Here I am inside Visual Studio.
03:55I'm going to create a new project by clicking on the New Project Link.
04:00Then I'll choose my language.
04:01I want to choose Visual C# and then I'm going to choose Windows Store and I'm
04:07going to choose this first template called Blank App (XAML).
04:09I'll going to leave it at the default name for now at App1 and click OK.
04:18Since this is the first time I'm using Visual Studio, I'm automatically prompted
04:21to get a developer license.
04:24I will click on the I Agree button.
04:32Here is the Sign in screen.
04:33I need to enter my Microsoft account email address and my Password, then
04:38click the Sign In button.
04:44If I'm successful, I'll see this dialog.
04:46You now have a Developer License and you can develop apps for Windows 8 until it expires.
04:51You'll need to renew your license before it expires to continue developing apps.
04:55A standard developer license is good for 30 days.
04:58If you have a Windows Store account tied to the same Microsoft Developer
05:02account, you get a 90-day license.
05:04It takes one to three weeks to get approved for the Windows Store account.
05:07So you might need to start that process as soon as possible.
05:10It's easy; I'll show you how.
05:12I'm going to close this dialog, then go to the Project menu, the Store menu
05:22and then choose Open Developer Account.
05:25Then on this Microsoft site, you'll sign up for a Windows Store developer account.
05:29Now you're ready to start creating your first application.
05:32The next movie shows you more about the Visual Studio environment.
Collapse this transcript
Setting up Visual Studio 2012 for this course
00:00Most developers I know have a strong opinion on how they want their developer
00:04tools to look and behave.
00:07Visual Studio 2012 is a powerful code tool.
00:09I have a few settings that I use during this course that are different from the defaults.
00:13Follow the steps in this video to make your copy of Visual Studio look the same as my copy.
00:18There are a ton of options you can set for your work environment.
00:21If you want to find out more, watch the Visual Studio 2010 Essential Training,
00:26here in the lynda.com online training library.
00:30I'm in Visual Studio, and I've opened a project called Setting Up Visual Studio.
00:35There are two user interface features in the Visual Studio that I dislike.
00:39I'm not alone in my feelings.
00:40Both these issues are frequently lambasted in the Microsoft community.
00:44The first UI gaff is the all cap top level menu.
00:48This is a recognized, readability killer and Microsoft should know better.
00:53The second UI annoyance is the washed out color schemes.
00:56Visual Studio is configured with two colorless UI themes.
01:00These themes are supposed to make the environment designer-friendly, but I
01:04need a bit color in my UI.
01:06Both of these issues are fixable by installing some Visual Studio extensions
01:10that modify the default settings.
01:11Visual Studio extensions are applications that alter the behavior of Visual Studio.
01:15To add this extensions, open the Tools menu and choose the Extensions and Updates menu.
01:24This shows all my installed extensions.
01:26I can search for more extensions by clicking on the Online node.
01:31I'm looking for an app called Remove CAPS.
01:37Here it is, right at the top of the list.
01:40I'll click on the Download button and then click on the Install button.
01:45And the bottom of the screen it says, "You must restart Visual Studio in order
01:49for the changes to take effect."
01:51I'm not going to start immediately because there's one more thing I want to install.
01:55This time I'll search for color theme.
02:03Here it is again, it's at the top of the list.
02:05I'll click Download, Install and at this point I'll restart Visual Studio.
02:17When Visual Studio restarts, it asks me which theme I would like to pick.
02:22My favorite is blue, but as you can see, there are a number of default themes. I'll choose blue.
02:29That changes the design of the inside of Visual Studio to a blue theme.
02:33And now I'm going to use the Remove CAPS tool.
02:38Now I'll restart Visual Studio, much nicer.
02:42A new theme, and the All CAPS menu is removed.
02:45Now that I'm back in the Visual Studio, I'll re-open my Project.
02:49Next, I will change a few code editor options.
02:52Before you change your options, you may want to save your current settings.
02:56Open the tools menu and then choose Import and Export settings.
03:02Choose the Export selected environment settings then click Next.
03:06Choose the settings that you want to export by checking these check boxes in
03:10this dialog and clicking Next, and then name your file and choose a
03:14location and click Finish.
03:16I will choose the defaults and click Close.
03:20Now that our settings are saved in a backup file, let's get to work
03:23configuring our editors.
03:24You'll find the settings dialog in the Tools Options menu.
03:31The first thing I'm going to change is my Fonts and Colors.
03:36I'm going to change my main Font to be 14 points and then if I click on OK, it
03:43will save my settings, but also close the Option dialog.
03:45So I'm going to wait before I click OK.
03:48Next, I want to look at the settings for the Text Editor.
03:52Those are down here in this Text Editor Node.
03:55This node lists all of the installed Text Editors inside Visual Studio.
04:00The editors that I spend the most time in are the C# Editor and the XAML Editor.
04:05XAML stands for Extensible Application Markup Language.
04:08I'll start by changing some All Language features.
04:12I'll open this node and then in the general section, I'll make sure that
04:17Line Numbers is checked.
04:18I like having Line Numbers on when I'm coding; and also make sure that the
04:22Word wrap is checked.
04:24Next, I'll set my Tab sizes by clicking on this Tabs node.
04:29I will use two units for the Tab size and two units for the Indent size.
04:34Then you get to choose whether you want to insert spaces, or keep tabs.
04:38Next, I will go down and modify my XAML Editor.
04:41We'll start by looking at the Formatting section.
04:45Here, I like to have Single quotes enabled instead of Double quotes for my attributes.
04:51In the Spacing section, I like to position each attribute on a separate line.
04:55I'll show you what that means in one minute.
04:58I also like to collapse multiple empty lines in content into a single line and
05:02I'll click OK to persist my settings.
05:06Now let's look at the XAML Editor in Visual Studio.
05:09I'm going to open this mainpage.xaml by double-clicking on it in Solution Explorer.
05:16This shows the default designer.
05:17On the top half of the screen inside Visual Studio, is the Visual Editor.
05:22And on the bottom half of the screen is the XML or Text Editor.
05:26These two screens are synchronized.
05:28What I want to do is go down to my XAML.
05:30I am going to make this a little bit smaller on the top half for now.
05:34I am going to scroll down here to this Image Section.
05:42Here's an image and I want to put on some extra spaces in here and some
05:47extra spaces down here.
05:49And then I want to move this Stretch attribute up to a different line, like so.
05:59Now I can automatically format this document using the rules I specified in the Text Editor.
06:05To do that, I go to Edit, Advanced and then I choose Format Document or you can
06:12use this keystroke, Ctrl+K, Ctrl+D. And did you see what happened?
06:18The extra spaces inside this file between the two tags are removed.
06:23And all of the attributes are aligned underneath the top level attribute.
06:27I find this makes XAML much more readable.
06:30Visual Studio has a ton of options you can set for your work environment.
06:34Spend a few minutes examining the options menu to learn more.
Collapse this transcript
Preparing a Windows 8 tablet for development
00:00You can test your app on your local developer computer, on a device simulator or
00:05use remote debugging to test on actual tablet hardware.
00:08I always strive to test on real hardware.
00:11Currently, I have several devices including the Asus Vivo Tab.
00:15Testing on ARM processors will give a sense of what your app behaves like in the real world.
00:21ARM processors are not as powerful as the CPU on your developer computer.
00:25You'll smoke out problems, especially performance trouble when testing
00:29on different tablets.
00:30You can run on any Windows RT device as long as that device has the
00:34necessary software.
00:36You must install the remote debugger application on each tablet that you want to test.
00:42The remote debugger is a desktop app.
00:44Microsoft locks down Windows RT.
00:47Only a small approved list of desktop apps can be installed on a device.
00:52The good news is that the remote debugger is one of those
00:55privileged applications.
00:57Let's look at what you need to do on the tablet.
01:00Open the desktop version of Internet Explorer and download the installer from this URL.
01:05You have to install with the desktop browser.
01:08Don't try to use the app store one.
01:11Once the debugger is installed, run the application and click the Options menu
01:16to configure the port number and security settings for the debugger.
01:19Note, the tablet and dev computer need to be on the same network sub-net.
01:25When the remote debugger is running it broadcasts its identity to the
01:29local network sub-net which allows Visual Studio on the same sub-net to find the device.
01:35You will have to get a developer license on your computer.
01:39The way you do that is you run Power Shell as an administrator.
01:43As you can see on the screen shot I have to type in Show-Windows Developer
01:48License Registration that causes this dialog to pop up.
01:52You'll either be renewing license or adding it for the first time.
01:56You then click on the I Agree button.
01:59After a few seconds, you'll see this dialog that says, "You now have a developer
02:03license" and the expiration date for that developer license.
02:07On the developer computer, you just have to run Visual Studio and open a
02:11Windows Store project.
02:13Change the deploy location to the Remote Machine and attempt it deploy it in the machine.
02:17Visual Studio will show a screen that lets you pick the remote computer and
02:21enter your credentials if necessary.
02:24It's time to switch to my tablet PC.
02:27The first step to installing the remote debugger is to open the desktop version
02:31of the Internet Explorer.
02:32Touch the desktop tile on the Start screen, then touch the Internet Explorer
02:37icon on the bottom of the screen.
02:39I'm going to go to My Favorites, touch the Favorites icon and click on this
02:45remote debugger install, which is the location for downloading the remote
02:49debugger and then I'll touch this ARM version of the install.
02:54At the Download prompt, I'll touch the Run button, then accept the license terms
02:59and touch the Install button.
03:02On the User Account Control dialog, I'll touch Yes and then I'll close
03:08the Success dialog.
03:09I'm going to run the remote debugger now.
03:13I'll swipe in from the right side of the screen and touch the Search charm, then
03:19type in "REM" then touch this remote debugger tile.
03:24Now the remote debugger is running.
03:27I'll change a few options in the tool before switching to Visual Studio.
03:31I'll touch Tools, Options.
03:34And here, I can pick the port number that I want to communicate on and I can
03:39also pick the Authentication mode.
03:40You need to decide what security mode to use for the connection from your developer PC.
03:45The most secure is Windows Authentication.
03:47If you use this setting, you might be prompted for a name and password when you
03:51connect to the device.
03:52It's time to switch to Visual Studio.
03:55In Visual Studio, I've opened the project called FilePickers and ready to
03:59deploy to the tablet.
04:01I'll choose this dropdown and pick Remote Machine from the bottom of the list.
04:06Visual Studio finds on my sub-net the Lynda Surface device. So I click here.
04:12It will give me some information about this device, the IP address, what kind
04:17of authentication mode it's using, and when the developer license expires on that computer.
04:22I click on Select and at this moment, I am now connected and ready to debug or
04:28deploy to the computer.
04:29Now you can use Ctrl+F5 to deploy.
04:33You can press F5 to debug and you can also choose this menu and choose to Deploy Solution.
04:40I'll try this one. Click here.
04:43Keep an eye on the bottom of the screen and I see I have one deploy succeeded.
04:48Now I'll switch back to the surface and see the application running on that device.
04:54Here on the surface, I'll press the Window key to open the Start screen, scroll to the end.
05:00There is my FilePickers tile.
05:03I'll touch the tile to open the app, and it's successfully running on this PC.
05:09It takes a few steps to get the tablet ready for development.
05:11But now that the tablet is configured, it is dead simple to deploy and debug on
05:16this fancy new tablet PC.
Collapse this transcript
2. Windows Store App Fundamentals
Creating a Windows Store app project with Visual Studio 2012
00:00If you are a .NET developer, you are probably familiar with Visual Studio.
00:05It excels as a developer tool for many types of projects.
00:08If you are new to Visual Studio, I recommend you watch the Visual Studio
00:12Essential Training Course on lynda.com.
00:15In this video, I will show you how to create a Windows Store App with Visual Studio.
00:19You have to use Windows 8 if you're working with Windows Store Apps.
00:23I'm running Windows 8.
00:24To open Visual Studio, I'm going to press the Window key on my keyboard and then
00:29at the Start screen, I'm going to type in VIS to find Visual Studio.
00:34Then I can either touch or click this Visual Studio 2012 tile.
00:41I'd like to create a brand new project.
00:43As you can see on the left side of the screen, there are several hyperlinks over here.
00:47The one I want is this first one, New Project.
00:53Next, I have to pick a template.
00:56A template contains preset files and settings for Visual Studio.
01:00You can see there are templates for Visual Basic, for Windows Store Apps, for Visual C#.
01:06There's Windows Store Apps and there's also an area for the C++ Developer.
01:11I will work with the Visual C#.
01:13So I open this node and then I choose the Windows Store node and then here are
01:19the different templates available for this project.
01:22I'll start with the basics, Blank App.
01:24I'll come down here and give it a decent name, Demo1, and then I get to choose
01:29whether or not I want a separate folder for a solution or not.
01:33I'll go ahead and take the default.
01:34You notice that it's going to put it in this default location in this Visual
01:38Studio 2012 Project folder.
01:41So I'll click in OK, wait about two seconds, and I have a brand new project.
01:49If you go to your File Explorer and look in this Projects folder, Visual
01:53Studio 2012 Projects, there is my Demo1 folder and there's my sln file and the other files.
02:02I'll talk further about this in other videos.
02:06I just created a standard Windows Store App based on a template.
02:10The Solution Explorer on the right side of the screen is where I can see
02:14the Project Structure.
02:15You notice there are several standard files in here. There is map manifest;
02:18there is some sort of temporary key file in here;
02:22there's two xaml files that I can see: App.xaml and MainPage.xaml.
02:25The one I want to look at is MainPage.xaml.
02:29If I double-click on MainPage.xaml, it loads it into this portion of the Window,
02:34the center area of the screen.
02:36Now I have a split screen over here.
02:38The top half of the screen represents the Visual Designer and the bottom half of
02:42the screen represents the text editor or the xaml editor.
02:46What I'm going to do is add a background color to this first grid.
02:50We'll scroll down, until I find the grid element.
02:53Then I'm going to change what's in the background attribute and just type in a color name here.
02:58We'll type in Orange.
03:00Notice that Visual Studio gives a list of choices.
03:04I can just press the tab key now to finish typing.
03:07That's very bright.
03:09Let's see what this look like when you run the application.
03:12There are two ways to run the application from within Visual Studio.
03:15You can choose F5, which launches the app and attaches the debugger.
03:20You can also do that through this menu here, start debugging.
03:24You can choose start without debugging which is Ctrl+F5.
03:26For today, I'm going to choose this second option Ctrl+F5.
03:34Visual Studio compiles my app, then starts the app and runs it on my
03:38developer computer.
03:39As you can see, I have a giant orange screen.
03:42Since this is a Windows Store App, it defaults through the Chromeless full screen view.
03:47This is a very basic app, but it still supports many of the default
03:50Windows Store features.
03:51For example, I can grab the top edge of my Window with my mouse.
03:56Notice how that cursor turns to a hand and then I can drag downwards to resize the Window.
04:02Now I can drag it to the left side of the screen and put it in what's
04:06called the snapped view.
04:09I can then grab this splitter bar and drag to the right, to put it back in full screen mode.
04:15I get all of that functionality without having to write any special code.
04:19If I was running the app on a tablet, I could use touch, to do the same tasks.
04:24I can invoke the charms by pressing the Window key and the "C" key. Isn't that great?
04:30I get the standard charms for my app even though this is a simple, 'do nothing app'.
04:35I'll go over and press the settings charm.
04:39You can see that at the top of this list, I can see some information about my app.
04:44It's called Demo1, it's by Walt Ritscher.
04:46So that information was pulled from my app and put in the Settings bar and if I
04:50click on the Permissions, you'll see that my app has permissions to use that
04:55Internet connection.
04:56Again, this is great.
04:57All this functionality is built right into my "do nothing app".
05:00The way you close the app, you can press Alt+F4 or you can go and drag the top
05:05of the window down, all the way to the edge of the screen on the bottom.
05:09Now Because I'm inside Windows 8, when I close the window in that fashion, I end
05:14up back in the Start screen.
05:16If I want to return back to Visual Studio, I can choose Alt+Tab and switch back.
05:22I like to modify this app slightly and show how it works in snap mode.
05:27I'll click on my designer;
05:29I'll add a rectangle to my xaml.
05:36You can see my rectangle in the center of this designer.
05:39It's a 400 x 200 red rectangle and I need to give it a name.
05:44Another way to work with items is to click on it in the designer and then go to
05:48your properties window.
05:49Press F4 if you don't see the properties window and I'm going to give it a name
05:53by typing here, rect1, is the name I use.
05:57Now I'm going to go to my code behind.
05:59I'm going to do that by opening up this node in Solution Explorer and then
06:03double-clicking on MainPage.xaml.cs.
06:08That takes me to what's called the code behind view.
06:10I'm going to write a few lines of code in this code behind.
06:13I'm going to start on this OnNavigatedTo method.
06:18You can think of this as this method runs when our page is viewed in the application.
06:26The first thing I'm going to do here is set up an event handler.
06:30Let's say Window.Current.SizeChanged.
06:35What I'm doing is I want to react to whenever the Window size changes.
06:39I want to run some code of my own.
06:41I'll type in "+=" Visual Studio is prompting me to just type in some code.
06:46So I'll press Tab and then it says press tab to generate the handler.
06:50I'll press Tab the second time.
06:52So it's written the rest of this line and added this method down here.
06:57Now I'm going to paste in a little bit of code here, to save some time.
07:01I'm getting some red lines or squiggles under a couple of portions of my code,
07:06that because these classes are not in scope.
07:08The Visual Studio has this awesome tool where I can automatically fix this
07:12problem by pressing Ctrl+Period (.)
07:15and I get these choices.
07:16I'm going to choose this first choice, add a using statement to the top of my document.
07:22I'll do a Ctrl+K, Ctrl+D to reformat my code.
07:26Now let's look at this code.
07:28I'm checking to see if I'm in the snap mode and if I am, I set the
07:33rectangle with the 200. Otherwise, it's set to 400.
07:37I'll press Ctrl+F5 to run the application.
07:41There's our red rectangle, in the center of the screen.
07:45Now I'm going to snap the app to the side and my code ran and resized the
07:51rectangle, so it fits in the location.
07:54That concludes our first look at the Visual Studio Projects.
07:57At this point, you've seen how to use the blank template to create an app.
08:02Later in this chapter, I'll show you how to work with the other app templates.
Collapse this transcript
Familiarizing yourself with the Visual Studio interface
00:00Visual Studio is a big sprawling developer tool.
00:03It has dozens of powerful tools tucked into its interface.
00:07Mastering some of these tools will make your daily coding chores easier and more rewarding.
00:12In this video, I will demonstrate a few of these important tools.
00:15I have already opened a solution in Visual Studio.
00:19On the right side of my screen is something called the Solution Explorer.
00:23It allows me to look at all of the files inside my projects.
00:27At the top of this list is something called a Solution.
00:31A Solution is a container for zero or more projects.
00:35This solution has two projects:
00:37the App Library Project, which is a delow project and heading a single class,
00:43and it has a Sample App Project that contains a Windows Store App example.
00:50Now you'll notice one thing happened when I clicked on the Class it loaded a
00:54temporary viewer for that Class1.cs file.
00:56I'll close that tab by clicking the X button.
01:00Each project is a container for files.
01:02Each project also produces a compiled file when the solution is built.
01:06So if I were to compile my app right now, I'd end up with two binaries;
01:10one for Sample App and one for the App Library.
01:14In Visual Studio 2012, you can filter these project file list through the
01:18Search box at the top.
01:21I will type the word gauge, and here is the filtered list.
01:25I can see that there are two PNG files and a number of files down here.
01:30There's a XAML file and a CS file and some sort of code that's available here.
01:36Again, as I touch this with my mouse, it's letting them in this temporary viewer
01:40here on the right side of the screen.
01:42If you double-click on a file, like let me double-click on this,
01:46GaugeControl.xaml, then it loads in on a permanent tab on the left side of the screen.
01:52Let me close both these.
01:56To stop filtering, I click on this X button.
01:59I use this Search feature on a daily basis.
02:02Let's look at the xaml designer.
02:04I'll double-click on this Demo.xaml file to load it into the center area of the screen.
02:10Double-click.
02:12This is the standard view you will get for a XAML file.
02:15The top half is a visual designer that shows the application as it's going to
02:20look when it runs, and the bottom half is an XML editor which works with a
02:24special dialect of XML called XAML or X-A-M-L.
02:29These two views are linked.
02:31If I choose an item on the top half of the screen, you will see that it moves to
02:35the bottom on the bottom half of the screen.
02:37If I click on this slider, you'll see that it moves to the slider.
02:41If I make a change, let's say that I come down here to the slider and I change
02:47the horizontal alignment to the right, I made that in my XML editor, but you see
02:52that's reflective in the designer.
02:54If I move this gauge control with my mouse to a new position, you'll see that
02:58that brought a new margin value here in the XML.
03:01I'll press Ctrl+Z to go back to where it was.
03:04That's the Undo key.
03:05Let me show you how to add a control to this designer.
03:08I'm going to open the toolbox.
03:11If you did not see the toolbox, you should go to your View menu and enable the toolbox.
03:15Currently, it's a little nub on the left side of the screen.
03:19If I click on it, it pops out, and then I can click on this Auto Hide button to
03:24stick it to the left side of the screen.
03:25Now I'm going to take a toggle switch.
03:30Scroll down and you'll find this toggle switch.
03:32I'm going to drag that over and drop that somewhere on my designer.
03:36We'll scroll it up so it's a little bit easier to see.
03:40Again, I can change the settings here in the designer in the XML or I can also
03:47use this thing called Properties Viewer or the Properties Grid it's sometimes called.
03:51If you do not see the Properties Grid, you can press F4.
03:54Now the Property Grid is arranged by default in a category view.
03:58You can change that by selecting this dropdown and choosing Name or Source.
04:02So if I choose Name, they're sorted in alphabetical order for the properties.
04:06I'm going to change the header property. Here it is.
04:15I click on the Enter key and you see that the header changed over here.
04:18There's another tool that I use all the time in Visual Studio, it's called
04:21the Document Outline.
04:22If you do not see the Document Outline, you go to View>Other Windows>
04:29Document Outline or Ctrl+Alt+T. Now I'm going to auto hide this window too.
04:35In a real app, the XAML will get complicated.
04:37The Document Outline provides another way to view the UI elements in their
04:41hierarchical relationships.
04:43Once the Document Outline is open, I can see my elements.
04:45Here, I have a user control at the top that contains a grid, and within that
04:50grid are a number of children elements.
04:52Selecting an item in the Document Outline also selects it in the designer
04:56and the XAML editor.
04:58I can also rearrange the order of the elements by picking it up with my mouse
05:02and dragging them to a new location.
05:04Let me try this gauge down here.
05:06You don't see any changes in the designer, but I've just changed what's called
05:11the Z index of those controls.
05:12Now I'll press Ctrl+Z to undo that action.
05:16Next, I'd like to look at something called the Device Window.
05:19To see the Device Window, use the Design>Device Window.
05:23The purpose of the Device Window is to change settings in the designer to match
05:27the possible conditions on the tablet device.
05:29When I switch to a different XAML file, this BlankPage.xaml, which is using a
05:36default theme, a black background with white text.
05:39Now the user on their computer can choose to use what's called a Light Theme
05:44instead of a Dark Theme.
05:45So when I click on this light version here in the Theme Box, my application is
05:51told that we're working in a machine with the light theme.
05:54What should happen is my UI should change to match that.
05:58This is how you test your UI to make sure that's it's readable in light
06:02themes and dark themes.
06:03Remember, you need to test this because Microsoft is also going to test it when
06:07you submit it to the store.
06:09They're also going to look for things high contrast mode.
06:13You can also test for different screen sizes and different Visual states.
06:17I'll change this 140 scale, and what happened is my check box here got larger.
06:25That's good; it's making itself larger based on the screen size.
06:29Speaking of testing your code on a device, Visual Studio lets you deploy and
06:33debug your app into three locations that are listed in here:
06:36The Simulator, the Local Machine and the Remote Machine.
06:41Let me show you the Simulator.
06:42I'll click here and then I'm going to run the application by pressing Ctrl+F5.
06:52After about five to ten seconds, the simulator is loaded and my application is
06:57loaded within the simulator.
06:59Now I can interact with it with my mouse.
07:03I can also interact with these buttons on this side. I can simulate touch mode.
07:08I can simulate rotation touch mode.
07:10I can simulate the users switching this tablet orientation by clicking on this button here.
07:16When you're done with the simulator, you can click on this minimize button.
07:19The simulator continues to run even while I'm back in Visual Studio, so it's
07:25ready for me to load it the next time I want to try out my application.
07:29The final tool I want to show you is the Store menu; Project>Store.
07:35It is here where you acquire your developer license, take screen shots and
07:39configure the App Manifest.
07:41Let's choose Edit App Manifest.
07:46The manifest is where you set up your standard store images and app logos.
07:50It's here while you tell your user what capabilities your app needs in order
07:53to run successfully.
07:55For example, I can state that I need the microphone capabilities.
07:59That concludes this quick tour of the important XAML and Windows Store App
08:03tools and Visual Studio.
08:04I'll provide more details about all these tools in other chapters.
Collapse this transcript
Knowing when to use Expression Blend instead of Visual Studio
00:01Visual Studio is, without a doubt, the best developer tool produced by Microsoft.
00:05In Visual Studio 2012, the XAML UI designers are first-rate.
00:10Expression Blend is a separate tool that is also an excellent XAML design tool.
00:14That shouldn't be a surprise;
00:15they share a common designer engine.
00:17Some XAML developers never leave Visual Studio.
00:20But that is a mistake.
00:22There are some UI tasks that are hard to do in Visual Studio that only take a
00:26few minutes in Blend.
00:27Are you afraid it will interrupt your workflow? Stop worrying.
00:31It's trivial to switch to Blend, and both tools share the same formats.
00:36At the bottom of this screen are list of the activities that I always do in Blend.
00:41For example, I always use it for making my animations and story boards and my
00:45style and template editing.
00:48I'm inside this project called Blend Collaboration.
00:50There are two files that I want to edit in Expression Blend:
00:53this Octopod.xaml file and this Demo.xaml file.
00:57I'll start by looking at octopod.
00:58I'll double-click on the file.
01:01Octopod is a cute animal I created a few years ago out of a set of ellipses and custom paths.
01:06I need to modify the colors and the angles of the gradients, and that's a
01:10perfect job for the Blend Gradient tool.
01:12The second file, Demo.xaml -- looks like I need to build the project so I'll do a
01:18Ctrl+Shift+B -- contains a pie chart.
01:24In Blend, I'll animate the large blue pie slice.
01:27Let's start with octopod.
01:29How do you switch to Expression Blend?
01:31Well, you right-click on the file you want to open and choose Open in Blend. It's that simple.
01:39Here is the octopod file in Blend. I can see the bottom of one of his legs.
01:43I'm going to hold down the spacebar and then mouse drag down to pull him into view.
01:49There he is.
01:50If you look on the left side of the window, you'll see there's a projects tab.
01:54This is similar to the Solution Explorer inside Visual Studio.
01:58If I double-click on this Demo.xaml file, it will load it into the designer
02:03inside the Expression Blend.
02:05This designer area is called the Art Board in Expression Blend.
02:09Let me switch back to octopod.
02:12On the left side, the farthest left, is this tool palette.
02:17You can see there's a number of tools here.
02:19Some of these tools have a small white triangle on the corner.
02:22That means there are additional tools available for you to work with.
02:25For instance, I want to add an ellipse to octopod.
02:29So I'm going to go to my toolbar and choose this dropdown and pick the ellipse tool,
02:34and you see that my cursor changes to a cross hair.
02:37Now if I hold down the shift key while I'm drawing, it'll get a circle
02:43instead of an ellipse.
02:45Notice that it's using whatever brush I have selected in the Properties Grid.
02:50So it's a transparent blue brush.
02:53Once I have the ellipse, I can size it, I can rotate it, I can do other
02:59fun things in Blend.
03:01You can also do this in Visual Studio.
03:03I'll do a Ctrl+Z to remove the ellipse.
03:06What I want to show you next is how to change the brush that is assigned to the head.
03:12To pick the head element out of octopod, I'm going to go to the section called
03:15the Objects and Timeline.
03:16I'm going to open this section under the User Control and I'm going to drill
03:21down until I find the head canvas section and then the head itself, down here.
03:26On the right-side of the screen is the Properties pane, and at the top of
03:31Properties pane is the section where I can choose the colors.
03:34You can see I'm currently painting the head with something called the Head
03:37brush, but I can quickly change to other brushes by clicking here.
03:40Let's go back to the head brush.
03:42The head brush is painted with a gradient fill.
03:44I'm going to edit that gradient.
03:47I could edit it for just the head, but what I want to do is edit it for all of
03:52the elements that are using this brush.
03:54So what I'm going to do is go to the section that says Fill, where the little
03:58green square is and click here and then I'm going to choose to edit the brush resource.
04:04Now that pops up in the center of the screen.
04:09I have a brush currently that's got a blue and a white color, both of them transparent.
04:14I'm going to click on this white color fill and I'm going to dial in a green value.
04:20Do you see what's happening in the art board?
04:22Every single element that is painted with that brush updates to the new color.
04:26I'm going to click on OK.
04:28Then I'll show you one of my favorite tools.
04:31I use this all the time when I'm working with gradients, and I want to change
04:34the rotation or angle or the way that gradient looks on the page.
04:38It's called the Gradient tool and at lives on the tool bar.
04:42Here it is. You can also press the G key.
04:46I've clicked on the Gradient tool.
04:47Notice the arrow in the center of the art board.
04:50There are two circles on this arrow, there's a blue circle at one end of the
04:53arrow and a green circle at the other end of the arrow.
04:56These correspond to the two gradient stuffs that are part of the brush.
05:00If I grab this green circle and slide it up the arrow, it's changing the
05:06location of the gradient stuff.
05:07See how it's changing over here, too, to match?
05:11If I drag this blue icon over here, it changes the location on the arrow.
05:15I can also do something else with this arrow.
05:17I could change the angle of the gradient.
05:21I'll drag the head and the tail of the arrow to change the look of the octopod.
05:26Now my octopod is cute, but not something you see in a practical application.
05:31Let's go add an animation to our pie chart.
05:33I'm going to double-click on Demo.xaml and then I'm going to select this
05:40large blue pie slice.
05:42You can verify that it's selected in the objects in timeline.
05:46See this white bar across the pie slice? That means it's selected.
05:50The next thing I want to do is set the pie size property to zero.
05:54To do that, I'm going over this Property pane and I'm going to type in slice size.
06:01The current slice size is 255. I'm going to change this to zero.
06:06There's a couple of ways I can do it. One, I could type in a new value.
06:10Click and type in 0.
06:13But another way of doing that is with this feature of Blend, which is the four headed mouse.
06:18When I hold my mouse over this area, it changes to a four headed arrow.
06:22Now I can hold down the left mouse button and drag up and down to set the values.
06:27So what my animation is going to do is start at zero and go up to 255.
06:32So I'll set this back to zero and then I will create an animation.
06:37To do that, I'm going to go over to my Objects in timeline.
06:40I'm going to click on this plus symbol to add a new story board.
06:45Notice the red border in the art board; that means I'm recording everything at the moment.
06:49I'm going to go to my timeline over here on the left and set a keyframe at
06:56the zero point and then another keyframe at the two second mark, then I'll go back and review.
07:04At zero second, the pie slice size is zero.
07:09If I go to the two second mark, I'm going to set this to 255.
07:17I'm being warned that I'm using a kind of animation that may cause
07:20performance problems.
07:21I'm going to go ahead and click Close on this dialog.
07:24Then I'm going to scrub this yellow bar back and forth to see what will happen
07:28during the animation.
07:29I can also play the animation by clicking Play.
07:35I'll make this animation more lively by adding and easing.
07:38To do that, I'm going to click on this last keyframe with my mouse, then I'm
07:43going to go over to my Properties area and clear out the slice search term by
07:47clicking the X button.
07:48I'll come down here and choose an Easing.
07:51I'm going to choose this Bounce easing, the Bounce Out easing.
07:56And then I'm going to play my animation one more time.
07:58There is the Bounce effect.
08:01I'll save the changes in Blend,
08:08return back to Visual Studio,
08:09Visual Studio notifies me that I've made some changes with the files.
08:13I click on Yes to All and now Visual Studio is up to date with the Blend version.
08:17Just remember, Expression Blend is your friend.
08:20Spend some time learning how to work with this powerful tool.
Collapse this transcript
Using app templates to jump-start your new project
00:00Visual Studio ships with a handful of Windows Store App templates.
00:04Earlier, we saw the blank app template.
00:07In this video, we'll look at the Grid App and Split App templates.
00:11These are complex templates that are worth investigating for layout ideas.
00:15I'm inside Visual Studio and I'm going to create a new project.
00:18I will do that by going to File>New> Project, selecting my language, C#, then the
00:26Windows Store node and then Split App project template type.
00:31You can read more information about the template type over here on the sidebar.
00:35This is a two page project with one page with details and one page sets an overview.
00:40I'm going to leave the app name at App1 and click on OK.
00:47In this Solution Explorer are two pages:
00:50Split Page, which is the first page shown to the user, and Items Page, which is
00:56the detail page that is shown when the user selects an item on the Split Page.
01:00There is also a Data model folder and several other folders in here.
01:05The Data model contains Microsoft's sample data source.
01:08We'll investigate this in a few minutes.
01:10I'll press Ctrl+F5 to run the application, to see what we get with this template.
01:17This first page is the Split Page.
01:19It is populated with sample data.
01:21As you can see, Microsoft uses very generic strings for the sample data.
01:25It also uses a simple three-tone gray color scheme.
01:29That's good enough for a sample, but you'll want to change the data and colors
01:33to match your app specifications.
01:35Clicking on one of these groups loads the Detail Page.
01:40Note the left facing arrow at the top left side of the UI.
01:43That is the back button that takes you back to the previous page.
01:46Let's try that one more time.
01:48I'll go to group Title 3, the left side has the individual items and the right
01:53side has the details about those items.
01:55I'll close this app by pressing Alt+F4 and returning back to Visual Studio
02:00by choosing Alt+Tab.
02:02Next, I will show you a grid app.
02:04Rather than creating a new one, I'll open one that I create beforehand.
02:10File>Recent Project and Solutions, and I'm going to choose this
02:13GridAppWithBetterData.
02:18Close this window and let's look what we have here.
02:21There are three files here, there's a GroupDetailPage, a GroupedItemsPage and an ItemDetailPage.
02:28The first page shown to the user is the GroupItemsPage.
02:33Here's what the UI looks like, and I'm going to make this bottom half a little
02:37bit larger and I'm going to scroll down to look at this CollectionViewSource.
02:41This is getting the information from this SampleDataSource, but it just
02:46happens as when you select an item in the Solution Explorer and it opens this temporary view.
02:50Let me close that.
02:51What this CollectionViewSource is doing is it's loading the source from
02:55inside that data source.
02:57I will show you that source in just a second, and then it's creating some
03:01group binding in here.
03:02When you click on one of these items, it will take you to the GroupDetails and
03:06then you can drill down even further into the ItemsDetails.
03:08Let's look at some of the data I've created.
03:11I modified this SampleDataSource to have some sample tour information.
03:15It's for a fictitious tour company in California.
03:17Most of this is Microsoft's code.
03:20For instance, here's something that's called SampleDataCommon. That's their code.
03:24There is a class down here called SampleDataItem that derives
03:27from SampeDataCommon. That's their code.
03:30You can see, I've got titles and subtitles and paths to pictures and so on.
03:35I do have some pictures.
03:37They're in this Assets folder, here are my pictures.
03:42Then down here at the very bottom, right here, I'm starting to create the
03:49instances of those Data Items.
03:51So I'm creating a new group called Group-1 that has a name Gold Country.
03:57That's the text that the user should see, I should say, and a byline,
04:01the picture that I want to loadt and then some details, and then I do the same
04:05thing for another one.
04:06Here's the American River Bike Trail and I keep doing that for several groups.
04:10Now let's see what this looks like when you run the application.
04:12Ctrl+F5, here's our starting page.
04:17This is the group page.
04:19I have one group called Gold County and one called High Sierra.
04:23When I click on the group name, it takes me to the GroupDetailsPage.
04:27Here, I'm reading information about the group itself on the left side and on the
04:32right side are all of the tours that are part of this group.
04:35What do you think it's going to happen when I click on the Whitewater Rafting node?
04:41You're right, it takes me to a detail page about the Whitewater Rafting tour.
04:45Here's the picture and a bunch of text about the tour.
04:48Now this back button takes me back to the tour detail and back to the main page.
04:55One really nice thing about these templates is they are very layout-aware.
04:59Watch what happens when I take this page and I snap it to the side of the screen.
05:03You see how dramatically the UI changed?
05:06We now have a vertical list of data.
05:10I'll pick this Kings Canyon.
05:12It looks good over here.
05:13Now I'm going to expand the snap view and I'm back in this wide, rich format.
05:20But it remembers where I'm at in the app, so if I click on the back button it takes
05:25me back to the previous page.
05:26That's really nice.
05:27These templates are powerful and complex.
05:30I recommend you spend some time exploring the details of the template projects.
Collapse this transcript
Exploring the file structure for an app project
00:00Whenever you create a new Windows Store App, you get some default files and
00:04folders included in the project.
00:07Most of these items are essential to the success of your app or are necessary in
00:11order to submit your app to the store.
00:14I'm inside the Visual Studio and I've opened a project called
00:17DefaultFileStructure.
00:18This is a project that I created from the blank store template.
00:22I haven't modified any files.
00:25Let's start by looking in the Assets folder.
00:29This is where you store any asset files like music or image files that are
00:33included in the application.
00:35By default, you get four files in this location.
00:38Logo.png is the default background image for your Applications tile on the user start screen.
00:44It is 150x150 pixels in size.
00:48SmallLogo.png is the picture used when the application list is shown.
00:53For example, when you search for an application in Windows 8 or in the Share
00:56menu; the SmallLogo.png is 30x30 pixels.
01:00At such a miniature size you want to make sure that this icon really represents
01:04something well-known or recognizable about your app.
01:07SplashScreen.png is the initial image that loads when your application is started.
01:12Remember that this image will always be 620x300 pixels and it will be
01:18vertically and horizontally centered on your user screen regardless of their
01:21monitor resolution.
01:23Finally, we have the StoreLogo.png.
01:27This 50x50 pixel file might be the most important image in your application.
01:32This is the icon that is shown in the App Store.
01:34It is the first marketing image a user will see when deciding which new app to install.
01:39Do I need to say this?
01:40Don't ship your app with this generic PNG files.
01:43Replace them with ones that represent your application.
01:47Next, we'll look in the Common folder at this StandardStyles.xaml file.
01:53This file was lovingly created by the Win Store XAML team and filled with
01:57hundreds of style assets for your application.
02:00This template file defines to look for every control available from buttons and
02:04text boxes to fancier items like the app bar.
02:07Let me show you some of these styles, down here.
02:13When you change a style on this file, it affects every screen in your application.
02:17That is a huge benefit for XAML apps.
02:19But be cautious, the styles in the XAML file know how to adapt to changes in the device styles.
02:25If your user picks a dark theme, the styles in this file know how to adapt to match.
02:30How are these styles applied to all the UX in your app?
02:33For that answer, we need to look at App.xaml.
02:37I'll close this file by clicking on the X and then open App.xaml.
02:43If you're a Silverlight like or WPF developer, App.xaml should be familiar to you.
02:48For those new to the platform, this is where the start up code exists and
02:52where you put styles, templates and other resources that are shared throughout
02:55the entire application.
02:57Note this line here, ResourceDictionary Source equal, and this file
03:05StandardStyles.xaml is the file we've just looked at a few minutes ago.
03:10This line of XAML forces the application to load the StandardStyles.xaml file
03:15as a ResourceDictionary, making those styles available to every page in the application.
03:20There is another file affiliated with App.xaml.
03:23If I click on this triangle, I can expand this note and see a file called App.xaml.cs.
03:29This is sometimes called the Code-behind file, too.
03:32Many files have it.
03:34Here's another example, MainPage. xaml, has a code-behind file, too.
03:38There can be a lot of code concealed in this file.
03:40It's full of application level event handlers.
03:43For example, I have this OnLaunched method.
03:48This OnLaunched method runs when the app is launched normally by the user.
03:52There's also an OnSuspending method down here.
03:56This method runs when Win RT suspend your app.
03:59Let's look at MainPage.xaml.
04:02This file is the default start screen for the app.
04:04It is the first UI shown after the splash screen is finished loading.
04:08By default, this file is mostly empty.
04:11You'll need to add some UI to this file.
04:13Remember, I also pointed out there is a code-behind file here.
04:16Let's look at that.
04:18Most XAML files usually have a code-behind file.
04:21This is where you write your code for managing the main page.
04:24Well, that's not entirely true.
04:25A lot of developers prefer using view models and service calls for code, but
04:29that's a story for later.
04:31Next, I will look at this file here, the one ends with PFX.
04:36Every Store App must be signed by a certificate.
04:39This PFX file contains a test certificate generated by a Visual Studio.
04:44This certificate is replaced by a more authoritative certificate during the
04:47app submission process.
04:49Finally, we have the Package.appxmanifest file.
04:55Double-click to open this file.
04:57This is an XML file that contains everything important about your app.
05:01I'm not seeing the XML because Visual Studio includes a designer tool
05:05for editing the XAML.
05:07This is where you specify the run-time configuration for your application,
05:11including the background color, logos and supported orientation those are up here.
05:17This is where you define desired capabilities, like I need to work with the webcam.
05:22And this is where you can specify other certificates and version numbers.
05:27This is an important file.
05:28If the settings are not correct in here, your app may silently fail
05:31during debugging sessions.
05:33Worse, it could fail the App Store Submission Process.
05:36These are the basic files included in every project.
05:39Get familiar with them.
05:41You are going to spend a lot of time in their company.
Collapse this transcript
Exploring the XAML syntax
00:00You cannot be a successful Windows Store App developer without utilizing a markup language.
00:06There are two choices of markup for apps:
00:08HTML, a language utilized by millions of websites;
00:12and XAML, a language invented by Microsoft for UI Expression.
00:16Since our app is built on XAML, it's time to explore the basics of this rich language.
00:20This is a deep topic, one that is covered in the Silverlight 5 Essential
00:24Training course on lynda.com.
00:26Watch these videos listed on this page for more details.
00:31The XAML concepts described in that course apply for Windows Store Apps.
00:35In this movie, I'll provide a brief introduction to the basic XAML syntax.
00:39I'll start by looking at some of the syntax on this MainPage.xaml file.
00:44Let me add a button to this page.
00:48I'm going to do that by writing some XAML.
00:50I'll move my cursor down here to this section of the XAML and I'll make a little
00:55bit more room at the top.
01:01What I've just typed is known as an object element.
01:04This will cause an instance of the button class to be instantiated at run-time.
01:09Next, I'm going to set a property attribute.
01:12This is an attribute in my XML but it's going to cause a property to be set on
01:16the instance of the button class.
01:21I've set three property attributes:
01:23I've set the content property, the width property, and the background property.
01:28Each property attribute is linked to a property on the underlying .net type.
01:33Property attributes use a type converter to convert from the string value to the
01:38underlying property type.
01:40For instance, this steel blue is a string, but the background is expecting a
01:46brush to be applied.
01:47If you ever want to know what the underlying type is for a property, you can
01:51press F1 to see the Help page.
01:54Here is the Help page on MSDN.
01:57Notice that it says it's of type Brush;
02:00that means there is a type converter that's turning this string into a solid color brush.
02:06That happens at compile time.
02:09There's another way to assign a value to a property in XAML, it's called
02:12a Property Element. Here's how you write one.
02:17Notice how I put the period in the center of that element, button.Background?
02:22That signifies that I'm working with the button class and the
02:25Background Property.
02:26Now the reason why I'm getting a blue squiggle is because I've set the
02:30background property twice;
02:31once in a property element and once in a property attribute.
02:33Let me delete this line here.
02:41Now I need to add a solid color brush.
02:43You see how I am being more explicit here.
02:46I created an instance of a solid color brush and I set an attribute on that to
02:50blue, then that solid color brush is being applied to the background property of the button.
02:55This is a more explicit way of doing what I did in the property attribute.
03:00The power of the syntax becomes apparent when you see how to use your complex
03:04nested property settings.
03:05To see that, I'm going to open this page, PropertyElements.xaml.
03:14Here, I have a button and I'm setting the background property not to a solid
03:20color brush but to a linear gradient brush.
03:22In order to do that, I need to instantiate a linear gradient brush and set some properties.
03:27Then I need to put some other elements inside there, some gradient stop elements.
03:31This would be very hard to do in a property attribute.
03:34Here's another example.
03:36This button.RenderTransform is causing a composite transform to be applied to it
03:41with the rotation value of -7.595.
03:45That's why this button has at a tilted angle.
03:47Let me switch back to main page.
03:50I want to write a little code to change the content of the button.
03:54I cannot access the button in my code unless it has a name.
03:57There are a couple of ways to add a name to an element. One way is to use the name property.
04:07The other way to do it is to use this X: syntax, Name.
04:15Note, you can't use both at the same time.
04:17This Name property is part of the button class definition.
04:22The X:Name is part of the XAML name space, and that allows me to talk about name spaces.
04:29Let me show you.
04:30I'm going to delete this part and I'm going to go scroll up to the top of this page.
04:36Here's my page class and here are the five declared name spaces.
04:40This first one, xmlns is the default name space.
04:45It contains the button element and all the common elements used in XAML files.
04:49Next, comes the X name space, this is for the XAML language itself.
04:54This is the prefix that I use to declare the name in my element.
04:57Below that becomes a local name space.
05:00This name space contains all the classes in my project.
05:03Let me show you how to use this local name space.
05:05I'm going to come down here after my button, I'm going to use the local prefix.
05:15Visual Studio shows me three of my local elements.
05:17I'm going to choose this octopod and then close the tag.
05:22As you can see, my custom control is added to the page.
05:25That's how you use the local prefix.
05:28You bring your own items in the scope and use them in your XAML. I'll erase that.
05:33I still haven't added My Event Handler.
05:36To do that, I'm going to come up here.
05:38I'm going to type in "click."
05:40See the lightning bolt there that signifies an event attribute?
05:45And then here, Visual Studio is saying, "I can stub in a new handler for you or
05:50use an existing one."
05:51Let's use this one.
05:54Now in my code-behind page, there is an event handler that will run whenever
05:58the user clicks on this button.
05:59So it's an alternate way of wiring up an event handler strictly within your XAML file.
06:05The last XAML item I want to explore is something called a Markup Extension.
06:08I'll open this file here, Markup Extensions.
06:11This is what a Markup Extension looks like.
06:15Here is one on this grid, background equal
06:18and then I have a curly brace and some values here and then a close curly brace.
06:22Everything between the curly braces is a mark of extension where the parameters
06:25are being passed to the Markup Extension.
06:28A Markup Extension is common for data binding and style access in your XAML.
06:33It provides a way to perform a run-time calculation for a value for a property attribute.
06:38When I use the Markup Extension in my XAML, I don't have to write the code to do the task.
06:43Even better, I can use Expression Blend or the Visual Studio designer to
06:47generate the XAML for me.
06:49Let me show you two examples.
06:51You see these two text boxes at the top here?
06:53They're both blue and then this third one down here is a rainbow color.
06:57Let's take a look at this.
07:00Here's the first text box and has a name, "textHello" and the background color
07:04is set to cornflower blue.
07:06Here is my second text box.
07:08The background property is being set through a Markup Extension, a
07:12binding markup extension.
07:13So at run-time, we'll go find an element called textHello -- here it is --
07:17and we'll look up the path for the background and we'll find out whatever the
07:20background value is and we'll assign that to the background color of this text box.
07:25So that implies that, if I change this to orange or old lace, then the other
07:30control changes to match.
07:32Here's another example.
07:34This text box is getting its background from something called a Static Resource.
07:38This is also a Markup Extension.
07:40At run time, we'll look for a resource name sunnyBrush.
07:44In my case, that's up here.
07:47In the top section, way up here, in my Page.Resources, I've declared a Linear
07:53Gradient Brush called sunnyBrush.
07:56I'm pulling that out of run-time and applying it to the background of this text box.
08:01That finishes this brief XAML overview.
08:04As you can see, XAML is a powerful and efficient mark-up language that works
08:07tightly with the Win RT UI Element Libraries.
08:10In many cases, you can write a complex UI in XAML without resorting to writing
08:15much code. And writing less code is always that worthy goal, right?
Collapse this transcript
Working with C#
00:00No matter how powerful XAML becomes, there are certain tasks that could only be
00:04accomplished by writing code.
00:06C# is a language for this course.
00:08In this video, I will show how C# code is incorporated into your XAML project.
00:13This is not a C# tutorial.
00:15If you are new to C#, you should watch the C# Essential Training course on lynda.com.
00:21When you add a XAML file to a Windows Store project, Visual Studio automatically
00:25creates a companion C# file.
00:27Let me show you what I mean.
00:29I will right-click on my project and choose Add, New Item.
00:35Then I'll pick this blank page and give it a name of LiveDemo and I'll click the Add button.
00:44You'll see that inside the Solution Explorer, there's a new file called
00:47LiveDemo.xmal and another file called LiveDemo.xmal,cs.
00:53By default, Visual Studio names the code file with the .cs extension and nests
00:59it underneath the XAML file, so I can open and close it by clicking on this
01:03little triangle here.
01:04There are two files that are working with the same page.
01:08That means there has to be some sort of link established, so that Visual Studio
01:12and the XAML know how to affiliate the two files.
01:14Let me show you how that is done.
01:16Here at the top of my XAML is a page element.
01:19Immediately below that is an X:Class statement that says, "I need a class inside
01:24this project name working with C#.LiveDemo."
01:28If that class does not exist in this project, you'll get a compiler.
01:32Let's go look at the class that's inside the C# file. I'll double-click and
01:38scroll down, and look at this line here, there's a "public sealed partial class, LiveDemo:Page."
01:45There's one more criteria.
01:47This base class, the page base class, has to match the tag that's used at the top of the page.
01:53Let's go back to LiveDemo and verify that that's true. Yes.
01:56Here's the page element and is also a Page base class in the Class file.
02:01Once you have the code class configured correctly, you can write code in the code-behind.
02:04I'll start by writing this code inside this MainPage.xaml.cs file.
02:14I'll write it down here at the bottom.
02:16I'm going to override a method that's on the base class.
02:20I'll type in the override keyword and then I'm going to press the Tab key and the spacebar.
02:26Visual Studio looks at the base class and gives me a list of methods that I can override.
02:31The one that I want to override is called OnPointerMoved. There it is.
02:38Now I'll press the Tab key and Visual Studio stubs in the rest of the code.
02:43Here's what I'm going to write.
02:45I'm going to create a new variable called PointerPoint.
02:50I'm going to call this "pt" and I'm going to get an argument from this "e"
02:54variable here, I'm going to get the current pointer, e.GetCurrentPoint.
03:02I said I was going to get the current pointer.
03:04I should have said I'm getting the current point.
03:06I'll pass in a special C# word called "this."
03:10That refers to the current page.
03:12I'm finding out where the current pointer is based on the position on the page.
03:18Once I have that, I'm going to do an Ellipse1.
03:20Now I happen to have an ellipse on this page with the name Ellipse1.
03:26I'm going to set its margin to a "new thickness" and I need to provide up to
03:33four arguments in this method.
03:35The first argument is going to be the left margin of the ellipse.
03:38I'm going to use the Pointer's X value for that, so I'll say,
03:43"(pt.Position.X, 300, 0, 0)."
03:54Did you see what I did?
03:55I'm getting the current point position, setting the left margin of the ellipse
03:59and the top margin of the ellipse.
04:01I'll run the application by pressing Ctrl+F5.
04:07Now as I move my mouse on the screen, or my finger on the Touch screen, this
04:14ellipse will move to different positions.
04:16I'll press Alt+F4 to close the application and then return to Visual Studio.
04:22.NET element support events.
04:24You can think of an event as a message pass between two types.
04:27Events are raised by .NET types and you can write code to respond to those events.
04:31Events are covered in-depth later in this course.
04:34For this demo, I'll create an event handler that runs when the user clicks
04:36the second ellipse.
04:38To do that, I'm going to scroll up to my constructor and I'm going to set up an event handler.
04:45I'm going to work with the PointerPressed event handler.
04:49Next syntax is to type in the "+" symbol and then "=" and then Visual Studio
04:55offers to stub in some code.
04:56I'll press Tab once to stub in this bit of code and I'll press Tab again to stub
05:01in the actual method.
05:02I'll delete this code and write a change of the ellipse width.
05:07I'll take Ellipse2.Width = Ellipse2.Height = 200.
05:16What this code will do is when I press the ellipse, I'll change the width and
05:20height to the 200 pixel size.
05:22I'll press Ctrl+F5 to run the application and I'll touch this yellow ellipse
05:28down here with my mouse.
05:29This also works with your finger if you have a touch screen, and I now have a larger ellipse.
05:34I showed you how to override a method and I showed you how to write an event handler.
05:39It's also possible for you to place code in separate files and call that code
05:43from the code-behind file.
05:44Let me show you a demo then.
05:46I'll press Alt+F4 to close the application, switch back to Visual Studio and
05:51I've already got a Class over here called DiceFactory.
05:54I'll open that up and what I have here is a method called GetDiceRoll that takes
06:00a number of dice I want to roll and returns a random number generated from that.
06:04Here is my random number generator at the top.
06:07I've already got the code.
06:09I'll just paste it in.
06:10Open this dice code snippet, copy this, switch back to DiceFactory, and paste it
06:19here where I have this return null.
06:21What this code does is it creates a new observable collection, which I'm
06:24returning, of integers.
06:28I am going to do a "for loop" here based on the dice count and I'm going to add
06:34to my temporary observable collection, one number for each dice ever requested.
06:39What this code is doing is it's creating a new observable collection of integers.
06:44Next, I have a "for loop," which goes to the number of days counts and adds a
06:48number to this temporary collection, one for each dice request.
06:52Now let's call this method, getDiceRoll from my main page.
06:56I'll go to my main page C# file.
07:00In the constructor, I'll write this code, ListView1.itemSource = and now I'm
07:06going to make that method call to my DiceFactory.getDiceRoll and I'll ask for a
07:13three dice to come back.
07:16I have a list view on this page.
07:18You'll see that as soon as I run the application, Ctrl+F5 to run, and there is
07:24my three dice numbers, 4, 3, and 6.
07:28Feel free to use those in your local lottery.
07:31Send me half your winnings.
07:32Alt+F4 to close the application and return back to Visual Studio. To make the
07:37dice factory reusable across multiple Windows Store Apps, we need to move the
07:40code out of this project.
07:42One possibility is to place the code in a separate class project.
07:47To do that, you could go to your Solution, right-click and choose to Add a New
07:52Project, and then you can choose a Class Library for Windows Store apps.
07:59Another popular approach with tablet and MOLAP to put the code in the cloud and
08:04then call the method via the Internet.
08:06Visual Studio supports the cloud.
08:09Here's the Cloud link.
08:11You can create Azure projects in Visual Studio.
08:14Azure is Microsof't latest Cloud initiative.
08:17If you want to work with Azure, click on this link to download the azure.sdk.
08:25To summarize this brief C# introduction, C# and XAML are tightly integrated with each other.
08:30XAML is best for describing the UI features for pages and user controls.
08:35C# is best used for creating rich business infrastructure code and making
08:39function calls to the WinRT framework.
08:41You need both to build a successful store app.
Collapse this transcript
Working with the device simulator
00:00There's nothing better than testing your app on a real Windows 8 tablet,
00:04but let's face reality.
00:06You may not be ready to spend $500 for a tablet computer yet. Don't worry.
00:12Visual Studio includes a tablet simulator that lets you try your application on
00:16a virtual rendition of the real hardware.
00:18In Visual Studio, I have two projects I want to use to demonstrate the simulator.
00:23The first is ExploreCa and the second is called SimTest.
00:27Make the SimTest to start a project by right-clicking and choosing Set
00:31as Startup Project.
00:33Unlike other platforms, like Android, you don't have to go through a lot of
00:37steps to configure a simulator before using it.
00:39All you need to do is open up Visual Studio and change this drop down to Simulator.
00:46That's it. Now you are ready to try the app in the simulator.
00:49To run the simulator, simply press Ctrl+F5. To debug your app in a simulator, press F5.
00:56I'll press Ctrl+F5.
01:01Here is my app in the simulator.
01:04Currently, I'm in the mouse mode, meaning I can click on these items with my mouse.
01:09You can also simulate a single touch mode by clicking on this finger icon.
01:13This big circle represents the finger of a user touching this item.
01:18Let me see if my code still works.
01:22I would like to enlarge this image by using the pinch gesture or the stretch gesture.
01:27To work with that, I can go to this button, the Pinch and the Zoom Touch mode.
01:32Now this is hard to do with the mouse, but here's how it works.
01:36There are two circles that represent your two fingers.
01:38You move over the picture, hold down the left mouse button and then you use your
01:43mouse wheel to simulate pinching and zooming the item.
01:50Same thing works with Rotation.
01:51We'll switch the Rotation mode, move over this item, click and hold with the
01:57left mouse button, and then you use the mouse wheel to simulate spinning
02:02around that access.
02:04It's awkward but at least you can test it.
02:07There are a couple of ways of shutting down the simulator.
02:10From one, I can click on this Minimize button.
02:13That really does shutdown the simulator. It's just hides to screen.
02:17It's still down here on my task bar.
02:19Microsoft does that so it is instantly accessible later when I need it.
02:23To really kill it, you can right-click and choose Close Window.
02:29For my next demonstration, I'll use ExploreCa.
02:32Right-click and Set as Startup Project.
02:35I'm going to run the application by pressing Ctrl+F5.
02:42I want to simulate changing the resolution on the device.
02:46I can do that by clicking on this button here, Change Resolution and click, and
02:51then pick a different resolution;
02:52it's 1920x1080 on a 23-inch screen. That sounds nice.
02:58Here's what my app looks like on that page. Not too bad.
03:02To simulate another resolution, I just click on that button again and choose 1366x768.
03:09I also use this to Test Rotation, click on these buttons, rotate counterclockwise and clockwise.
03:14Click on any of these buttons to test that your application in those orientations.
03:19I also like to take screenshot functionality.
03:22See this camera icon here, copy screenshot it says.
03:25I'll click on that one once, twice, and then I'll go then to the Screenshot
03:32settings and look at the View saved screenshots and here it is.
03:38The simulator works without much fuss.
03:40It's not as fast as working with real hardware, but when you don't have a Microsoft surface,
03:45it's a reasonable solution for testing your app.
Collapse this transcript
Debugging your code
00:00Debugging is a critical skill to have as a developer.
00:03In this video, I will show you a few of the Visual Studio debugging tools.
00:08I'll debug in this application called Debugging App and all the code I'm going
00:12to be looking at is inside MainPage.xaml.
00:15I'll start by showing the MainPage.xaml UI. It's very simple.
00:19It just has three buttons that I'll use for the demos.
00:22I'll double-click on this top button to see the code.
00:26The Debug class provides a simple way to output information while debugging your app.
00:31Here, I'm calling the WriteLine method and outputting a string.
00:36I'm also calling WriteLine after this method call.
00:39Down here in this method itself, to check if tour is full method,
00:43I'm also writing out a little bit information.
00:46There is an overload of the WriteLine method, that let's me do a little bit more
00:51interesting stuff when I write the line.
00:52I can choose WriteLineIf and then I can put a Boolean expression here,
00:58(tourID<0, and then I can change this as a "In Tour Method - Negative Input").
01:09Well, I need a comma here too.
01:13I'm ready to debug.
01:14I'll press F5, which will run the application and attach a debugger.
01:18Now I'll click on this top button.
01:21Nothing visible happens inside the application, but if I switch back to Visual
01:25Studio, you can see that my three debug outputs are in this Output window,
01:30before the call, in the Tour method with a negative input and after the call.
01:36If you do not see the Output window, you can enable it by going to the Debug
01:40menu, Windows, and then choose Output.
01:45Visual Studio, like most modern ID's, supports adding breakpoints to your code windows.
01:50A breakpoint is a signal to the debugger to stop the execution of your
01:53application and break into the code editor.
01:57I'll use this code down here for breakpoints.
02:01There are several ways to add a breakpoint in Visual Studio.
02:03The easiest is to press the F9 key. That is a Toggle.
02:08Everytime you press F9, it enables or disables that breakpoint.
02:12Another way to add a breakpoint is to click on the gray margin bar on the
02:17left side of the screen.
02:19The Margin Bar also provides other options for the breakpoint.
02:22When you right-click on this red dot, you'll see some other options for the
02:26breakpoint, Hit Count, Condition, and Filters.
02:31I'm not going to use any of those in this demo.
02:34I'm ready to test my breakpoints.
02:35I need to stop debugging.
02:38I'll press in this Stop Debugging button and then I'm going to choose Local
02:41Machine and then press F5 again to debug.
02:45Now I'll click on the second button and I've hit my breakpoint inside Visual Studio.
02:51I can tell because the line of code is in yellow, and if you'll look on the left
02:55margin, you'll see there's a yellow arrow over there.
02:58That signifies, I'm sitting at this line of code. I haven't run this line of code yet.
03:02To run the line of code, I press F10 or F11.
03:06I'll press F11 and steps a single line.
03:09It skips over that line of code.
03:13I wanted to see what would happen when caseCount was larger than 30, so what I'm
03:17going to do is grab this yellow arrow and drag it back up to the if statement,
03:23and then I'm going to modify this value.
03:25Currently, caseCount is equal to 7.
03:28I can modify that by opening up my Locals window.
03:32Local shows all the variables that are in scope inside this method and choosing
03:37caseCount and typing in a new number.
03:41Now let's see what happens up here.
03:43If I hover our caseCount, it says 60.
03:46Now I'll also press F11 to step through the code and you see, I've now
03:49entered that if statement.
03:51Let me stop the debugger.
03:55You are not limited to debugging on the local computer.
03:58You can easily debug on the simulator or the tablet.
04:02Just choose a desired location from this dropdown and then press F5.
04:08Here's my app running on the simulator.
04:11I'll click on the Add Breakpoint button, and there are my breakpoints and again,
04:15I can step through this code by pressing F11 just like before.
04:19It's just as easy to work with the tablet.
04:23I'll stop debugging, choose the Remote Machine.
04:27Here comes the Remote Debugger Connections.
04:30There is my surface tablet.
04:32I'll click on this one and then click on Select.
04:36I'll press F5 to debug the application.
04:39I'll switch to the tablet now.
04:43As you can see, Visual Studio deployed the app to the surface, started the app
04:48and attached the debugger to the process.
04:49When I touched the second button here on the tablet, the breakpoint is hit
04:55in the Visual Studio.
04:56From this point, the Visual Studio debugger works the same as the other two methods.
05:00Press F11 to step through the code and so on.
05:05Let me stop the debugger and switch back to the Local Machine for the rest of my demos.
05:13The last thing I want to show you is the Watch window.
05:16The Watch window allows me to put any variables I want on the bottom of the
05:21screen while I'm debugging.
05:22Let me put a breakpoint here by pressing F9 and pressing F5 to run the
05:27application with the debugger and then click on the Watch window button.
05:31On the bottom of my screen, I have the Autos window, the Locals window, and the
05:36window I'm interested in, the Watch window.
05:38Now I can take this value here, message, and write it here in the Watch window,
05:45and then it keeps a permanent display on the bottom half of my screen showing
05:49the information for that message.
05:51Watch what happens as they step through my code.
05:53I'll run this line of code and then in the Watch window, you can see this string value.
05:58Now the red color signifies as the most recent change in the Watch window.
06:03Let me try another item.
06:04I'll put t down here, t. and I'm going to work with this tour instance.
06:10I want to print out the TourName to the Watch window.
06:14Currently, it says, t = null.
06:17Now I'll step down here to these lines of code.
06:20Finally, you can see Wine Country show up.
06:24You can also just put the entire object in here, put t like that, and Now I can
06:28click in this + symbol to see the information for each of the properties.
06:31Visual Studio is packed with debugging tools, many more than I can discuss in this course.
06:37To learn more about the rich debugging tools available, I suggest you watch the
06:41Visual Studio Essential Training course.
06:43It has an entire chapter on the Visual Studio Debugging tools.
06:47That course covers all the debugging windows, details about the breakpoints, the
06:51fantastic IntelliTrace feature and more.
Collapse this transcript
Understanding screen resolution and image resources
00:00Windows has always had limited support for high DPI monitors.
00:04That changes in Windows 8.
00:07Finally, the operating system has a consistent high fidelity look across
00:11monitors and many resolution and DPI factors.
00:15XAML developers are fortunate.
00:16When your application runs on a higher DPI device, the framework automatically
00:21scales your image sizes.
00:23That's the good news.
00:24The bad news is that just making your pictures bigger is not the optimum solution.
00:29Costumers using the highest DPI devices will probably notice the lower quality
00:34of the enlarged images.
00:35It's better to be proactive to create your own higher resolution images and
00:40include them in your app.
00:42Microsoft has picked three major scale factors for Windows Store Apps.
00:46Why standardize only three?
00:48Having fixed scaling percentages prevents objects from looking blurry when
00:53scaled up or down unpredictably and it also maintains the size of the user
00:57interface elements on the high pixel density devices.
01:01It also makes the fonts and contents look sharper.
01:04The default size is optimized for the smallest screens, the 1366x768 devices.
01:11Next is the 140% scale, which corresponds to the 1920x1080 high definition tablets.
01:19Finally, there is the 180% scale, the premium screen seen only on the quad-XGA tablets.
01:27There are some special case formats you need to consider.
01:30Windows, in certain situations, can make the Start screen tiles smaller
01:35shrinking them to 80% of their original size.
01:38Also, for accessibility purposes, there are two high contrast modes in the
01:43operating system, High contrast black and High contrast white.
01:48Switching to high contrast mode on the operating system is easy.
01:51Just open the PC setting screen and toggle the High contrast slider.
01:55Here is a before and after screenshot of some of the tiles on my Start screen.
02:00On the left, are the normal tiles, and on the right are the black and white high contrast tiles.
02:06If you choose to create multiple copies of your image assets, you need to follow
02:10a strict naming pattern for the files.
02:13This is an example of the five tiles I created for a Splash Screen image.
02:17My original file was named Splashscreen.png.
02:21I renamed that to Splash.scale-100.png.
02:27That is the default size.
02:30Next, I created a black and white version of the Splash screen and added the
02:34.scale-100_contrast-black identifier to the file.
02:39Then I created another black and white version and named it appropriately.
02:44Finally, I created two enlarged versions on the images and named them with the
02:48140 and the 180 values.
02:50Visual Studio provides an editor for listing the Visual assets for the app.
02:56After adding the images to the Visual Studio project, open the editor and
03:00indicate which image is intended for which resolution.
03:03In this screenshot, you can see that I've added the default sized and the 140% sized images.
03:10The 180% size file has not been specified yet.
03:13There are other ways to add the necessary images.
03:16We'll look for details later in this course.
03:19To summarize, Windows 8 has fantastic support for high DPI monitors and
03:23screens of many sizes.
03:26To make it easier for developers and designers, to build apps for their
03:29countless numbers of screen resolutions and DPI setting combinations,
03:33Microsoft standardizes on three sizes for your image files.
Collapse this transcript
Understanding animation in modern user interfaces
00:00When I talk to mainstream corporate developers about animation in an
00:04application interface, I often get a raised eyebrow or distrustful look.
00:09There is good reason for this skepticism, though.
00:12Animations on older versions of Windows have always been more of a UI gimmick
00:16than a useful enhancement.
00:18Plus it was never easy to determine how to add a decent animation to an application.
00:23All of that changed with the debut of Windows Presentation Foundation.
00:26Now when you look at modern UI trends, you see animations everywhere.
00:32Used correctly, they can enhance the user experience and help transition user expectations.
00:37In this video I'll take a short tour of the animations in some of the existing
00:41Windows applications.
00:42I'm going to start in the Windows Store.
00:45When you click on an item to examine it you'll notice there's an animation as we
00:50transition to the new screen.
00:53As a new information is brought into view, it slid from the right to left.
00:58The same thing happens when you click on these tabs at the top.
01:01I'm going to click on Details or Reviews.
01:05Notice how the new information slides from the right side to the left side of the screen.
01:10That usually indicates that new content is coming on to the stage.
01:14When I click on the back button, the UI for the main store screen also slides in
01:19from the right to the left.
01:20Here is another example.
01:23When you swipe in from the left side of the screen or hold down Window tab,
01:27you'll see that this switch task bar pops up.
01:30Did you see how it animated in from the side?
01:33Let me show you that again.
01:35I'll switch to the desktop and then I'll do the same keystroke, Window tab.
01:39It slides in from the left to right.
01:42You'll also see the same thing with the charm bar on the right side of the screen.
01:46Window+C will bring that up.
01:48Again, you see it animating in from the side.
01:51Now I'm going to choose Settings.
01:55Did you see what happened there?
01:57Once again it animated in from the side.
02:00I'll choose Change PC Settings and then I want you to see what happens when
02:05I click and hold or if I had a touch screen, touch and hold this Notifications item.
02:10You see there's a slight animation that tells the user that they have selected
02:14this item but have not released it yet.
02:17It's animating to the left and then when I release the mouse, it switches the screen.
02:22These I think are useful animations to show the transitions in the selections of items.
02:28There's also a closing app animation.
02:31In Windows 8 you can go to the top of the item and drag down with your finger or the mouse.
02:37Now see here's an animation.
02:38As I slide down the screen, it animated to the small rectangle and then as I
02:43move my mouse further down the screen, you'll see it suddenly animate to a
02:46smaller size and then move off the edge of the screen.
02:50That indicates to the user that you're dismissing the item, but you're not
02:53moving immediately from one size to another.
02:56You're transitioning through an animation.
02:58It makes it seem more fluid.
03:02Every app has a default start-up pivot animation. Let me show you.
03:05I'm going to click on this Mail app, keep an eye on the screen.
03:09See how it pivots into view?
03:11There's also animation here. Let's say I want to send an email.
03:13I click on this plus symbol, the UI animates in the view. Let's see the close animation.
03:19I'll click on this X button and then when I click on the Delete Draft you'll see
03:25it animate to the bottom of the screen. There it goes.
03:28There was a slight hesitation and then it moved to the bottom of the screen.
03:33For my last demo, I'd like to show you some animations in an application called Fresh Paint.
03:37These are useful animations, but they're more whimsical than the ones we've seen so far.
03:43Here is the Fresh Paint application.
03:45I'm going to dismiss the app bars by clicking in the art board.
03:49See how they animated out of the way?
03:51Now if I right-click or drag my finger in from the top or bottom, they'll reappear.
03:56Notice how they animate in and kind of bounce when they hit the bottom.
04:00That shows they're moving in and getting ready to be used.
04:02Let's see that again.
04:04Right-click, they go away and right-click they appear.
04:07There are also some nice animations when you select the brushes.
04:10You see how there's the brush moves up, but this pallet at the top of the
04:15Windows also moves from side to side.
04:17This is how you select your brush size here. Okay, watch this.
04:23It's flipping over, as I move to these items.
04:27See these pallets flipped at one point and I love the animation over here on this side.
04:32When you click on this color wheel, you'll see that big circle animate around
04:38where I choose a new color for the brush, dial in my new color, like this purple color.
04:45Click on this icon to have it rotate back to the original position.
04:51It's very nice and quite different than a lot of the other applications in Windows 8.
04:56Animations, if used correctly, can enhance the user experience.
05:00The Win Art team has spent countless hours fine tuning the animation
05:03performance, so that it is silky smooth even on limited ARM hardware.
05:08For an in-depth study of this XAML animation system, watch the animation chapter
05:12in the Silverlight 5 Essential Training course on lynda.com.
Collapse this transcript
3. Page Navigation
Working with the frame
00:00A Windows Store App is composed from this core parts.
00:05The Windows, Frame, and Page classes are examined in this chapter.
00:11UIElements are diverse set of UI widgets, List View, button, Rectangle, and a
00:16bunch of other controls are covered later in the course.
00:20Windows Store Apps are Windows Applications.
00:22Any UI shown on the screen is embedded within a window.
00:26An instance of the Window class is created at app startup.
00:31The frame is a container for content.
00:34In most cases, this content is a page.
00:37The main purpose of the frame is to facilitate navigation between pages.
00:42Therefore, it contains several navigation specific members.
00:46Calling the navigate method causes the frame to change to a different page.
00:51The frame tracks each page loaded.
00:53That way, you can navigate back and forward through the page history.
00:58The frame raises events as pages are loaded.
01:01The Navigating and Navigated events fire when the page is loading into the frame.
01:06The Navigation Progress event is raised periodically during the loading process.
01:11You can also get events when the load fails, stops prematurely and when the page load is complete.
01:18The page is intended as a container for UI elements.
01:21There are other containers like the user control on the framework.
01:24What's different about the Page class is that it has a reference to its parent frame.
01:29That means that the page has access to the navigation controls and the frames
01:33back and forwards stacks.
01:36I'd like to discuss the App Startup process before I show you some code.
01:40When the app starts, it creates a frame known as the Root Frames and adds it to
01:46the Application window.
01:48Later, in the startup cycle, the app determines which page is considered the
01:52Start page and loads that page into the root frame.
01:55Let me show you an example of this process.
01:58The code for the loading event is inside this App.xaml file.
02:03It's actually in this CS file, App.xaml.cs.
02:08Here is the code inside the C# file.
02:11The key event is here, this OnLaunched method gets called when the
02:15application is starting up.
02:17This first line of code on line 44 is getting the Windows current content and
02:22storing it in this Frame variable.
02:25Then I instantiated a new frame and put it in that variable.
02:29Finally, we set the Windows content to the frame.
02:33At this point in the Lifecycle, we have a Window, which contains a frame but nothing else.
02:39There's nothing for the user to see yet, so I need to load a page.
02:44That's what's happening on line 63.
02:46This line of code is a bit complex, but the important part is I'm calling the
02:50Navigate method, which is moving it to the page that I want, and I'm passing it
02:54in the type of the page that I'm interested in.
02:57In this case, there is a page called Main Page.
03:00Main Page is over here.
03:02MainPage.xaml and when I run the application, just double-click on this to see
03:08what Main Page looks like, it's a blue page with some simple text on the top of it.
03:12If I press Ctrl+F5 to run the application, you'll see that that page is loaded inside the frame.
03:21To shut down the application, I'm going to drag from the top of the window to
03:25the bottom of the screen back to Visual Studio.
03:28Now I have other pages inside this app.
03:30I have a page called Products Page, which looks like this.
03:34I'm going to make that my startup page by going back to my C# file, my
03:38App.xaml.cs file, and changing this to say, ProductsPage.
03:44Now when I run the app by pressing Ctrl+F5, you'll see that the Products Page
03:49is the main page of the application.
03:53Another way to shut down this window is to press Alt+F4 and then back to Visual Studio.
03:59In some situations, an app frame is added to the content of the page.
04:04When you do this, you usually add a set of navigation or loading buttons to the same page.
04:10When the user presses the navigation controls, the child pages are added to the embedded frame.
04:16Let me show you how to do this.
04:19I'm going to go back to App.xaml.cs and I'm going to change back to the MainPage.
04:25Now I'm going to go add a frame to the MainPage.
04:28So I'll double-click on MainPage.xaml and in this Editor window, I'm going to
04:34zoom down, so it's a little bit smaller by clicking Fit All.
04:40And then I'm going to open my toolbox and look for something called the Frame. Here it is.
04:46I'm going to drag this Frame over and then I need to make it so it fills up the
04:52center of the screen, so I'll do that by setting a margin here to 12.
04:58All four sides will have a 12-pixel margin.
05:00I'll set my horizontal and vertical alignment to Stretch.
05:05You'll learn more about this as the course goes along.
05:10Now the frame fills up the top part.
05:12It's covering up the text on the Main Page, so I think I'll make this margin 40.
05:16Now I'm going to load another page into this frame.
05:22Here's how you do it.
05:24Change this so it has an open and closing tag.
05:28I'm going to load this About Page. Let's see what that one looks like.
05:32The About Page is a green page. Let's go back to my Main Page.
05:36What I want to do is, inside this frame, I want to tell it to instantiate that
05:40page and load it into the Frame.
05:42I can do that by referring to the local pages that are available by using this local prefix.
05:48Here in the IntelliSense, you'll see I've got several pages to choose from.
05:51I'm going to choose the About Page and then close that tag.
05:54I'm going to press Ctrl+F5 to run the application and I see we have syntax error, so let's go fix that.
06:01The problem is the frame can only have one content and I've set it twice.
06:06I've set it once here with this content attribute.
06:09I've also set it here by putting the page within the boundaries of the frame.
06:13Now you can see the About Page showing up.
06:15Again, when I run the application, we now have a window that contained the
06:20root frame that contains a page and then that page contains a frame which
06:25contains a child page.
06:26I'll press Alt+F4 to shut this down.
06:30With that simple bit of XAML, I'm able to set the content of the frame.
06:33Unfortunately, in setting the page in this manner bypasses all the navigation events.
06:38It's better to use the frames than navigation tools.
06:40That topic, I'll discuss in the next video.
Collapse this transcript
Page-to-page navigation
00:00I have a project with a number of pages.
00:03If you look in my Solution Explorer you'll see there is an About page, a Home
00:07page, several Product pages and the Main page of the application.
00:13Let's start by looking at the UI for MainPage.xaml. This is a shell;
00:18it is going to load the child pages.
00:21So at the top of the page, I have several navigation buttons and then in the
00:26center of the page I have a frame. Let's look at the frame.
00:31Here it is in my xaml.
00:33This frame is inside the page and I've given it a name so that I can refer to
00:37it in my code behind.
00:38Now let's review what we know about the Startup process.
00:41In App.xaml.cs there is a method called OnLaunched that runs when
00:49the application starts.
00:51At some point in that process I'm loading the main page into the frame.
00:55That causes a navigation event to fire, let me show you.
00:59I'll go to MainPage and go to the code behind.
01:06This event fires when that page is navigated to by the frame.
01:10You usually use this method to set up a state for this page.
01:14So what I'm doing is immediately loading the homepage into the embedded frame.
01:19Again, using the navigate method.
01:22Let's see what this looks like when I run the app, Ctrl+F5, and here's my main
01:29page, the purple area represents the embedded frame and the Home page.
01:34Now while we're here, let's look at what this application can do.
01:38If I click on this Navigate to About Page, it takes me to the About Page.
01:43Then I can click on this Back button to move me back to the previous page, which
01:49in this case is the Home page.
01:51There's also a button to navigate to the Products Page.
01:53I'll click on that which takes me to this red Products Page and there are two
01:58hyperlinks on this page.
01:59If I click on this first one, Product A, it takes me to the Products Page with
02:04the letters AAA across the top.
02:06Now I can click on the Back or the Next to navigate between those two pages, or
02:13I could click Home to take me all the way back to the initial page.
02:17There's one other path I can take, navigate and then to Products B and then to
02:24Product B. I'm going to shut down the application, drag it to the bottom of the
02:28page, return back to visual studio.
02:32Now let's go and look at how I navigate to these other pages.
02:36Here on the Home page I have those buttons we saw and if I double-click on this
02:42Navigate to Products Page, it'll switch me over to the code behind for this
02:47Home.xaml page and as you could see, it's the same code we've been looking at.
02:52I'm using the frame to navigate. Now let's be clear about which frame this is.
02:56I'm inside the Home page, which is loaded by the Main page.
03:01So this frame represents the embedded frame.
03:05So I'm asking the embedded frame to navigate off to this Products Page and this
03:09frame to navigate off to the About page.
03:13In the Products page, I have a hyperlink and again I can double-click on these to see the code.
03:20What I'm doing in this next demo is I'm showing how to pass data from one page to another.
03:27One of the overloads of the navigate method takes an argument.
03:31So here I'm passing a simple string over to the Products A page and then in
03:37Products A, I have a TextBlock on that page called MessageTextBlock. You want to
03:45guess what's happening in the code behind? Let's look.
03:47Press F7 to switch the code behind.
03:52In the OnNavigatedTo method, which fires when I navigate to this page, I'm
03:56getting this parameter passed in called the NavigationEventArgs and I'm using
04:01that to retrieve the parameter that I passed in, which in my case was that simple
04:05string and then I apply that to this MessageTextBlock.
04:08Now you can get more complex than this.
04:10Let me return back to the Products Page and look what we're doing here in
04:14this event procedure.
04:15I'm instantiating my own type of ProductInfo, which has a product name and an InStock property.
04:21So I'm setting ProductName and InStock to true and then I'm passing that
04:26complex object in as a parameter.
04:29Now over here in Products B page in the OnNavigatedTo method, I'm going to pull
04:38that parameter out by calling e.Parameter but I need to cast it from an object
04:44type back to the correct type which is ProductInfo and once I've done that, I
04:49can get the InStock property and the ProductName.
04:52I'm setting this switch, this InStockSwitch.IsOn property to true or false based on this.
04:58Let me show you what that looks like again, Ctrl+F5 to run the app, navigate
05:05here, when I click on the Products B hyperlink I'm going to pass that
05:09information in and populate this TextBlock with the letters B and turn on this
05:14InStockSwitch from the false position to the true position.
05:18Next, I'd to look at how I wired up this Home, Back and Next buttons.
05:23So drag the app down to close it, all tabs switch back to Visual Studio.
05:28I'll switch back to the Main Page designer and look at the code for this Home
05:34button, and this Back button, and this Next button.
05:38I'll double-click on this Back button, that's one of the simpler demos.
05:42Remember that I'm loading the child pages into the EmbeddedFrame.
05:45So I'm going to use the navigation framework.
05:47I'm going to go to that frame and ask it if it has any pages in the Back stack.
05:53If it does, this can go back or return true then I call EmbeddedFrame.GoBack,
05:59which returns me to the previous page.
06:02On the Next button, I ask if there are any pages in the Forward stack and if
06:07true, I go forward and the third example, what I want to do is no matter where I
06:13am in the stack I want to go back as far as I can.
06:17So what I'm doing is I'm writing a while loop that says, Check to see if you
06:20can go back, if so go back, loop around try this again, see if you can go back,
06:27if so go back" and eventually we'll exhaust the Back stack, the while condition
06:32will no longer be true and we've reached the end of our Back stack.
06:36One last time, let me demonstrate this, Ctrl+F5, navigate through a couple of
06:42pages and then we click on the Home button to return back to the Home page.
06:46So the navigation framework provides an easy-to-use system to move quickly to
06:51any page in your application.
Collapse this transcript
Dialogs and pop-ups
00:00I'm in a project called PopupsAndDialogs.
00:03I'm going to open this MainPage.xaml file and look at the UI.
00:08I have a number of buttons on the left side of the screen, when I click on these
00:12buttons it's going to run each of the demos.
00:14Let's look at this first one, the CoreWindowDialog.
00:16I'll double-click on this button, which takes me to the code behind.
00:21Use the CoreWindowDialog to show a dialog with only a title and buttons.
00:26It is a modal dialog, which blocks access to the page UI until it has been dismissed.
00:32This example creates a new core Windows dialog with the default button, a
00:36call to dialog that ShowAsync is asynchronous, meaning that it will be called on another thread.
00:43You'll see a lot of these async methods in the framework, thanks to the C# Await
00:49and Async keywords, the call to this method is simple.
00:52Just mark the Click method with the async keyword and then mark the call to the
00:57asynchronous method with the await keyword.
01:00Let's see what this looks like.
01:02I'll press Ctrl+F5 to run the application and then I'll click on this first button to show the dialog.
01:10It's a simple dialog, it has a title and a default button.
01:14Notice I cannot click on the app unless I close this dialog.
01:22Before I show the code for the next one, let me show you what the UI looks like.
01:26I'll click on this second button and what I want you to see here is that I've changed the buttons.
01:31I have added two extra buttons and I've changed the text on those buttons.
01:36I'll choose Cancel please and then I'll drag the window to the bottom of
01:41the page to close it.
01:43Then switch back to Visual Studio.
01:46Here is the code for the second button.
01:49It starts out the same by declaring a dialog and setting its title, but then
01:54these next three lines create the command buttons.
01:57I'm adding those to a Command collection, and what I'm using to build those
02:01buttons is something called the UICommand.
02:04The dialog will show this as a button but it's got some extra data that's used.
02:08For one, it has the name of the method that gets called when the user clicks on this command.
02:14I've also supplied my own index value for this command.
02:17You'll notice that I've also written a SaveCommand method, a NoCommand, and a
02:23CancelCommand method.
02:26I've placed a breakpoint on this line.
02:28To do that in Visual Studio you press the F9 key.
02:32Every time you press the F9 key, it toggles on the breakpoint.
02:35So now my breakpoint is set.
02:38I'll press the F5 key to run this application with a debugger attached and then
02:43click on the second button, and then I'll click on this Yes, save that file
02:47button, and you see my code is in fact running.
02:52To stop the debugger, I'll click on the Stop Debugging button.
02:59For my next demo, we'll look at the CoreFlyout.
03:02The CoreFlyout window is a close cousin to the CoreWindowDialog.
03:07There are two enhancements in the Flyout.
03:09One, you can choose the location of the flyout.
03:12You set that location as a point in the constructor.
03:16Second, the user can dismiss the flyout by pressing the screen outside of
03:20the flyout boundaries.
03:22Let me show you what this code looks like when it's running, Ctrl+F5 to run.
03:27Click on this third button, there is the flyout.
03:31Do you want to signup for coupons?
03:33I'm going to click outside of the flyout to dismiss it.
03:37I'll bring it up again and this time I'll click on the Yes button, which also
03:41dismisses the dialog.
03:43If you have ever worked with the MessageBox in .NET, the MessageDialog is a
03:48closest WinRT equivalent.
03:50Here is what it looks like.
03:54Now let's look at the code.
03:55Drag the window to the bottom, switch back to Visual Studio, very simple.
04:04I instantiate a MessageDialog on this line and then I call the ShowAsync method.
04:10Here, I'm setting two strings,the title, and the content.
04:15Next, I'll look at something called a Popup.
04:17Now the Popup is different than all of these dialogs I've shown you so far.
04:21The Popup is actually a XAML element.
04:24So let me go to my XAML page to show you this element.
04:27I'll double-click on MainPage.xaml.
04:30I'll scroll down in my XAML text until I find this section.
04:36Let me scroll this up so it's a little bit less room.
04:40So inside my grid I have a Popup and the Popup is the container for any valid UI.
04:47So that means I get to specify my own UI.
04:50Here, I've created a Stackpanel with a couple of TextBlocks and a button.
04:55Now the Popup has a property called IsOpen which I can change from True to False.
05:00If IsOpen is True, you can see the Popup.
05:03If IsOpen is False, you cannot.
05:06So let's see what I'm doing inside this button code.
05:10I'm setting the location of the Popup using a random number so it moves around
05:16on the screen, and then I'm saying Popup1.IsOpen.
05:19Here is what it looks like, Ctrl+F5, and Show the Popup and there is my cousin
05:26UI with the two text blocks and a button.
05:29Let's try that again, it should show up in a different location.
05:32Remember the use for the Popup is you can create any custom UI you want and
05:36float it over the top of the rest of the application.
05:39For my final demonstration, we're going to look at something called the Popup menu.
05:43This is like a context menu.
05:46The main restriction to this menu is it can only have six commands maximum.
05:51Here, I've added three commands, Add to favorites, Remove from favorites
05:55and Save to Skydrive.
05:57Let's look at the code.
05:59I'll close this application by pressing Alt+F4 and then back to Visual Studio
06:04and here is my final bit of code.
06:07I'm creating a Popupmenu in my code, adding a few commands, this one is
06:12slightly different. We haven't seen this one before.
06:14This is the UICommandSeparator that's drawing a horizontal line on the context menu.
06:19And then finally, I call await menu Async and I position it on the screen
06:25with this Point value.
06:27In conclusion, I'd like to say the framework has a nice collection of
06:31floating dialogs and Popups.
06:33There is sure to be one that fits your needs.
Collapse this transcript
4. Layout Containers and Brushes
Understanding layout
00:00UI principles keep evolving as our devices and screens change.
00:04When the smartphones appeared, our interfaces shrank to a fraction of our desktop size.
00:09So the UI designs change to handle tiny screens and tiny input controls.
00:15Also, recently there has been a movement in modern UI to simplify and reduce the
00:19number of screen elements presented to our users on one screen.
00:22In real-world Windows Store App UI will have many sophisticated part and it
00:27contain overlapping regions.
00:30These layers might have translucent areas and different opacity levels.
00:34It might have dozens of sub-areas that are animated onto the screen as UI state changes.
00:39There can be embedded images and video and so much more.
00:44This makes a lot of work for the WinRT runtime engine.
00:47I guess I've to spend time and money ensuring that this rendering is fast and battery-friendly.
00:52It seems obvious with all the complexity that we need a framework to facilitate
00:56placing elements on the screen.
00:58This is called Layout and it means what the name implies, controlling the
01:01location and sizes of a section of the screen.
01:04Great layout system will prove resilient to changes in page size and font
01:08settings and they will also adjust the elements' positions when the screen
01:12resolution or device orientation changes.
01:14Let's talk about sizing for a moment.
01:17Each child element is responsible for determining its height and width.
01:21Most of the time, the size is set to autosize and the element can adjust its
01:26size whenever its contents change.
01:28Imagine a text box for a minute.
01:30What is the optimal width?
01:32Doesn't the answer depend somewhat on the text content of the control?
01:36A page is usually composed of scores of child elements.
01:39They can't all go in the root element so they are typically placed in layout panels.
01:43While the child is responsible for its size, the panel is responsible for the
01:47child's position on the screen.
01:49Each panel specializes in one layout approach.
01:52You can see on the bottom of this slide a few examples of these strategies.
01:57Rows and columns, that would be a grid.
02:01Absolute positioning, that would be a canvas panel, and many more.
02:06A Windows Store app page can only hold one child element or piece of content.
02:10Therefore, the page is known as a content control.
02:13There are other elements that are optimized for holding a single content, too.
02:17Examples are the button and the check box controls.
02:20Since pages and content elements only hold a single child, there must be other
02:25elements that can hold many children.
02:27There are two categories of these multi-child elements;
02:30Panels and ItemsControls.
02:32Panels are responsible for the layout of their child items and they have no UI of their own.
02:37ItemsControls are more sophisticated and can also contain children.
02:42These are controls however, not panels and can have sophisticated UI templates
02:47applied to dramatically change their appearance.
02:49ItemControls are typically used to show a list of application data.
02:55The framework ships with a handful of built-in controls;
02:59the Canvas, which arranges its children by absolute coordinates;
03:03the Grid, which excels at dividing the screen into rows and columns;
03:07the Stackpanel, which is good at stacking children elements.
03:11There is also a special panel for arranging items in the list controls called
03:15the VirtualizingStackpanel.
03:17There are other specialized panels, for example the Carouselpanel looks
03:21interesting but it can only be used within an ItemsControl.
03:26Details regarding these panels are available elsewhere in this course.
03:30At the top of this diagram, you see the panel base class.
03:33It is provided as a base class for all layout containers and it makes it
03:37possible to create your own custom panels.
03:39Since the rest of this chapter elaborates on the built-in panels, I thought in
03:43this video I would show you an example of a custom panel; to do that I need to
03:48switch to Visual Studio.
03:50There are two key parts to this example, the Diamondpanel class and
03:54the MainPage.xaml file.
03:57The Diamondpanel class derives from the base panel class, and there is a lot of
04:03code in here, I'll show you just a few of the lines.
04:06At some point in the panel's lifecycle, it has to pull each one of its
04:10children and ask the child elements what their desired size is. How wide and
04:17how tall do they want to be?
04:22That is done in the MeasureOverride method.
04:25We're overriding a method called MeasureOverride.
04:29There is something ironic about that.
04:32Here we would loop through all of our children in the elements and we would ask
04:37them for their size.
04:38In my example I'm not going to change the size of the children elements,
04:41they're all going to be exactly the same size, so I'll not ask them what they
04:45want their size to be.
04:46At a later point in the layout cycle, the panel will arrange its children.
04:50In this case it will tell the child how wide and how tall it's going to be and
04:55it also tell it its X and Y coordinate positions.
04:58Let's see how I'm doing that.
05:00Notice up here in this ArrangeOverride method.
05:05Here, on line 49 I'm grabbing all of the children controls and then I'm
05:10specifying their widths and their heights.
05:14This information is coming from a property on my Canvas, so I can set the
05:18property once and all the children have the same element dimensions.
05:22I tell the child its width and its height and I also tell the child where its X
05:27and Y coordinate goes.
05:29Then I call the Arrange method and I pass in this rectangle.
05:33That instructs the child where to position itself on the screen.
05:37Here's what it looks like.
05:38I need to go to my MainPage.xaml, I'm going to bring that Diamondpanel in the
05:43scope inside my XAML page by using this local prefix up here.
05:47This brings all of the classes in this project into scope and I can use them on the XAML page.
05:53And then you'll see down here, I'm using my Diamondpanel and I'm specifying my
05:58desired element dimensions of 110 pixels and then inside my panel I have a
06:06grid that contains a text block and an image, and that's being arranged at this position.
06:11Then the next grid that contains a text block and an image is being
06:14positioned here and so on.
06:16So it's going to arrange them in a diamond pattern.
06:19I'm also, on this button click procedure, instantiating a grid programmatically,
06:27adding a TextBlock to the grid and then adding that to my Diamondpanel.
06:32Let's see what this looks like.
06:33I'll press Ctrl+F5 to run the application.
06:36There are my six items arranged in the pattern and as I click on this button at
06:40the top, the seventh item is added and it keeps going in this pattern where I
06:45arranged them in a diagonal line across the screen.
06:50So what you see here is that WinRT provides an extremely flexible layout system
06:54that can be figured in endless permutations, one of which is sure to be the
06:58layout you are looking for.
Collapse this transcript
Understanding Microsoft layout guidelines
00:00The planning for a modern touch-based operating system at Microsoft has taken years.
00:06The Microsoft research group prototyped countless user interface variants.
00:10They looked at UX limitations on small screens and examined new opportunities
00:14with touch-based systems.
00:16Early glimpses of the new UX approach appeared on the Zune Desktop application
00:21and in the Windows Phone 7 release.
00:23The result of this work was a release of the Metro design guidelines.
00:27Metro is the code name for a new, clean tile-based system.
00:32The Metro name was dropped shortly before Windows 8 release due to legal issues.
00:37The official name for this doc is the Windows 8 User Experience Guidelines.
00:42I recommend that you download a copy and read every page in this impressive
00:47200-page guide book.
00:49The book is well organized and spacious with clear and easy to understand text and examples.
00:54Here is a list of the major sections in the guide book.
00:57In this video, I'll discuss the design and layout guidance.
01:00Microsoft recommends designing your page layout using a grid-based layout system.
01:05Their default sizes are 1 pixel, 5 pixels, known as a subunit and a 20-pixel
01:12block known as a unit.
01:14You don't need to think too much about the sizes because the layout grid is
01:17built into the Expression Blend and Visual Studio 2012 designers.
01:22I'll show you this in Visual Studio.
01:26I'm in a project called LayoutGuidelines.
01:29I'm going to open this page called BlankPage1.xaml.
01:34To turn on the gridlines you can click on this icon on the bottom of the Designer view.
01:39It's called Show snap grid.
01:41The little block shown on this grid are the units referred to in the
01:45Microsoft Guidelines.
01:47So each little block is 20 pixels by 20 pixels.
01:50I'll add a button by using my toolbox, click here to expand my toolbox.
02:01I'll drag a button to the Designer surface and I'll drag a TextBlock to
02:06the Designer surface.
02:13Now I'll dismiss the toolbox by clicking on the Auto Hide button.
02:17Because I have this Show and Hide snap grid, this will help me align where this
02:21button goes on the screen, but I could inadvertently put it somewhere not
02:26exactly on the units.
02:28To help with that problem, I can click on this Turn on and off snapping.
02:31If I turn on snapping then it snaps to the nearest item.
02:35Try that again, now it's turned on.
02:43That helps align your elements.
02:47The document provides detail layout suggestion.
02:49For example, the recommendation for an app page header is to set the left margin
02:53to 6 units or 120 pixels and the top margin to 5 units or 100 pixels.
03:00The guideline also contains details about typefaces.
03:04The suggested typeface for the page header is SegoeUI Light.
03:07SegoeUI is the endorsed typeface for Windows.
03:11The versions included in Windows are optimized to work with the Microsoft
03:14ClearType technology.
03:16For the content, the recommended left margin is six units and the top margin is seven units.
03:21As to the bottom margin, it depends on your content.
03:25For static pages, in pages where the content can scroll left or right, use 2.5 to 6.5 units.
03:32If the content scrolls up and down, you shouldn't have a bottom margin.
03:37The content should extend all the way to the bottom edge of the app.
03:40I'll modify my previous layout in Visual Studio to match the guidelines.
03:45First, I'm going to change some styles to make them look more like the default UI.
03:50I'll click on my button, open my Properties page and I'll search for style.
04:00There it is, and then I'm going to click on this little rectangle here and
04:06choose Local Resource.
04:08I'm going to choose this BackbuttonStyle, now keep an eye on the UI in the
04:14Designer and see what happens when I click on this.
04:17This is picking up a style out of this item, the StandardStyles.xaml file and
04:24applying it to that button.
04:26Next, I'm going to zoom in by holding down my Ctrl key and using my mouse wheel
04:31and then my spacebar and the mouse button to scroll this into view.
04:36But this should be placed right about here.
04:43I'll do the same with the TextBlock.
04:46I'll set its style to HeaderTextStyle.
04:53Now I'm using the recommended SegoeUI.
04:54Of course, I should line this up on the blocks to match.
05:00Rather than continue working with this one, let me show you one of
05:03Microsoft's templates.
05:05Here is this one called GroupedItemsPage and you can see they are using this
05:10layout guideline for all of their interfaces.
05:12I'll show you this page too, the ItemsDetail and the GroupDetailPage all use the
05:19same margins and units at the top.
05:23The layout guidelines also specify padding size within horizontal items and
05:27horizontal groups of items and vertical padding too.
05:30Remember these are strong suggestions from Microsoft.
05:33You can deviate from them if you desire.
05:36I'm sure that most of the game apps do layout differently.
05:38The benefits for following with the guidelines, is it speeds up your app design
05:42and it maintains unity across Windows apps.
Collapse this transcript
Using vibrant colors for elements
00:00During the last few years, Microsoft has elevated the designer role to new heights.
00:04While you may not agree with every choice made in the Microsoft product design,
00:08you have to give them credit for making UX design a prominent part of the
00:11application lifecycle.
00:12The Win 8 design guidelines have some important recommendations.
00:18For one, they say, be authentically digital, embrace what the PC screen has to offer.
00:24Other guidelines, put content before Chrome, use bold, vibrant colors, and use
00:30typography beautifully.
00:32As you can see on the screenshot, the desktop tiles for the Microsoft apps are
00:36using solid vivid colors.
00:39It's not just the tiles that use these color choices.
00:42The plum color is used within the messaging app for the Back button and
00:46the message background.
00:48The mail app uses the teal color in a similar fashion, and the SkyDrive app uses
00:53this blue color for the same purposes.
00:57Painting a UI element in an app is done with a brush.
01:00The solid color brush is the focus of this video.
01:03But there are other brushes you should know.
01:05I've created this class diagram, which shows the relationship between brushes.
01:10At the top of the list is the Abstract Brush Class.
01:13On the left is the SolidColorBrush, which we're going to be using in this video.
01:18Next is the GradientBrush, which paints with multiple colors.
01:22I'm going to be using the LinearGradientBrush later.
01:24WPF and Silverlight have the RadialGradientBrush, but Microsoft did not include
01:30that brush due to performance reasons.
01:32The ImageBrush is used to paint pixels of the picture on to other parts of the UI.
01:37And then I'll scroll over here so you can see this last one, the WebViewBrush
01:42copies the content of our browser page on to other UI.
01:45I will start by looking at this SettingBrushInXaml page.
01:51Here, I have four rectangles.
01:53One, I'm going to use a Named Color.
01:56To see that, I'm going to split the screen in half, and then scroll down to that
02:03rectangle, here, and I will select the rectangle. There it is!
02:10Here I am using a Named Color.
02:13These are prebuilt choices that Microsoft has.
02:15If I erase the value it has in here and hit the Ctrl+spacebar, you'll see a
02:19suggested list of the colors that Microsoft provides.
02:22I'll change this to CadetBlue.
02:26Next, we have the property element syntax, which allows me to specify it using a Rectangle.Fill.
02:35Here I'm saying Rectangle.Fill, that's a child element of rectangle class right here.
02:42Then I'm specifying a SolidColorBrush.Color, which is orange and a
02:47SolidColorBrush.Opacity, which is 0.5.
02:50That means this is similarly transparent.
02:53You can also specify values with a hexadecimal number.
02:57You use the pound symbol in front, and then you have several hexadecimal numbers here.
03:02These first two represent the alpha channel, transparency channel.
03:06The next two numbers represent the red channel, the two numbers after that
03:10represent the green channel, and the last two numbers, 99 in this case,
03:14represent the blue channel.
03:15If you'd rather use a designer for changing the colors, click on this rectangle,
03:20and then go over to the Properties window.
03:22I'm going to find the Brush section, which is up at the top, and open that.
03:29Then I'm going to make this a little bit taller so we can see what's going on here.
03:35I'm currently setting the Fill color to Orange, that is, the SolidColorBrush.
03:40I can also turn off the Brush entirely by clicking on this button;
03:43that sets it to Null.
03:44I can also set a Gradient Brush and an Image brush here with this Tile Brush.
03:50I'm concentrating on the solid color, so we'll click here, and look at how I
03:54can change the color.
03:55I can dial in a new color here until I find the color range that I want, and I
04:00can move my mouse around inside this area.
04:02If there is a color that already exists and you just want to grab it, you can
04:07use this Eyedropper tool.
04:08You click here, you move your mouse around till you find the color you want,
04:12say this dark blue at the top, and you click once, and that becomes the current color.
04:16You can also specify the red, green, and blue channels, and the alpha channel
04:21and the hexadecimal number.
04:23Now one of the things I like to do is work with this Hue Settings.
04:27Let me show you how that works.
04:29I'm going to pick a color of red like so.
04:34Then I'm going to click and hold on this R and choose HLS.
04:38That stands for Hue, Lightness, and Saturation.
04:43Now what I'm changing is the Hue values, the Lightness values, and
04:47the Saturation values.
04:49So I can keep this red color but just change the Lightness effect by
04:53pressing the up and down arrows, and that makes it a lighter or darker
04:57version of the same color;
04:59very handy when you want to have items that all have the same hue but different levels.
05:04As a programmer, there might be times when you need to programmatically change
05:07the color of an element, and you can do that in your code.
05:10To see this demo, I'm going to open this SettingBrushInCode, and I'll press F7
05:16to switch the code-behind.
05:23Here I'm creating a SolidColorBrush, and then I'm going to use the colors.
05:28It's not in scope, so I'm going to have to press the Ctrl+Period to use the
05:32Windows UI name space.
05:35Now I should be able to do the . operator, and pick from that same list we
05:39saw over in the XAML.
05:40I'll choose this aqua color.
05:45Then I'll copy and paste this a few times.
05:48And I forgot the new keyword here, let's type that in. There we go!
05:52I'll copy this and paste that in here and here.
05:58You can also retrieve the item out of a style.
06:00To do that, you come here, and you say this.Resources.
06:07And then I'm looking for a brush that's already predefined, so I will do sort
06:12of square braces here.
06:13Then I have to provide the key value with the name of the resource.
06:16So the one I'm looking for is Application Page Background theme brush.
06:22So you type that value in here, and that's what gets you to pull the item out of
06:27the Resource and apply it to this rectangle.
06:29Let me come in at this line and move down to the next one.
06:33Here, I'm going set it using a custom color.
06:36So I'm going to say I want a new SolidColorBrush, and then I'm going to use the
06:42color class .FromArgb method, and here I'm going to provide a byte value for the
06:54alpha channel, 255, and then a byte value for the red channel, 255, and so on;
07:010 for the green channel and 0 for the blue channel.
07:06Now when you run this application, it will apply this custom color to this fill.
07:12Finally, I wanted to show that color show up in lots of different places
07:16that you may not expect.
07:17Let's look over here in this BrushLocations. Here is a border.
07:22It has a BorderBrush property, which I can change the color.
07:28I can use the same techniques I used earlier.
07:31Here is a grid control.
07:33I can set its background property to a brush, and I can even use custom shape.
07:40Here's a custom path that I created.
07:42And I'm setting its Fill property to whichever color I desire.
07:47Painting bright single color areas in your UI is accomplished with a SolidColorBrush.
07:51The colors are predefined in the API, or you can build your own custom color set.
Collapse this transcript
Getting colorful with gradient brushes
00:00A common UI pattern in recent years is filling portions of your user interface
00:04with blended colors.
00:06In Windows Store Apps, this is accomplished with the Linear Gradient Brush.
00:10With gradients, you specify two or more colors, and the rendering engine
00:15determines the intermediate color ranges.
00:18Here in the Weather App, these rectangles at the bottom of the screen are using a gradient.
00:24It's a light blue color at the top.
00:27And at the bottom of the gradient, it blends in with the background color of the main app.
00:31I think this is a tasteful use of gradients in an application.
00:36A word of warning, gradients are performance drains on your app.
00:40Test your UI to ensure your gradients are not hindering the app performance on ARM devices.
00:46In Visual Studio, I've opened a project called GradientBrushes.
00:49I'm going to auto hide my Solution Explorer so I have more room for
00:54the properties grid.
00:56This first rectangle has a gradient applied.
00:59This is what it looks like in the XAML.
01:01Here is the Rectangle.Fill property.
01:04And inside that, I'm instantiating a Linear Gradient Brush and setting the start
01:08point and ending points.
01:11By setting the starting and ending points, you can have the gradient go from top
01:15to bottom, or left to right, or any angle that you choose.
01:18I've also specified two gradient stops;
01:21a black stop and a nearly white stop.
01:24When you select the gradient stop in XAML, you can change the color and offset
01:29in this property grid.
01:31I'm going to click on this black bar to set the black color to another value.
01:36Here is the standard Visual Studio color editor.
01:39You can dial in a color by dragging on this portion of the color editor, and
01:44then selecting the color in the center.
01:47Another way that you can select the color is to use this color eye dropper.
01:51I'm going to click here, and then go to my photograph, and choose this pink color.
01:57And then, for the white gradient, I'll choose the same item, color eyedropper,
02:05and I'll choose this purple color. It looks nice.
02:10You can also set red, green, blue, and alpha colors, and you can also switch
02:15to other color spaces by clicking and holding here on the R, or any of these other items.
02:21I can switch to CMYK, or Hue/Lightness/ Saturation, or Hue/Saturation/Brightness.
02:29Now you can see this is HSBA.
02:33This is the Alpha channel with transparency values.
02:37Another way of doing a gradient looks like this.
02:39I'll click on this rectangle.
02:41I'll click on brush.
02:44These buttons at the top represent the different types of brush you can apply.
02:48This first one means No Brush, the second means Solid color brush, the third one
02:53means a gradient brush, and then the fourth one is a Tile brush.
02:56This is typically used for images.
02:58When I click on the gradient brush, I get the default gradients down here; black and white.
03:04Now these little nodes down here can be moved on the gradient bar.
03:08So I can switch the order of the black and white.
03:11You can also add other gradient stops by clicking on this bar.
03:16And then of course you select that gradient stop, and change the color by
03:19dialing it in here in the color section.
03:27Make this one a blue color. Select this one.
03:31I have a blue color, and let's add one more.
03:33It will make this a purple color.
03:35It's not the nicest looking gradient I've ever made.
03:37I think I'll get rid of this purple stop.
03:41The way you remove a stop from the gradient bar is you grab the node, and you
03:45pull down off the gradient bar. Okay.
03:49I'm happy with that one.
03:50Now you can also rotate and transform your gradients.
03:55Let's make a simple one here.
03:57Click in this last rectangle, add a basic gradient, and then I'm going to come
04:03over to my rectangle, and click on the linear gradient brush itself.
04:09Here are a number of settings for the LinearGradientBrush.
04:11You've got different things like color interpolation values, the starting, and
04:16ending point, the collection of Gradient Stops, the Spread mode.
04:21There are different spread modes available.
04:24This is more visible if you have multiple stops.
04:26But what I wanted to show you is the relative transforms up here.
04:30So let's click on this item, and then grab this little wheel here, and this is
04:34how I can rotate the gradient around to new position.
04:38I can also do things like translate it to a new position.
04:42I'm going to use the up and down arrows on the keyboard to move it to a new location.
04:47Because I have the Reflection setting down here, you can see that as I move it
04:51to a new position, it's wrapping around to the other side.
04:55That completes the demo of gradient settings.
04:57The current Windows App Guidelines downplays the use of gradients in app UI
05:02preferring vibrant solid colors instead.
05:05My experience tells me that tasteful use of gradients can help deliniate
05:09parts of the UI in a helpful fashion.
Collapse this transcript
Exploring sophisticated layout with the grid
00:00The Grid panel is most powerful layout panel in WinRT.
00:04It is the default layout panel when you create a new XAML document in Visual
00:09Studio or Expression Blend, you automatically get a grid as the main element.
00:14When you add a grid, it contains one row and one column.
00:18It is up to you to add the additional rows, or columns.
00:21When you have the grid structure the way you want, you assign each child element
00:25a location in a suitable row and column within the grid.
00:29In Visual Studio, I'm in a project called UsingGrid.
00:32Here's the main UI of the app.
00:34It contains five buttons along the side that I want to use for the different demos.
00:40We'll start by looking at the AddRows example.
00:46On this page, I have a grid with a gray background, and I've defined three rows already.
00:51If I select the grid, you'll be able to see those in the designer.
00:55With the blue lines, you make this a little bit smaller.
00:58I'll hold down the Ctrl key and use my mouse wheel to make it a little bit smaller.
01:03Then I'll hold down the spacebar and use the mouse button to move up so it's a
01:08little bit more visible. Now I'll select that grid again.
01:10The blue lines represent the rows. There are no columns yet.
01:13If you're the kind of person that likes XAML, you can add them easily by coming
01:17down here and picking the GridColumnDefinition like so, and now I have one
01:24column, which is the default.
01:27I'd prefer my XAML to look like this.
01:30I will erase that last element.
01:32And then I'm going to copy this twice, and now I have two column blue lines.
01:41This button is not specifying a row or column, so it goes in row 0, column 0,
01:48numbering starts with a 0.
01:49I can change that by coming down here and typing in Grid.
01:55What I'm doing is setting a property on the button, but it's the grid that's
01:58interested in the information; so Grid.Row="1".
02:02That moves the button down to the second row.
02:06And now, I'll say Grid.Column="1" and now it's moved into the center area.
02:12Now I'll run the application by pressing Ctrl+F5.
02:18And then clicking on this Add Rows button, that button, the child element there
02:22is in the center cell, but you can't tell where it is because none of the lines
02:27are visible when you're running the application.
02:28Alt+F4 to close the app and I'll switch back to Visual Studio.
02:33I can add columns to this grid in another fashion.
02:36If I move my cursor up to the top area of Visual Studio, you'll see that I get a yellow triangle.
02:42Now when I click, I'm adding another column, and I'm adding another row.
02:47Let me zoom in a little bit so we can see what we're going to do with this
02:52button, and make sure that we can see the XAML on the bottom of the page.
02:56Now watch what happens when I pick up this button and move it to a new location.
03:00If you keep your eye on the XAML, you'll see that it updates the Grid.Row and
03:05Grid.Column based on my movements.
03:08If I drop the button between the two cells then you'll see that it adds this
03:13property called ColumnSpan.
03:15Same thing happens if I move it, so it spans to rows.
03:18So it's smart about this.
03:19The way that ColumnSpan works is you're telling the element to go in one column,
03:24but you're telling it that it needs to go across the boundary of another column,
03:28spanning two columns.
03:30Next, let's look at this Placing panel in Grid.
03:38Here's my grid definition, and inside my grid definition is the Stackpanel.
03:44Sophisticated layout in the framework is accomplished with nested panels.
03:47A grid is usually filled with a dense layer of child panels.
03:51This is a simple example to show what I mean.
03:53The stack panel is living inside the grid.
03:56It's the Stackpanel specifying Grid.Column="1".
03:59And then the buttons live inside the Stackpanel.
04:03I can control the sizes of my columns or my rows.
04:05There is a Width and Height property available in the row definition and column
04:09definition elements.
04:11I'll open up this file ControllingSizes.
04:18And again, I'll maneuver and then I'll size the UI.
04:22Here I can see the grid, and we'll scroll down here so we can see that row
04:27and column definitions.
04:29I already have some width set for the columns.
04:32I can hardcode a width by specifying a pixel number here. Let's put in 40.
04:38Now I have a 40 pixel wide column, the buttons in that column have been
04:42compressed in width to fit.
04:44I can also specify what's called an Auto Size.
04:46What Auto Size means is that the column is sized to whatever the widest element is.
04:53All of these buttons fit nicely in that first column.
04:55There is also a star sizing, which is what I had originally;
04:59three-star, two-star.
05:02The way this works is you're specifying a group of sizes, three plus one plus one.
05:08That's the sizes that we're using to determine the width of the unit.
05:11So this last column is one unit in width.
05:14The next and the last column is one unit in width.
05:17And the first column is three units in width.
05:20It's a way of doing proportional sizing.
05:23You can also change these sizes with the Visual Studio designer.
05:26Let's move up to the top of the grid with your mouse.
05:29Let me scroll up so it's a little bit easier to see.
05:33You can see this got three-star, one star, and one star.
05:36If I hover my mouse over these little white areas, I'll get a size pop up.
05:40Now I can come up here and choose to switch this to pixel size or to switch it
05:45to auto size, or just switch it back to star size.
05:50See how it's changing the values down here in my XAML, and I can also set some
05:54other items in here.
05:55Select Columns, Move Columns around in the screen.
05:58I'll show you auto size on another page.
06:01We'll go to Auto Sizing.
06:05Here are several rectangles inside this column, and some of these rectangles are
06:09too big to fit because I've set the column to be 100 pixels wide.
06:13We'll go up here, we'll change this to Auto and now the column size is the
06:18largest item, this blue rectangle.
06:20It's common to allow users to size the column or rows in a grid.
06:24Silverlight has the grid splitter control, but that control is missing in WinRT.
06:29The solution is to create an extra column, and add an event listener that
06:32responds whenever the user drags the sizing column.
06:35I've got a demo of that here, DynamicallySizingAtRunTime.
06:40I have a number of columns at the top, and this narrow column here, this 30
06:45pixel wide column is going to be my sizing column.
06:48Let's see what's in there.
06:49Inside that column, I put a rectangle in there with a lavender backfill.
06:53I told it to stretch vertical and horizontal so it fills up that column.
06:59I told it to span the five rows that I have.
07:02I placed it in the second column as you can see here.
07:05And then, I'm using two manipulation items here.
07:09I'm going to use the ManipulationDelta event and setting the Manipulationmode property.
07:14I'll cover these awesome touch events in more detail later in the course.
07:20Let's look at the code; press F7.
07:22Here's the splitter ManipulationDelta event.
07:25What I'm doing is through this ManipulationDeltaRoutedEventArgs, I'm getting the
07:30current translation, how far you've moved to the left or right.
07:34I am finding out the actual width of the column to the left of this cell.
07:39Now this is a name that I created.
07:41If I switch to my XAML, here's my grid, and in my ColumnDefinitions, this is my
07:4730-pixel wide column that contains the splitter, but I named the column before
07:52it and after it, column left, and column right.
07:56So I switch back to my code.
07:58I'm checking the actual width of the column left and starting in here.
08:02And then I'm making some changes.
08:04I'm setting the width base on this delta value that I got and then I'm assigning
08:08a new width to that column.
08:10So when you run the application, Ctrl+F5, I'll be able to go in here, grab the
08:17splitter, and the first column, I'm changing the size of the column, which is
08:22also changing the size of the items in there.
08:24I'm not changing the size of the right column yet.
08:27I'll leave that up to you.
08:28The grid is an essential layout control.
08:30I use it on nearly every project I create.
Collapse this transcript
Absolute positioning with the Canvas panel
00:00Sometimes you don't need all the power of the layout system.
00:03You just want to brute-force the location of an element on the screen.
00:07This is a terrible idea for data entry forms or other stand put input screens,
00:12but it's perfect when you need a drawing surface in your application.
00:15Another use is when creating logos, vector art, or custom graphics.
00:20In these cases, use the canvas panel to tightly control the spatial
00:23relationships of elements within the overall drawing.
00:27The canvas is the best performing panel because it has a simple layout system.
00:31It simply positions elements based on a provided X and Y coordinates.
00:35You use it when you need something light weight and flexible.
00:39I'm inside a project called Using Canvas.
00:42I'm going to start by looking at this BasicCanvas.xaml file.
00:46This is a page that contains a canvas, and inside the canvas are three rectangles.
00:52None of these rectangles have specified a location to the canvas, so they're all
00:57being placed in the upper left-hand corner of the canvas.
01:01If you want to change the location, you use what's called an Attach property on the rectangle.
01:06You might be surprised to find that Elements don't have an X or Y property, nor
01:10do they have a top or left property. Why is that?
01:16Remember that the child element is only responsible for telling its size to the layout engine.
01:20The panel takes care of the positions of it's children.
01:23X and Y coordinates have meaning to the canvas panel.
01:26They don't make sense in a stack panel though.
01:29So it is the canvas panel that has the left and top property.
01:32What I do is I type in the word Canvas.Left and then I'll type in 120 here.
01:40And you see that the purple rectangle moved to the left. So let's be clear.
01:46I'm in the rectangle, but I'm setting a property that has meaning to the canvas
01:51inside the rectangle.
01:52I can also move these items up or down the Z order;
01:57left, right, and forward, and backwards.
01:59I'll take this red rectangle and move it up in the Z and X. I'll type in Canvas.ZIndex.
02:11I will type a big number in and you see how it moved up in the Z order, and it's
02:16covering up the other rectangle.
02:19Let's delete that, so they go back to where they were.
02:22You probably are going to do this at some point in Visual Studio.
02:25You're going to pick up a rectangle, and you're going to drag it with your mouse.
02:28Let's try that here.
02:31Let's see what's happening in my code when I do that.
02:36Is it setting the canvas left and right?
02:40Yes! So it's setting those values.
02:42Since I'm inside a canvas, it will set the Canvas.Left and Canvas.Top.
02:46If I'm in a different kind of control say a stack panel or a grid, well
02:51stack panels, you can't drag the controls around, they always stack them
02:54back where they were.
02:55But if you're inside a grid, you don't set the Canvas.Left because you're
02:59not inside a canvas, you set the margin property instead when you drag it on
03:02the designer surface.
03:04I mentioned that canvases are sometimes used for graphics art and vector art. Here's an example.
03:09You might have seen this in other videos in this series.
03:12This is an octopod graphics that I created and I'm going to switch over to the
03:18XAML view for that item.
03:20You'll see that here is a canvas.
03:23And within this canvas are hundreds lines of layout that are drawn in different
03:30shapes, and paths, and colors within that.
03:34But I'm using a canvas here because I want to have absolute control over the
03:37positioning of every single element.
03:40In other words, this eye contains many ellipses, and I want to make sure that
03:45they're all exactly positioned relative to each other.
03:48Another use of the canvas is for a drawing surface in apps.
03:51Perhaps you want to support hand annotating a document.
03:54In that case, pop up a canvas and let the user handwrite notes.
03:59Here's an example, using Ink. Here's a canvas.
04:04I named it InkCanvas.
04:06Then in my code-behind, press F7 to switch to my code-behind.
04:10I'm working with the pointer events.
04:12You'll learn more about these in another chapter.
04:15I'm listening for PointerPressed, PointerMoved, PointerReleased, and PointerExited.
04:22There's a lot of code in here, but basically what I'm doing is getting the point
04:26where the user touch the screen, doing that here, finding out what kind of
04:32device you're working with; pen and touch and mouse.
04:36I'm also checking that they are using the left button if they're working with a mouse.
04:43Then as I move the item on the canvas, I'm creating a color stream here.
04:48I'm changing the color for every point that they render on the screen.
04:52And then I am drawing, here's the bit that does the drawing.
04:57I'm drawing a new line from the previous line position to the current position.
05:05Click the first button to see the basic canvas demo, click the second button to
05:09see octopod and the third one shows you the ink on the canvas.
05:13I'll move my mouse over the canvas.
05:15I'll hold down the left mouse button and start drawing.
05:18See how the color is changing for every line that I'm drawing on the screen.
05:28This is cool! So wrapping this all up;
05:31Canvas is great for positional layout and for drawing surfaces.
Collapse this transcript
Manipulating elements with transforms
00:00The panel Layout System is flexible, and contains many prebuilt panels, which
00:04help arrange your elements.
00:06There are certain situations however, that don't lend themselves well to
00:10manipulation strictly with panels.
00:13The WinRT XAML stack exposes a transform mechanism, which is very useful for
00:18additional layout manipulation.
00:20All transforms happen after the main layout pass.
00:23The formal name for this is the RenderTransform.
00:26The layout pass is when the element and panels determine where to position each
00:30element and choose the elements' dimensions.
00:33After the layout pass, the API performs the transforms.
00:37I'm inside a project called UsingTransforms.
00:41I'll open the ShowTheTransforms.xaml file.
00:45Here's a button with no transformations applied.
00:49Here is a button that has a ScaleTransformation applied.
00:53That changes the size of the button.
00:55In this case, I'm making it 55% width and height of the original button.
01:00This is a RotateTransform.
01:01I'm angling that at 15 degrees, TranslateTransforms move them.
01:08This is very common for animations;
01:10use a TranslateTransfrom to move something on to the stage.
01:14Here I'm doing that with this button RenderTransform; TranslateTransfrom, and
01:17I'm changing it on the X axis; 230 pixels.
01:21Let me show you how to do these live.
01:23I'll switch to this LiveDemo.xaml file.
01:26Now any UI element can have a Transform applied, even something like this
01:32TextBlock that is inside a list box.
01:35I'm going to click on this TextBlock, and I'm going to apply a Transform.
01:40There's two ways to apply the Transform;
01:42one is I can use a visual editor inside Visual Studio, the other is to go to the
01:46Properties section, and scroll down to the Render Transform section.
01:51So let me transform this Thursday with some item from the Properties grid.
01:56I'll come down here and I'll choose a Translate.
02:00I will move it in the X coordinate to 100 and press the Tab key.
02:06See how that moved to the side.
02:08If you'd rather do it with a visual editor, you can click on the item.
02:11Let's choose this Saturday.
02:13And see the white rectangles that show up around the outside edge, those are
02:17the sizing handles.
02:18It also will apply a transform if you know what you're doing.
02:22For instance, if I move my mouse over this corner rectangle, you see here's
02:26the resizing rectangle.
02:29That didn't scale the font up; Ctrl+Z to go back.
02:32If I move my cursor slightly off the edge of that white rectangle, I can now
02:38rotate that with my mouse.
02:39You see that's still applying a composite transform rotation, but I'm doing
02:43it in a visual manner.
02:46I can change the center point of this rotation.
02:48I can move this over let's say to the letter S. Now when I rotate it, you'll
02:53see it's rotating around that point.
02:56I can also skew this item, changing the relationship between the top and left
03:02angles by dragging like so.
03:04Now you see how the font size is changing, and skewing as well. Very nice!
03:10So there are two separate ways of doing transforms.
03:13I find myself using these a lot during the animation phases.
03:17If you can't find a layout panel that suits your needs, chances are that a
03:21Transform panel will solve your problem.
03:22They are the last layout edit before the UI is sent to the rendering engine.
Collapse this transcript
5. Async Programming
The async API
00:00I'm certain that you have used an application that locks up or
00:03becomes unresponsive.
00:05If you are lucky, the screen freezes for only a few seconds.
00:08The worst offenders may become so unstable that they require task
00:12manager intervention.
00:14This widespread problem is unacceptable and must be stopped.
00:18The first symptoms usually seen in a frozen app appear in the UI.
00:22The mouse stops moving for example or scrollbars fail to work correctly.
00:27This is such a common problem that some additions of Windows apply a white
00:31frost overlay to a frozen UI to signal to the user to stop interacting with the application.
00:37There is a well-known solution to the problem, never let the UI thread do any
00:41lengthy jobs, instead, delegate that work to another thread.
00:45Then the UI thread can return to its primary task, updating the User Interface.
00:51There are a number of ways to do async in Microsoft APIs, but until recently,
00:56the burden of writing good Asynchronous code was on the developer's shoulders,
01:00plus the traditional delegate callback patterns made the code a plumber's
01:04nightmare, hard to read and difficult to follow the code path as it winds through
01:09the callback functions.
01:11I'll start by showing an example of an app that uses the UI thread in the wrong
01:15way, and then show one way to fix problem.
01:18I'm in this project called UnresponsiveApp.
01:20This is not a WinRT app, this is a WPF app.
01:25I'm going to press Ctrl+F5 to run the application.
01:29I have an animation running on the screen that is moving this yellow box back
01:33and forth on the screen.
01:35If I send the UI thread off to do some work, you see that the box animation
01:42freezes during that work phase.
01:44Let's try that again.
01:50But if I use the worker thread to do the work, the box keeps animating.
01:54I'll press that one more time and you can see that here it's working and then
01:58the other thread finished.
01:59Let me show you the old school way of doing this.
02:03Here is the first bit of code, the wrong way of doing it, I showed some text on
02:09the UI thread and then I put the thread to sleep, and then I showed some text on the UI thread.
02:15The way I did with the worker thread is I showed some text on the UI thread,
02:20then I set the ThreadPool off to do the work, which meant I had to write another
02:24method called DoWork.
02:26Here's where the Thread went to Sleep.
02:28Now I'm on the worker thread, I need to marshall back to the UI thread in
02:33order to update the UI, so that's what I'm doing on line 36, by specifying a call to UpdateUi.
02:40And then in UpdateUo, I update the message box.
02:43So I had to create two methods to do this work.
02:46The Tablet and Smartphone renaissance has changed the consumer space in
02:50radical ways, but these awesome devices have limitations that tax the
02:54developer ingenuity.
02:56For one, they run on hardware that is typically low in memory with less capable
03:00graphics processors, but we want to write apps that have great UI animations and
03:05allow the users to touch screen in multiple finger gestures, plus, we want to
03:09work with network resources.
03:10For example, pulling map data to the device from our servers.
03:14These tasks need to be done correctly or the apps will freeze.
03:19Microsoft knows that if a user applies a gesture to a WinRT app and nothing
03:23happens, the consumer will deem their operating system a failure.
03:27It doesn't matter if the problem is caused by poorly written code from the app developer.
03:31The problem will taint the operating system's reputation.
03:34So Microsoft instructed the WinRT team to consider the behavior of each function.
03:39If the code could possibly take longer than approximately 50 milliseconds, the
03:44function has to be written as an asynchronous function.
03:46Microsoft also knows that most developers don't do async when they should, so
03:51they change the C# and VB languages to make it easier to do the right code.
03:56There are two APIs for writing async code.
03:59WinRT uses interfaces to expose async functionality because the runtime can
04:04support code in many languages like JavaScript, C# and C++, the API couldn't
04:10take dependencies on the .NET framework.
04:13As you can see, there are a number of interfaces like IAsyncAction and IAsyncInfo.
04:20You'll see the documentation refer to them collectively as IAsync*.
04:26If you are a .NET developer, you can also use the Task API.
04:30This library appeared a few years ago and predates the WinRT tools.
04:34For XAML, C# developers, you can use libraries in either world.
04:39The C# and VB teams decided to modify their languages to let developers write
04:44async code in a synchronous fashion.
04:46They did this by adding two new keywords to the language, async and await.
04:51These keywords work with the WinRT and .NET APIs.
04:55Here is an example of the old way of writing code.
04:58I have a UI thread on line 36 that's reading information from the text box.
05:04On line 38, I'm calling the LongRunningWork method and passing in that information.
05:09This method will get called on the UI thread.
05:13After the UI thread is done in the LongRunningWork method, it will then update
05:17the ResultTextBlock on line 41.
05:20This is the same code written using the new async keywords.
05:24Notice on line 44, the async keyword.
05:27That marks this method as waitable, meaning I can use the await keyword within the method.
05:33And then on line 49, I use the await keyword word when I call the
05:37LongRunningWork method.
05:38Those are the only changes I need to make.
05:41But now on line 49, that method gets called on a worker thread, not the UI thread.
05:47And then on line 51, the UI thread resumes where it was and continues on and
05:52updates the ResultTextBlock.
05:54As I mentioned, these keywords are very useful.
05:57You write your code in a normal synchronous fashion and let the C# compiler
06:01handle the async parts for you.
06:03When you compile your code, the compiler makes massive changes to your code to
06:07make it work in the correct async manner.
06:10You'll learn how to use these keywords in the next video.
Collapse this transcript
Working with C# await and async
00:01Async and Await keywords provide a streamlined approach to
00:04asynchronous programming.
00:05I'll show you how to use them in this project.
00:10The project is AsyncKeywords.
00:11The demo code is in this demo.xaml file.
00:14Here is the UI, there are several buttons on the left side for activating the
00:18demos and then two ListViews, which will get loaded with information during some
00:23of the demos and there's also a TextBlock over here that will get loaded
00:28asynchronously with some information from the Internet.
00:30I'll start by looking at a Synchronous call.
00:35When I click on the button, this code will run.
00:38I'm on the UI thread until I get to this point. I'm going to transition into
00:43this PrintCubesA method, which is down here and all it does is output some
00:49information to the output window.
00:52Since it's a Synchronous call, the UI Thread will also be running the code in
00:56here and then this breakpoint won't be hit until after we've exited this method.
01:01You see I have three breakpoints set up, let's see this demo run.
01:04I'll press F5 to debug the application and then click on this button.
01:11I'm on the UI Thread or the main thread here, I'll press F5 to show that we're
01:18now inside the PrintCubes, as we haven't hit this breakpoint yet, because the
01:23main thread hasn't left the PrintCubesA method.
01:26Now I'll press F5 again and we've hit the third breakpoint.
01:32Now click on the Await method call.
01:34I've hit this breakpoint in this Await_Click method.
01:38This is called from the button, I'm on the UI Thread in here, I'm going to make
01:42a call to this PrintCubesAsTask method and internally, this PrintCubesAsTask
01:48method does some work on another thread, so we will spawn off a worker thread.
01:53What's going to happen is my UI Thread will enter this and then exit and
01:57continue to run the code down here, so you'll see this breakpoint hit first.
02:02And then later, you'll see this breakpoint, which is inside this
02:05PrintCubesAsTask button.
02:08What this proves is that without the special await key, the code will just
02:13continue to run and keep processing the code after this line 53.
02:17So I'll press F5, you see I hit the Debug.WriteLine.
02:20Now I'll press F5 again, we'll return to our UI for a few seconds and then
02:26suddenly our breakpoint is hit back in the code.
02:28Our code in PrintCubesAsTask has now finished running.
02:35What I prefer to do is -- let me shutdown the application and return to Visual
02:40Studio and wait for the debugger to detach.
02:46What I would prefer to do is wait and not run this line of code until after
02:51all the code inside this PrintCubesAsTask has run, so the way I'll accomplish
02:55that is very simple.
02:57I'll use the Await keyword and you can only use the Await Keyword if you're
03:01inside a method that is marked as Async.
03:03The Async keyword enables the functionality of the Await.
03:07What I'm saying now is run the code in that method, but don't process anymore
03:12lines of code inside this method until that method has finished its work.
03:16Let's see what this one looks like with the debugger attached.
03:20I'll click on the same button and then I'll press F5.
03:26We're busy processing the cubes.
03:28The cubes is finished, I'll press F5 again and now I hit my
03:31Debug.WriteLine("Done").
03:33So that's the key part about Await.
03:35You can go do another task and let the thread that was doing the work go off and
03:39do its other things, like in my case update the UI, and then when the job is
03:43done in the other method, you can continue running the Await.
03:48You can think about it as an awaitable action.
03:51Now my method didn't return any data.
03:54This PrintCubesAsTask, it just returned an empty task, but you might want to
04:01return some data from a function, so let's look at a function that does that.
04:05Here, I've got a GetCubesAsTask and I'm going to return a task filled with a list of ints.
04:11So now when I make my call, you see what I'm doing up here, I'm setting my
04:15ListViews.ItemsSource to null and then I'm calling GetCubesAsTask.
04:18I'm going to wait on that line until it's done and then I'm going to fill this
04:24cube value here and then assign that as the property.
04:27In the meantime, the UI Thread can go back to doing what it's supposed to, which
04:30is updating the UI and listening to mouse requests and things like that.
04:35Let's switch back to my UI and then click the Get List Of Cubes.
04:38We're waiting for the results, I'm still able to move the mouse and there are
04:43the results loaded in my ListView.
04:45Of course in the real world, we're going to be working with WinRT functions and
04:50the .NET functions, so let's see how you would work with some of those.
04:54Back to the code, I'llcollapse a few of these regions.
05:03I'll open up this one.
05:06Here's the code that calls a WinRT function.
05:09I'm going to use the SyndicationClient.
05:11I'm going to specify a URI that contains an RSS Feed and then here's the key part.
05:18I'm going to make a RetrieveFeedAsync.
05:19Microsoft uses the word Async on the end of all of their methods that are
05:24awaitable, so I'm saying, go RetrieveFeedAsync with this uri.
05:29I'll wait for a few seconds and then this feed will get populated.
05:35Ready to see it in action? I know I am. Here we go.
05:39The second ListView will get filled with the information, there it is.
05:43You will run into Async methods everywhere in WinRT, even something as simple as
05:48getting information about the current user is asynchronous. Let me show you.
05:56Here, I'm getting the DisplayName of the current logged-on user, so I'm
06:00calling UserInformation.GetDisplayNameAsynch, I'm awaiting that value and then
06:06putting it in the text box.
06:08Then here, I am opening a picture, again, I'm using
06:12UserInformation.GetAccountPicture to tell what kind of picture I want, and then
06:16I open that picture asynchronously, so I use the Await keyword here.
06:20And then once that's done, it's the normal code to load that picture into an
06:24image stream and then adding it to the UserImage.
06:26This is what it looks like when you're running.
06:30The name of the account is Essential Training and there's my picture.
06:34For the last demo, we'll look at calling a .NET method, which returns a task. That's down here.
06:41The code will look very similar.
06:44I'm using HttpClient, which is a .NET class.
06:49I'm going to ask it for some HTML from my blog.
06:54I call GetAsync and I pass in the URI and again I use the Await keyword here and
07:00then I'm going to read the string out of that content and I'm going to do that
07:04in an awaitable fashion.
07:05And then once that's done, I assign it to the output TextBlock.
07:08Let's see what this last demo looks like.
07:11We're waiting and here comes the HTML from my blog.
07:16So as you can see, the new keywords greatly simplify calling Async methods.
07:20If the Async method takes a long time to run, you should show a progress
07:24report to your users.
07:25That's the topic of the next video.
Collapse this transcript
Showing progress during an async operation
00:00When you have a long running process that impacts the user experience, you
00:04should display some type of busy notification or progress report.
00:08Lucky for us, Microsoft provides progress reports from the WinRT Async libraries.
00:12I made a project called ShowingProgress and I've opened the demo.xaml file.
00:18This is the UI and it has three buttons for activating the demos and three list
00:23views for loading list of asynchronous data.
00:25I'll press F7 to look at the code.
00:28We'll start here in this region called TextProgress.
00:33At the top, I'm instantiating a syndication client which I'm going to use to
00:37retrieve an RSS feed.
00:40In my button click procedure, I clear out any items that might exist in the list
00:44view and then I call this GetFeedWithTextProgress method.
00:48Here, I'm turning off the cache.
00:51This is for demonstration purposes only, so that each time I run the demo, I
00:55pull the information from the Internet.
00:58The code is quite simple.
00:59I specify in line 36, the URI that contains the RSS Feed and then on line 37, I
01:06call the Retrieve Feed Async method and pass in the URI.
01:10I'm not using the Await keyword here, because I need to have a progress report.
01:16Down here, I'm setting up a progress method that gets update whenever this
01:20SyndicationClient has extra information for me.
01:24Here is the code, the UpdateProgress.
01:27This method has a parameter of type RetrievalProgress and it's through this that
01:32I get the information about the current progress of the downloads.
01:35Here I can get the BytesRetrieved and I can also get the TotalBytes.
01:40I'm building up a custom string.
01:42And then to switch back to the list view, I'm going to marshall over to the
01:46other thread by calling Dispatcher.RunAsync.
01:50And then inside here, I'm checking for the output list view and I'm adding some items to it.
01:55This is what it looks like when we run the application.
01:58Ctrl+F5 and when I click in this button, I'll get a list of progress text from the server.
02:05I get 4096 bytes, which means we're about 6.31% done and then I get a complete list.
02:13Next, I want to show the progress bar during this phase.
02:17The progress bar is a long time staple at the Windows Operating System.
02:20They are a common site when copying files on your computer.
02:24Done right, they provide a visual indication to the user, how much longer
02:27the task will take.
02:29In my XAML, I have a progress bar here named progressBar1.
02:34I'm going to change the values of the progress bar during the download process.
02:40Press F7 to switch to the code behind and open up this section.
02:44The code is nearly identical.
02:48I'm setting up the progress bar.
02:50I did also setup a Completed event that fires when we're done loading the
02:54information from the async call.
02:58And then the key part down here is I have this UpdateProgressBar method and in
03:04here I set the Maximum value of the progress bar to the Total Bytes we're going
03:09to Retrieve and then I set the value and every time this gets called, I set the
03:14value to the BytesReceived.
03:15Let's see what this looks like in action.
03:21On my computer this runs very fast, so you may have to keep a careful eye in
03:25this white section for the progress bar. It's done.
03:33The asynchronous download happened so quickly that it wasn't very noticeable
03:37in the progress bar.
03:39For the last demo I'll use the ProgressRing.
03:41Sometimes you have a long running process, but are not provided any progress
03:45updates from the API.
03:47In that case, it's best to show a non- determinant busy indicator to the user.
03:52In store apps, this is done with the ProgressRing control.
03:54Let's switch to the UI.
03:59Here is the ProgressRing.
04:00These are available on the toolbox.
04:02You can just drag this over to your designer surface.
04:04I've given this the name of ProgressRing1.
04:07It has a property that you can enable and it starts spinning and when you
04:11disable the property, it stops spinning, and you can't see it any longer.
04:15Let's look at the code behind.
04:19For this demonstration, I'm not going to go out to the Internet and get some information.
04:23I'm going to retrieve some information from my local computer.
04:27What I'm doing in this code is when I start the process by clicking in the button;
04:31I'm going to make the ProgressRing as active.
04:34This is what makes it appear on the screen and starts spinning.
04:37Then inside this foreach loop, I'm going out to my computer and finding all the
04:42devices on my computer, every single one of them.
04:46And then I'm loading them into this list view.
04:49When I'm done loading them, I set ProgressRing1.IsActive=false.
04:53Let's see what this looks like, Ctrl+F5.
04:57The device information will be loaded in the third column.
05:01I'll click the button now.
05:02Keep your eye in this white area for the ProgressRing. Here it is.
05:07Once again, the data loaded quite quickly.
05:10Let's see it again. Click and it's gone.
05:15We see in three different ways to show progress to the user.
05:18The code for the progress report was more complicated than simply using the Await keyword.
05:22That's because it's necessary to setup the completed and progress event
05:26handlers, and make sure that code marshals back to the UI Thread.
05:30Still having worked with other async tools in the past, I find this approach the easiest.
Collapse this transcript
Cancelling pending operations
00:00The new async tools make it easy to cancel a pending async operation.
00:05I'll show you how it is done.
00:07In this project, CancelPendingOperation, I've opened Demo.xaml.
00:12This has three buttons.
00:13This first button will get two feeds from the Internet and load them into this list view.
00:18This second button does the same task getting those two feeds but I'll make
00:22the code cancelable.
00:24And then this third button will do the actual cancel operation.
00:27Let's look at the code.
00:29F7, here is the section for GetFeedNormal.
00:33In this button click, I'm clearing out the items in the list box, if there are
00:37any and calling my custom method, GetFeedAsync.
00:41This code is retrieving some information from a website URL, loading the RSS
00:47feed and then populating the list box with five items.
00:52I'm using the LinkMethod.Take to get five of the items.
00:56Here, I'm drawing a line in the list view and then finally I'm loading a second feed.
01:01The end result is I'll have two feeds loaded in the list view.
01:05Here's what the application looks like.
01:07Ctrl+F5, click GetFeed, there is the top feed and there's the bottom feed.
01:16I need to make some changes to my code to permit canceling of the operations.
01:19I'll open this region.
01:22We'll start by looking at this line of code where I'm declaring a
01:25cancelation token source.
01:28I'll press F12 so you can see a little bit more about this class.
01:34As you can see, it provides a rich set of methods for controlling the cancelations.
01:40I need to put a try-catch block in my click handler to handle the exception it's
01:46going to get thrown when I cancel the pending operation.
01:50So we will put in a try block, press the Tab key twice and enter the code.
01:57Then I'm going to copy this line of code and move it up here.
02:07On line 64, I'm going to listen for the operation canceled exception to be
02:12thrown and then down here I'm going to do something you probably would not do.
02:17I'm going to add a message to my list view.
02:20This is something you normally wouldn't show the user.
02:22But it helps during the demonstration.
02:27And then I'll put a finally block in here and I'll set my cancelation token to No.
02:39Now my click procedure is finished.
02:42The next thing I need to do is modify my asynchronous method here, this
02:47GetFeedAsync2 and I had to make a couple of changes in here.
02:50I'm going to add a cancelation token to this method and then of course, I need
03:00to go back to the code where I'm calling this and I'll reopen this.
03:07Here when I'm calling GetFeedAsync, I need to pass in the current token that I have.
03:11I'm going to get that from my cancelation token source.
03:14Copy this and set in here. Okay.
03:18I've got it setup.
03:19My click handler is calling my method, passing in this token.
03:23I'm retrieving that here in the inbound parameter and then I'm going to use that
03:28in a couple of places in my code when I make my async calls because I'm making
03:32this as an async call, I need to be able to interrupt this call.
03:37I'll pass my token into this as task and then I'll do the same bit of code down here.
03:47Now all I need to do is interrupt the flow and this cancel click handler.
03:53I'll call it cancel and then for my demonstration purposes, I'll add something
03:58to my feed list so we can see this happening on the screen.
04:05Let's see if this works.
04:06I'll press Ctrl+F5.
04:11I'll start the operation and then I'll click Cancel.
04:18As you can see in the bottom, this feed it says feed download was cancelled and
04:21then you see my exception was thrown in the original click handler.
04:24Let's see that one more time.
04:26I'll click GetFeed with cancel, we're starting to load the information and I
04:30choose to cancel the pending download from the second RSS feed.
04:34It's important to provide cancelations for any long running process otherwise
04:38your users will feel helpless when they can't find a way to stop the process.
04:42The WinRT team proves once again that they are striving to make a scalable and
04:46responsive system for your applications.
Collapse this transcript
6. View States
Understanding the view states
00:00Windows 8 has a standardized approach to screen size.
00:03By default, a Windows Store App starts in full screen mode.
00:07There are other sizes and orientations though.
00:10Collectively, these are known as View States.
00:14Windows also enforces a standard for showing multiple simultaneous apps.
00:18We can have one app on the screen or two.
00:21These are the only options for Windows Store Apps.
00:23Sadly, for those developers with multiple monitors that two app rule still
00:28applies, two apps are all that are allowed on the primary monitor.
00:31You cannot have additional apps on the secondary monitors.
00:35This is the full view state where the app takes up the entire screen.
00:40It is the default state for all apps.
00:43When the user rotates the device, the app can enter the full screen portrait state.
00:48You can opt out of this view.
00:50In that case, the UI stays oriented in the landscape mode.
00:55The default for a new Visual Studio project is to support all rotations.
01:00You can change the default by modifying the app manifest.
01:04Here, I've disabled the two portrait modes.
01:07The snap mode is one of the unique features of a Window Store App.
01:11The user can drag an app to the left or right side of the screen and the
01:15operating system snaps it to the edge of the device.
01:18This way, the user can have two apps on the screen at the same time.
01:22There are two view states that correspond to these two app sizes.
01:26The main app is considered to be in the filled state and the snap app is in the snap state.
01:32Snapping is only supported on devices with a minimum resolution of 1366x768.
01:39The snapped app is 320 pixels wide.
01:42For those interested in trivia, the splitter between the two apps occupies
01:4622 pixels in width.
01:49Here is an important point.
01:50While you can opt out of the portrait mode, the same cannot be set for snapping.
01:55The user can always put your app in snap mode.
01:59This means that you must consider how to change your app UI for the snap state
02:04otherwise your UI will get truncated or squished which makes your beautiful full
02:08screen app look shoddy or incomplete.
02:11Good design is important for this narrow slice of your app.
02:14One option that is popular is to show a limited subset of you app data.
02:19For a game, that might means showing only the current gamer score or information
02:23about the player achievements.
02:25For a movie app, this snap view might show the movie queue or smaller version
02:29of the video player.
02:31The weather app illustrates how to change the UI in response to the changes in view state.
02:36In the snap state that has the basic info at the top and a vertical list showing
02:41the upcoming days' forecasts.
02:45In full view landscape state, it shows a big image at the top and a
02:48horizontal scrolling list.
02:51In full view portrait state, the UI is similar to the snap state, only bigger.
02:57The key point for this topic is that you must think about the various ways
03:00your app will be displayed on a device and design an interface that optimizes
03:05for each mode.
Collapse this transcript
Handling orientation changes
00:00Handling an orientation change is easy because the framework display properties
00:05class does all the work.
00:07Once you know the orientation, you modify the app UI to fit the new orientation mode.
00:12I have this project called Handling Orientation Changes.
00:15There are two pages that we're going to examine, MainPage and AllTours.
00:19Let's start with MainPage.
00:21I'll double-click on this file.
00:24At the top of the page is some large text that's centered on the page.
00:30Within the page, I have a frame that is currently hosting a child page called AllTours.
00:35Let's see what the XAML looks like for AllTours.
00:40This is a child page. It has a list view and it has a data view.
00:48Details about these controls are available in other videos.
00:51Here's a short summary.
00:52List view shows items in a vertical list, while grid view shows them in horizontal list.
00:58I have two different views of the data inside this page.
01:01Now let's go see how I switch the orientation.
01:04On the MainPage, I need to listen for the change in orientation so I'm going to
01:08do that inside this C# file.
01:12Here, inside the MainPage constructor, I'm going to setup an orientation change
01:18event handler and then I'm going to call a method that's called UpdateUI.
01:23An UpdateUI takes a very brute force approach to changing the controls.
01:30I'm going to check to see if I'm in the landscape orientation or the
01:35landscape flip orientation.
01:38If so, I'm going to go find some controls and change their font size.
01:41This is the header on that page.
01:42I'm going to change the font size to 96 and 66.
01:45I'm going to change this column width to 220 pixels, but if I'm in portrait
01:51or portrait flip mode, I'm going to make the font sizes smaller and the first column narrower.
01:57There are better ways of doing this but this does demonstrate that I will change
02:01the sizes of UI when I switch orientation modes.
02:05In the AllTours page, I'm also listening to the orientation change fit and
02:12setting up a handler.
02:13What I'm doing inside this UpdateUI is I'm just turning on and off the
02:18visibility of those two controls so I have the tour list view and the tour grid view.
02:24This is the vertical view and this is the horizontal view.
02:28When I'm in the landscape mode, I collapse the list view and I show the grid
02:33view otherwise if I'm in the portrait mode, I show the list view and I hide the grid view.
02:39Let's see what this looks like in the simulator.
02:42I've chosen the simulator from my drop down list.
02:45I'll press Ctrl+F5 around the application and here's my application in landscape mode.
02:50Now the simulator supports this rotate buttons.
02:52I'll click in this rotate clockwise and you'll see that the text change size at
02:58the top of the screen.
03:00The column down the left-side is narrower and I'm now using that list view to
03:05look at the data inside the child page.
03:08Now let's rotate it again, keep an eye on that text at the top of the page, it
03:13gets larger, the left column gets larger and I'm back in the horizontal look of the child page.
03:21I'll rotate two more times just so you can see what the result looks like.
03:29Now the simulator is slow at changing orientation.
03:32If you try it out on your hardware device, you'll see that it reacts much faster.
03:36Thanks to the display properties class.
03:38It is simple to write code that reacts to orientation changes.
Collapse this transcript
Working with the snapped state
00:00An app can be snapped by the user anytime.
00:03When snapped, your app occupies a 320 pixel wide location on the left-side or
00:09right-side of the Window.
00:10On the screen, I have two apps.
00:13Snapp to the left is the Bing weather app and in the filled mode is the people app.
00:17Now I can drag the splitter over to the right-side of the screen and now, its
00:22the people app that is snapped and the weather app that isn't filled.
00:26I can close the people app and then a weather app takes up the full screen mode.
00:33You can always move to snap mode again by grabbing the app and pulling down,
00:37sliding it to the side until you see the snapped splitter, and then let go.
00:42All right, let's close that app and see how we can write this in Visual Studio.
00:47I'm in Visual Studio and I've opened this MainPage.xaml.
00:51I've created this nice looking page for the full screen state.
00:55Let's see what this looks like in the snapped view.
00:58It's not necessary to run the app to test different view states because Visual
01:02Studio provides that the device window for that purpose.
01:04To see the device window, go to the Design menu and choose this first item.
01:10Then I'm going to click this AutoHide button to dock it to the left-side of the window.
01:14While I'm adding Windows, I'll also add the document outline window.
01:18You can find that in View>Other Windows> Document Outline, or press Ctrl+Alt+T.
01:27Let me size this window and make them both about the same size and then I'll use
01:31the spacebar and the Mouse button to move this in where I can see it.
01:34In the device view, I have four buttons, each one maps to a different view state.
01:39This first one represents Landscape, the second represents Filled, the third one
01:43represents the Snapped state. Yuck!
01:47My app looks terrible in this state.
01:49The fourth button represents the Portrait state.
01:52So what I want to do is see how to design this in snapped state to make it look better.
01:58To do that, I've added some special elements to my UI.
02:03Here I've got a grid and I have two children grids, I have a full-size grid,
02:07which contains all the UI for the full state mode, and I have the snap grid
02:12which contains all the UI for the snapped mode.
02:15Currently, FullSizeGrid is visible and SnappedGrid is not visible, so let's alter that.
02:21I'll click on the FullSizeGrid, go over to my properties window, if you don't
02:25see this you can press F4, and then I'll scroll down here to Visibility.
02:29If they're not in alphabetical order, you can change it by choosing to arrange by name.
02:35I'll set my visible property to collapse for the FullSizeGrid and visible
02:41for the SnappedGrid.
02:42I'm going to switch states, I did something wrong, let's try it again.
02:47Go to full-size view, SnappedGrid, this should be visible, not collapsed.
02:54Now let's try this.
02:55We'll go to the snapped mode and it looks great.
02:58Now I'll show you the code that can switch between these modes.
03:01First, let me set this back to the previous values.
03:04I'll set this to Collapsed and this to Visible, and then we go to my
03:12MainPage.xaml.cs file.
03:16In the constructor, I'm listening for the Window.Current.SizeChanged event and
03:22I'm running this code whenever that happens.
03:24Let me hide this window so we got more code on the screen.
03:28And then going to the application view and getting the current view size, let's
03:33turn that right here, and then I've got some code that if I'm in the snapped
03:38view, switches the visibility of these two grids. Makes one visible and one collapsed.
03:44If I'm in the full screen landscape mode, I switched them to the opposite settings.
03:49Also, before I run the application, let me show you one more bit of code.
03:53You can attempt to programatically unsnap the application by calling
03:56ApplicationView.TryUnSnap.
03:59Now I have some notes in here and I say, "Don't be a bad neighbor."
04:03You should really only unsnap if you need the extra space if you have a feature
04:07that needs that room.
04:08Let's see how this works.
04:09I'm going to press Control F5 to run the application.
04:12Let's start some full screen view, I'll grab the top of the application;
04:17slide it down, move to the side till I see the splitter. Nice!
04:22It changed to the snapped view.
04:26I'll make it back to full screen and it switches back to the other view, and
04:30then I'll snap it to the right-side of the screen and then I'll attempt to
04:35unsnap and see if that works.
04:38Yes, I have success! To review:
04:41I created two UI's for the page, one for each view state that I want to support.
04:46Then I wrote the code to handle the view state changed.
04:49This could get complicated if my app has lots of pages.
04:52In that case, I should create a base class that handles the layout features and
04:56have each page inherit from the layout aware page.
Collapse this transcript
Why Visual State Manager is the best tool for view states
00:00I have a confession to make.
00:02I enjoy designing my app interfaces in Expression Blend.
00:05For certain tasks, it is the best tool to use.
00:09Today, I'm going to show a Blend tool called the Visual State Editor that
00:12simplifies creating a UI that change based on view states.
00:17This is the page that I'm going to change.
00:19I'll double-click on it in Visual Studio and I'm going to look at it in the xaml only view.
00:24I'll do that by clicking on this Collapse pane and then switching to xaml.
00:31Here I have a grid with two children grid.
00:34One that holds the UI for the Fullsized state and one that holds the UI for the
00:39Snapped state, and then in my code behind I've got a little bit of code that is
00:44listening for the SizeChanged event, and then detecting in that event what the
00:49CurrentViewState is.
00:51If it's in the Snapped view state then I set the visibility of the SnappedGrid
00:55to visible and make the FullSizeGrid not visible.
00:59And if I'm in the FullScreenLandscape I'd do the opposite.
01:02I'll press Ctrl+F5 to see what the application looks like.
01:05It looks good in full screen mode and I'll snap it to the left and it looks
01:13good in the Snap mode.
01:14Close the application and return back to Visual Studio.
01:18What I want to do now is add some visual states using Expression Blend.
01:24To do that, I'm going to right-click on MainPage and choose Open in Blend.
01:30There are currently no visual states set up in the Visual State editor so
01:34I'm going to choose this States tab over here and I'm going to click on this Add state group.
01:39If I choose the right name, I can collaborate with the device manager over in Visual Studio.
01:44The name I need to choose is Application View States.
01:53Next, I'll add two states by clicking on this plus button twice; once, twice,
02:00and I'll rename these.
02:01This first one I'm going to call Snapped and the second I'm going to call
02:05FullScreenLandscape.
02:11These names match the same names that Microsoft uses in their API.
02:15Do you see that red border in the center of the screen around the art board?
02:20That signifies that any changes I make to this view at the moment, while I
02:25have the FullScreenLandscape selected are stored inside that state, and any
02:31changes that I make while the Snapped state is selected are stored inside the Snapped state.
02:36So what I want to do is on the FullScreenLandscape, I want to open this section
02:40down here and find my FullSizeGrid and then make sure that it's visible and that
02:45the SnappedGrid is not visible.
02:48Then when I'm in the Snapped state, I want to make sure that the FullSizeGrid
02:53is collapsed or not visible and the SnappedGrid is visible.
03:00Now I'm ready to save my project.
03:02I'll choose Save All and then I'm going to return back to Visual Studio.
03:06Visual Studio detects that I've made a change to the files in this prompt "Do
03:11you need to reload?"
03:12I'll say Yes to All and then I'm going to go to this code I wrote earlier, the
03:18SizeChanged event and I'm going to comment out the If statement.
03:23So I'll select all of that text and press Ctrl+K, Ctrl+C. Now I'm going to copy
03:30this bit of code here and paste it in and save some time.
03:34So what this says is to the Visual State Manager, this is the class that
03:39controls which state you're currently looking at, your custom state that is, and
03:45I'm saying, Go to my custom state and I have to pass it a reference so I'm
03:50passing it a reference to the current page and then next I need to pass it the
03:54string identifier for the state.
03:56I have Snapped and I have FullScreenLandscape.
03:58So what I'm doing is just taking the current state and getting the string
04:01version of that and passing that into the state engine.
04:05This last argument is true, which means I want to use any transitions that are
04:09available for the states.
04:11Now let's see what this looks like when we run it, Ctrl+F5.
04:16It looks good in full view and then it looks the same as it was before in the
04:21Snap view, close this app and return to Visual Studio.
04:25Now I know what you're thinking, using the Visual State Manager in my code doesn't
04:29seem like that much of an improvement over the previous version, but it is the
04:33better solution over all and here is why. For one, I reduced the amount of code
04:37that I had to write in my SizeChanged event.
04:40I'm able to use the GoToState that makes it much simpler, but here is where
04:45it gets even better.
04:46Remember that I can do any state changes in my states in Expression Blend.
04:51Any visualization I can conceive in my UI I can store in that state, that
04:55makes it much more powerful, plus I can choose an animation between the different states.
05:02Let me show you how to do that.
05:04I'm going to save the file here, return back to Expression Blend and I'm going
05:13to add an animation to this Snapped state.
05:16To do that I select the Snapped state, then I go down here to the Objects and
05:20Timeline and I choose the SnappedGrid in this Stack panel.
05:26What I want to do is rotate this Stack panel so you won't see the Stack panel
05:30and then it rotates at a 90-degree angle in so you can see the images that are
05:35part of that Stack panel.
05:36So to do that I'm going to click on this button here, Show Timeline and these
05:42numbers represent seconds on the timeline.
05:44I'm going to click here to add a keyframe on the zero mark and I'll drag this
05:51yellow marker over to 1 second and add a keyframe and then I'll do the same
05:56thing at 2 seconds, add a keyframe.
06:00Then I'll rewind and I'll start making my changes.
06:03I'm going to rotate this, so I'm going to use a transformation to do this.
06:07I'll go over to this Properties section on the right side of the window, scroll
06:12down to the Transform section.
06:15I'm going to use it what's called a Projection transform on the Y-axis.
06:19So I'm going to start at 90 degrees at the zero second mark and then I'm going
06:25to go to the 1 second mark.
06:29Did you see that on the Designer, it rotated while I was moving the timeline?
06:34I'm going to set this to 90 degrees and then we're going to go to the 2-second
06:40mark and I'm going to set it back to zero and then I'm going to use my spacebar
06:46and my mouse button to move this in so it's a little bit easier to see.
06:50So if I drag this item along the timeline, you can see what's going to happen.
06:55We'll start by not seeing anything when we load the Snapped view.
06:58We're going to make the grid itself visible and then we're going to take Stack
07:03panel and we're going to rotate it to 90 degrees so you can't see it.
07:06Then after one second, we're going to start animating it into view so you can
07:11see the full beauty of those pictures.
07:14Are you ready to see this in action?
07:16I can run this from Expression Blend by pressing the same keystrokes you do in Visual Studio.
07:21Let me save my project first and then I'll press Ctrl+F5 to run the app.
07:29Here we are in the full screen mode and I'll drag it over here and when I snap,
07:34watch for the animation. That was cool.
07:38Let's do that one more time.
07:40I'll snap it to the other side. Ready?
07:47Now you've seen a few of the actions you can do with the Visual State Manager
07:51and the Blend state tools.
07:52I'm sure you'll have fun creating your own rich user interactions with
07:55this compelling tool.
Collapse this transcript
7. Understanding the Event Model
Understanding routed events
00:00The .NET framework has supported events since the very beginning.
00:03Before we look at WinRT events, let's review .NET events.
00:07An event is a message that is sent via the .NET framework from one type to another.
00:11It might be from a button to the page that contains the button.
00:14It might be from the TextBox to a Window.
00:18In .NET, when you create an event, you add it to a type definition.
00:22It is defined within the type.
00:23An event is considered to be a type member just like methods, fields, and properties.
00:27The event is raised within the type.
00:30For example, a TextBox raises the text changed event whenever the text changes
00:34to the text content.
00:36Later, a developer creates a page that contains an instance of a TextBox.
00:41To subscribe to the TextChanged event, the developers create a function in the page code.
00:45This function must be written to match the event signature.
00:48Then the developer registers their subscription with the .NET framework.
00:51When the TextChanged event is raised, the .NET framework dispatches the event
00:55to each subscriber.
00:57Routed events are an enhancement to the .NET event model.
01:01Routed events are events that are designed to work well with a tree of elements.
01:04XAML UI is composed of complex tree of elements.
01:08It's not uncommon to find hundreds of branches in this tree filled with grids
01:12and stacked panels and other elements.
01:14In XAML, you can have complex content within the controls themselves.
01:18For example, the button can contain sub elements.
01:21Perhaps a Border, within that Border a Grid and within that Grid is an Image.
01:25The problem for the UI developer is that it's messy, tedious and error prone to
01:30create event procedures for a composite UI.
01:33In the .NET world, you have to add a procedure to every child element of
01:37the parent control.
01:38Imagine, a button containing an image sub element.
01:41I would have to write a pointer pressed event for the image and it also have to
01:46write it at the button level.
01:47With routed events however, events can bubble up to the handler in the parent element.
01:52Here is an example of the button with nested content.
01:55The XAML on the slide will render a button that looks like the screen shot.
01:58In the XAML, I have a button that contains a border that contains a stack panel
02:04that contains an image.
02:06You can see that over here.
02:08Now if the user were to click on this blue border, we'd have to have an event
02:13fire for the border.
02:15Prior to routed events, I will have to write one event procedure for each of these elements.
02:20Routed events are a simple and elegant solution to this dilemma.
02:23The routed event traverses the tree of UI elements and it calls every registered
02:27event handler on the way up the tree.
02:29It starts at what is called the originating leaf and moves up to the root node.
02:34It always moves in an upward direction.
02:36You have the pointer released handler at the button level and no handlers for the children.
02:40Now when the user touches each child element, the parent's handler is invoked.
02:45Each routed events sends a package of extra data to the event handler.
02:49Most events use the RoutedEventArgs or one of its derivatives as a container for
02:53the transferred data.
02:54There are about 20 of these specialized RoutedEventArgs and I've listed them on
02:58the right-side of the screen.
03:00This is a class diagram containing information about three of these helpful classes.
03:05At the top of the diagram is the base class named RoutedEventArgs.
03:09It has the original source property, which provides a reference to the original
03:12leaf node, to the event handler.
03:15The PointerEventArgs contains properties for the current pointer, which could be
03:19a mouse, finger or stylus.
03:22It also has a Method, GetCurrentPoint that provides the location where the press occurred.
03:27The KeyRoutedEventHandler contains properties identifying which key was
03:31pressed on the keyboard.
03:33Finally, there is the DragEventArgs, which has the data property, which contains
03:38the information being dragged across the screen.
03:42Not all routed events will bubble.
03:45One final point of clarification for those who have worked with other
03:48XAML frameworks, most of the events you will see in the API use a
03:51parameter of RoutedEventArgs.
03:52If I Click, GotFocus or SelectionChanged events are examples of this style of events.
04:00The quirky factor about these events is that they don't bubble up the element tree.
04:04even though they are using the RoutedEventArgs parameter.
04:08The reason for oddity is historic.
04:11In WPF, all elements traverse the element tree.
04:14The Silverlight team kept the RoutedEventArgs and their event signatures, even
04:19though their eventing model is slightly different.
04:21They did this to leverage the existing designer tools in Visual Studio
04:26and Expression Blend.
04:27Now WinRT is following in Silverlight's footsteps in creating event signatures
04:31that use RoutedEventsArgs without the events actually being a true routed event.
04:36The best way to tell if an event can bubble up the tree requires knowledge of
04:40how the dependency system works.
04:42A simpler test is to check whether the event uses a derived
04:46RoutedEventArgs type.
04:47Details on some of these events are available in the other videos in
04:51this chapter.
Collapse this transcript
Wiring up event handlers
00:00In this video, I will show you how to subscribe to an event in WinRT.
00:05I'll use this project WiringUpEventHandlers and three pages; Bubbling.xaml,
00:12InCode.xaml and InXaml.xaml.
00:14I'll double-click on this first one.
00:18I have two buttons in this designer.
00:21This first button is a button with no name.
00:24The second button, I've provided a name called Livebutton.
00:28So what I'm going to do is I'm going to this first button and I'm going to add a
00:32click event handler.
00:33Type in the word Click, this is in my XAML.
00:36Visual Studio prompts me and asks me if I want to create a new event handler.
00:40If I want to, I can just press the tab key and have Visual Studio create a
00:44method with a generic name, button_Click_1.
00:49And if I do the do the same thing down here on the second button -- I've already
00:53given this button a name.
00:55It'll still prompt me and ask me if I want to use the existing method that's
00:59already been created or I'd like to create a new event handler.
01:03I will select new event handler and press Tab.
01:05Do you see the difference?
01:08When I had a named button, I got the name at the beginning part of the method name.
01:14Now I'm going to press F7 to show you the code that was written for me.
01:19Here are the two bits of code that were written for me.
01:22These are event handlers.
01:23They return void and they have two arguments.
01:26Microsoft uses a standard pattern for most of their event handlers.
01:29The first object represents the item that's raising the event called the sender
01:34and the second arguments are the extra pieces of data that are passed into my
01:38method that I can use within the body of the method.
01:41In this case, it's RoutedEventArgs and I can come over here and use e. to see
01:46what kind of interesting information is in that parameter.
01:49The most interesting one for me is its original source, which represents the item
01:53that raised the event, if it's a bubbling event.
01:58Let me show you a couple of other ways of adding.
02:01I'll switch back to my XAML.
02:03I'll click on this ellipse to start the process.
02:06There are hundreds of events available in the framework.
02:09I thought I would show you an even for the ellipse shape.
02:12I'll add the double tapped event.
02:14The way I'm going to do that is I am going to use this property window.
02:18I'll open the Property window and pin it with the Auto Hide button and then I'll
02:22click on the lighting bolt, which represents all of the events that are
02:26available for this ellipse.
02:27Here is the DoubleTapped, DragEvents, some FocusEvents, a number of Pointer
02:34and ManipulationEvents.
02:36The one I want to work with today is the DoubleTapped event.
02:39I can't come to this TextBox and type in the name of the event handler.
02:46Now I want to press the Enter key, Visual Studio switches me over to the code
02:52window and shows me my new method.
02:54Here it is DoubleTapped.
02:57Do you see anything different with this method, from the previous method? Yes.
03:03The event args are different.
03:04Here I had a RoutedEventArgs and here I have a DoubleTappedRoutedEventArgs.
03:09That means there should be some extra information inside these arguments.
03:12Let's look, e. I see a method called GetPosition that is different.
03:18I also have something called PointerDeviceType.
03:23So the point is you get different bits of information based on the different
03:26kinds of RoutedEventArgs.
03:27I'll write a little bit of code to change the size of the ellipse.
03:34This code will set the width and height to 10 pixels larger than the current size.
03:39I'll press Ctrl+F5 to run the application and I will double tap with my mouse on
03:48this ellipse and it grows in size.
03:51Let's switch back to Visual Studio.
03:53Now I'm going to look at the code in this file, InCode.xaml.
04:00Here I have a rectangle and I have a button in my UI.
04:06And if I press F7, I'm showing how to wire up an event handler.
04:11The first thing you need to do is register an event handler.
04:15So what you do is you choose the item that's going to raise the event.
04:19In my case, it's an instance of the DemoShape, which is that
04:22Rectangle.DoubleTapped and then I use this special C# syntax, (+=) and then the
04:28name of the method that's going to respond to this and it is down here.
04:33Let me put a breakpoint on that line of code, we'll need it later.
04:37Line 40 shows another way to register an event handler with a more verbose syntax.
04:41I'll start with the Demo.Shape.DoubleTapped +=.
04:45But then, I'm being more verbose here saying, what is the delegate type that I
04:50need to instantiate?
04:51You'll sometimes see code like this.
04:53But nowadays, you're more likely to see this simpler version.
04:56If you're a WPF or Silverlight developer and familiar with the dependency
05:01system, here's another way to setup an event handler.
05:04You call the AddHandler method on the dependency object.
05:08Pass in the event you want to listen to and then the name of the function.
05:13So at this point, I've registered three event handlers.
05:17Now I'm going to debug the application by pressing F5 and show that yes, when I
05:22click on this rectangle, when I double-clicked on the item, it switches me over
05:29to the debugger, once. Press F5, twice.
05:33And press F5, and a third time.
05:37So that proves that that event handler was called three times.
05:40Now normally you wouldn't do this.
05:42But this is for demonstration purposes.
05:43I'm showing how to wire up these handlers.
05:45I'll switch back to Visual Studio and remove that breakpoint by clicking on it
05:50and then we'll see this in action.
05:52Double tap to spin the rectangle.
05:54I'll wire up an event handler live for this button here in the code behind.
05:59Switch back to Visual Studio.
06:01Go back up here to my constructor for this page and then type in the name of my button.
06:12It's called Livebutton.
06:14So we'll type in Livebutton.Click.
06:17Did you see that lighting bolt there?
06:19Let me back up and show that again.
06:22Here is a lighting bolt next to the click.
06:24That signifies that this is an event that's raised from this button class.
06:28So I'll type in Click+=.
06:31And here, Visual Studio's offering to help.
06:34It says that if I press the Tab key, it will write some code for me.
06:37Press Tab to Insert.
06:39And then if I press Tab a second time, it will stub in the event handler. Like so.
06:44Now of course it's my responsibility to write some code in this method.
06:48Finally, I want to look at event bubbling.
06:51This is a grid that contains a number of shapes.
06:55I'll zoom out so we can see all the shapes.
06:59There are three ellipses and three rectangles.
07:02They are inside a grid, here they are down here and they're inside this grid.
07:08So I will write an event handler in the grid that will listen for a double tap
07:12in each of the children.
07:14Tapped, ask for new event handler.
07:18Press F7 to switch over to the code behind.
07:24Now I'm going to get the original source from this DoubleTappedRoutedEventArgs.
07:30So I'm going to check to see if it's a shape that's starting there.
07:33I'm looking for ellipses and rectangles.
07:36I'm not interested in buttons or TextBoxes.
07:39So I'm going to write an if statement that checks that.
07:42First however, I need to get the source.
07:44So I'll say var currentShape = e.OriginalSource as Shape.
07:54So this C# code says, get whatever is stored in this reference.
07:58Try to cast it to a shape and store it in this variable.
08:02Then I'll check for currentShape!=null, for those that are C# developers, this
08:08should be very familiar to you.
08:10Let's say that this was a button that came in the original source.
08:13When I attempted the cast to a Shape, that's not true so this would be would be null.
08:19So I'm checking to make sure it's not equal to null, then I know I got a Shape.
08:23Then here, I'll say curerntShape.
08:24Width, same code I wrote earlier.
08:30But I'm doing this now at the grid level.
08:32So when I run the application, I'll go to bubbling event parent and I
08:38double-tap on the ellipse.
08:39It works, when I double-tap on this rectangle it works.
08:42And when I double-tap on this ellipse, it still works.
08:46So what I want you to takeaway from this movie is that there are two ways to
08:50wire up your event handlers.
08:51You can do it in your XAML file or in your code behind file.
08:55Plus, Visual Studio provides tools to help you write that code.
Collapse this transcript
Exploring keyboard events
00:00In WinRT, there are two types of keyboards that are available for the end user;
00:05Physical keyboards and onscreen keyboards.
00:09You can capture the event from these keyboards in your app.
00:12The easiest way to do this is with the key up and key down events.
00:16These events are raised whenever a UI element has to focus and a key is
00:22pressed on the keyboard.
00:23Within your element tree, only one element can have the input focus at a time.
00:27That means you're getting keyboard input from a single element.
00:30I'll start by showing the key up and key down events in this project KeyEvents,
00:36in this file, GetKeyInfo.
00:39Make this easier to see, Fit all.
00:43There's a TextBox at the top of this page and a TextBox a little bit
00:50further down the page.
00:51This first TextBox has the Key Down Event handler wired.
00:56This same code is going to run wherever you press the key down.
00:59Let's see what that code is.
01:01I'm using the information inside the KeyRoutedEventArgs to retrieve the key
01:07information and show that the key that was pressed.
01:10Something called the scan code for that key and whether this is considered to
01:14be an extended key.
01:18I'll run the application by pressing Ctrl+F5 and then make sure this TextBox has the focus.
01:25I can only get the key events if the TextBox has the focus and then we'll type
01:29the letter A. And here's the information I gathered from that event args.
01:33I'll press the letter A, the scan code is the numeric code that the keyboard
01:37sends and it's not an extended key.
01:40What about if I hold down the Alt key?
01:43That's considered to be the menu key in WinRT.
01:47Here's the Caps Lock, the letter A, the letter B. Here is the one, that's on the
01:54number 1 on the keyboard, here's the number 1 on my keypad.
01:58As far as the TextBox is concerned, it sill outputs the number one but we
02:02know physically which one of theses keys were pressed because of the key pressed value.
02:07I can also do F1 and I'll press the Home key.
02:11This is interesting what it happens in the home key.
02:14I'm in the TextBox.
02:15When I press home, do you see how the cursor moved to the front of the TextBox?
02:19But the key pressed does not update, that's because the TextBox is listening for
02:25the event too and when it receives the event, it didn't let it bubble up any
02:29further and I'm not seeing the keypress value until I press home a second time.
02:35I'll do the same thing with End. I'll press End.
02:38The TextBox gets that and moves the cursor to the end of the TextBox.
02:41Now if I press End again, I'll be notified in my code that someone pressed the End key.
02:46You can find out which key is physically being pressed at any time in your code
02:51by using something called the virtual keys enumeration.
02:54Let me show you how that works.
02:58Here's the code that's relevant.
03:00I'm getting e.Key like I did before and then I'm comparing it with this
03:05enumerated value, VirtualKey.A. Did I get an A key?
03:10Did I get the LeftShift key or the RightShift key, or I can get a virtual
03:14representation of that, just Shift.
03:17In other words, did they really click the left or right, or sometimes you don't
03:21care whether they click the left or the right Shift.
03:23You just want to know if they're holding the Shift key down.
03:26Then I can use .Shift or .Sleep to get the virtual representation of
03:31the physical sleep key.
03:33I can even find out if they have pressed the Window key on the keyboard.
03:37Determining whether the user pressed a key combination like Alt+B is a little more work.
03:42To do that, you use the GetKeyState.
03:46We'll go to this KeyStatePage and press F7 to look at the code.
03:51The way I implemented this is I have a timer on the page, that's firing every 40
03:55milliseconds and then I'm going to pull the current state.
03:59I'm using this GetKeyState.
04:02When you call the GetKeyState method, you pass it the key that you're
04:05interested in, menu.
04:07Remember what I showed you earlier, that's the equivalent to the Alt key.
04:11And here, I'm asking for the GetKeyState of virtual B. So at this moment in time
04:16I'm asking, is the menu key pressed and is the B key pressed?
04:21Also, I'm asking if the cap block key is pressed.
04:25So this is how you can get combinations.
04:26If both of these are true, you know the user is doing an Alt+B. I'll press
04:31Ctrl+F5 to run the application and click over here.
04:34This is running on a timer so all I need to do is hold down the key.
04:38I'll hold down the B key and you can see the different states.
04:41It's in the down position.
04:43Here the Caps Lock is locked.
04:45Now it's not locked. Now it's locked.
04:49Now I'm holding down the Alt key.
04:50Sometimes you'll get double values like down and locked, so you need to check
04:56with a logical word.
04:57They Key Down event is a routed event, that means that I can write a handler on
05:02a parent element to handle the event for the children events.
05:05To do that I'm going to switch back to Visual Studio and open this numeric only page.
05:12This is a common pattern you see where you want to filter out they keystrokes
05:17and only accept a small range of keystrokes.
05:19For my case, only number keys.
05:21The way I'm going to make this is I'm going to go to my grid and I'm going to
05:27setup a KeyDown in the grid.
05:28Now since this is a routed event that will handle keystrokes for any nested
05:33items, Stackpanel, TextBlock, TextBox.
05:37Now only some of these can have the input focus, TextBox, TextBlock and this RichEditBox.
05:43So those are the ones I'm going to be listening for these three text boxes down here.
05:47Then what do I do in my code?
05:49Well, when a key down happens for those three text controls, I check to see
05:54if the key is not equal to virtual number zero and then I build up this
05:58logical statement here.
06:00And then here's the key part.
06:02I'm on the grid and I am listening before the children get an opportunity to see the key event.
06:09I get an opportunity at the grid level.
06:11So what I do is I use a property on the KeyRoutedEventArgs called Handled.
06:18I signal that I've handled the key event and then that prevents the event from
06:23getting to the individual text controls.
06:26So they never receive the event that key was pressed, so they don't show the
06:31key in their text editor.
06:33Let's see what this looks like.
06:35I'll type 123, that's in the TextBox.
06:40I'll go to the RichEditBox and type in 123, but now, when I try to type the
06:44letter A, that doesn't show up.
06:47I don't know if you can hear the keyboard on my microphone but I'm tapping the A
06:51key and nothing is happening.
06:52Same here and same with this TextBox, but the number values are accepted.
06:58There are also some control specific events like the TextChange events, which are
07:04helpful in some scenarios.
07:06Check the MSD in documentation to find out more.
07:09The framework also has an assortment of touch-related events which are covered
07:13elsewhere in this course.
Collapse this transcript
8. Exploring the Touch System
Understanding the touch system
00:00Touch interfaces are showing up on every imaginable device;
00:04Smartphones are one of the obvious examples as are the rising flood of
00:08new tablet computers.
00:09I've seen 82-inch wall mounted monitors as well as 32-inch models for the
00:14desktop that are touch-enabled.
00:15Microsoft has even started to sell touch-based mice.
00:19The Windows 8 API represents a new way of thinking about touch and mouse input.
00:23A Windows Store App can work with a huge list of touch devices.
00:28The app supports single and multiple touch inputs.
00:31As far XAML, any type that derives from UIElement is touch-ready.
00:36There is a strong likelihood that your favorite control exposes a number of touch events.
00:41Even better some controls take the existing touch system and build a rich
00:45interactive UI on top of it.
00:47For example, child elements in a Scrollviewer are automatically scalable with
00:52the pinch and zoom gesture without the developer having to write any code.
00:57Touch events can be separated into three categories;
01:00Pointers are the lowest level events, Gestures, which are higher level events
01:05for standard motions.
01:07Gestures leverage the raw touch pointers, and then finally Manipulations.
01:12Manipulations are the highest level events able to handle complex gestures like
01:16pinch, zoom and rotate.
01:19Pointers are an abstraction around the mouse,touch and pen inputs.
01:24The Pointer API exposes information about input devices available on the
01:27computer and which input devices currently interacting with your app.
01:32You won't find a mouse down event in WinRT, instead you'll use the
01:36pointer pressed event.
01:37That way you write one event handler and it works whether the input is a mouse
01:42or a pen or a touch screen.
01:45Pointers are a great new concept but the Windows team didn't stop there.
01:49Imagine you are an app developer and want to write some pointer level code to
01:52enable sliding an item vertically on the screen.
01:56The code will be lengthy with lots of edge case scenarios.
01:59From Microsoft's perspective, there is another problem.
02:02If 200 app developers each create their own sliding library, the user experience
02:06on Windows 8 will be inconsistent.
02:07So Microsoft created a gesture API that centers around the gesture recognizer.
02:14The recognizer tracks the latest user interactions with your app.
02:18When it detects a known pattern, it raises a gesture event.
02:22For the app developer, this is an outstanding feature.
02:25Write a gesture event handler and let the API take care of the rest.
02:30Here are a few of the gestures available.
02:32The Tap gesture is simple.
02:34The recognizer detects a momentary touch and release;
02:37use this to execute an action like switch to another screen or initiate a file save.
02:42The Press and Hold gesture behaves just like it sounds.
02:46The recognizer detects a touch without an immediate release.
02:49Use Press and Hold to show additional information, open a Help tool tip or an options list.
02:55The Double Tap is another way to initiate an action.
02:58I find that the tap event is usually sufficient for most situations.
03:03The gesture recognizer watches for multiple touch input.
03:07When it detects multiple touch points, it analyzes the motion between
03:10the various points.
03:12When the motion matches the correct pattern, it raises a manipulation event.
03:17There are several manipulations available.
03:19The Slide and Swipe manipulation are raised when the recognizer detects movement
03:24of multiple fingers.
03:25The Turn or Rotate manipulation is when the touch points spin in a clockwise or
03:31counterclockwise direction.
03:33The Pinch and Stretch manipulations occur when the touch points move closer or further apart.
03:39It's important to note that the recognizer can detect all of these manipulations
03:43occurring at the same time.
03:45So if the user spins and pinches at the same time, you get both actions in
03:50your event handlers.
03:52The rest of this chapter shows how to use pointers, gestures and manipulations.
Collapse this transcript
Using pointer events
00:00The pointer class is an abstraction around the mouse, touch, and pen input.
00:05In this video I'll talk about a few of the pointer events and properties and
00:08show how to track elements on the device screen.
00:11If you look inside this file, PointerApiInformation, inside this UsingPointers
00:17project, it has some information about some of the important classes that are
00:20part of the pointer API.
00:22For instance, there's a PointerDevice class which gives you information about
00:26the details about that hardware device.
00:28For example, the maximum number of contacts that that device supports.
00:33The Pointer class is that abstraction I mentioned earlier.
00:35Here you can get a pointer ID value.
00:38So if you have two fingers touching the screen you would have two
00:41pointer identifiers.
00:43The PointerPoint class represents the position and also has a property
00:48called IsInContact, which tells you whether or not the pointer is currently
00:52touching the digitizer.
00:53And here we have the PointerPointProperties class which gives you extra
00:57information about the pointer.
00:59For instance, it has something called TouchConfidence, which returns false if
01:02the touch seems accidental.
01:04It has IsPrimary, which tells you whether or not it's the first contact on the screen.
01:09So again, if you have two fingers and you touch one and then a second, the
01:13primary contact will be the first finger that touches the screen.
01:16How do we use this? Let me show you.
01:18I'll go over to the SimpleDrag.xaml file.
01:23It's a page with one Ellipse on the page.
01:26Now I'm going to wire up some event handlers for the Pointer events, and then
01:32I'll show you the code.
01:33I'll go down to the Ellipse itself.
01:36First I'll start with this PointerPressed event.
01:39Now I already have the code written so I'll just select from this list.
01:42I'll choose this PointerPressed and then handler.
01:45Next I'll wire up the PointerMoved.
01:50For the third event handler I'll choose the PointerReleased.
01:54And then I have two more than I want to wire up;
01:57one is called the PointerCanceled, I'm going to wire this up to the same Event
02:03Handler I just used, PointerReleased.
02:05And finally, I'm going to work with the PointerCaptureLost, and I'll also choose
02:11this PointerReleased.
02:12So what do we have here?
02:14The PointerPressed fires when a pointer is touched to the device.
02:18The PointerMoved fires as it's moved across the screen.
02:22The PointerReleased event fires when the user lifts their fingers from the
02:26screen or takes their finger off the mouse button.
02:29And finally, the last two, PointerCanceled and CaptureLost, these happen in rare
02:34instances, but you still need to write some code to handle them.
02:37Let's look at the actual code. I'll press F7.
02:40Here's the first bit of code, when the user touches the screen or uses their
02:44mouse button, we're going to capture the pointer.
02:46Now it's important to capture the pointer, because with the digitizers,
02:50especially with touch digitizers, the user can move their finger too fast for
02:54the digitizer to keep up.
02:55So what this says is I have this pointer and I'm the only code that can hold
03:00on to that pointer.
03:01No other item can grab a hold of it while the user is moving their finger.
03:05When the pointer is released, down here, I'll release the PointerCapture.
03:11And then when the is pointer moved, what I'll do is write some code here that
03:14checks to make sure there's only one pointer.
03:17Here I'm saying PointerCatures.Count ==1, and if there's only one pointer.
03:22I'll use this bit of code to retrieve the current point on the screen, and then
03:27I'll move the Ellipse to that new position.
03:30Let's see what this looks like.
03:32Press Ctrl+F5, I'll hold the mouse button down and drag, and it moves the
03:38Ellipse to a new position on the screen.
03:40Let me click on the Show Pointers button and show you the next example before
03:45I show you the code.
03:46I'll move my mouse to the canvas and hold down the mouse button.
03:52What you see is a new text block being added to the canvas based on the pointer.
03:57So it's telling me that this is a mouse pointer and the number represents the
04:01number of touch points.
04:02Since I have a mouse I can only have one touch point.
04:05Later when we move to the tablet we'll see multiple touch points.
04:08And again, as you see as I move the mouse around, this text block floats around
04:13to the new position.
04:14Let's see what this code looks like.
04:16I'll press Alt+F4 to close the app, switch back to Visual Studio.
04:23The code for this example is in ShowPointers, in the C# file.
04:30It's similar to the other code.
04:32It has a PointerPressed event and has a PointerMoved event and has a
04:37PointerReleased event.
04:38And I'm sending up the Event Handlers in the XAML file.
04:43In the canvas I'm setting up these event handlers.
04:46Switch back to the code.
04:49And then I have one other method down here called SetPositionForTextBlock, which
04:52just moves the IM around on the screen.
04:55Let's take a look at the top bit of code.
04:57Here I've got a dictionary called infoTextBlocks that stores a unique
05:02identifier, the uint for each of the items, each of the touch pointers.
05:06It stores a uint one for each pointer and then it also stores a TextBlock.
05:11So when the user presses on the screen with one of the pointers, I get the
05:16current point, I create a new TextBlock, and set the Text of the TextBlock to
05:21the PointerId, and the PointerDeviceType;
05:24that was when I said mouse in my case.
05:29Then I set the position on the screen and then I add the TextBlock to the
05:33dictionary and I add the TextBlock to the canvas.
05:37When the pointer moves I look in the dictionary to see if the item is in there,
05:41and if it is, then I move it to a new position.
05:46And when the pointer is released, I check inside the dictionary, and if it's in
05:51there I remove it, and I'll also remove it from the canvas.
05:56Now let me switch to the tablet and show the multi-finger input.
06:00I've deployed the UsingPointers App to the surface tablet.
06:04I'll touch the Show Pointers button.
06:07I'll touch the screen with a single finger.
06:10I create a new TextBlock.
06:12As I touch two fingers on the screen, two text blocks appear.
06:16Note the ID numbers.
06:18When I release and retouch the screen, each touch point appears with new ID numbers.
06:24This tablet supports up to five points.
06:27Pointers give you the power to work with the low level API, but for everyday
06:31interaction the gestures provide a simpler way to do common tasks.
06:35See more gesture information in the next video.
Collapse this transcript
Gestures
00:00Writing a gesture event handler is easy.
00:03Any element that derives from an UI element exposes the Tapped, Double Tapped,
00:07and Right Tapped events.
00:10Inside this UsingGestures project I'm going to open the TappedGesture.xaml file.
00:15Currently this page has a single image and I've already written an event
00:19handler for Tapped.
00:20Let me add a Rectangle.
00:23This rectangle also supports a tapped event;
00:25you can see events by clicking on this Event lighting bolt and then scrolling up
00:30and down in this list, here is the Tapped event, here is the RightTapped event;
00:35up here is the DoubleTapped event.
00:37Now let me delete this rectangle, press the Delete key and let's look at the
00:43code for the Tapped event handler for this image. Here it is.
00:49I'm given an event argument called TappedRoutedEventArgs, which I'm going to use
00:54on line 36 to get the position of the current tap on the screen.
00:59Then I'm going to create a brand new ellipse, set a Width and Height of 50, fill
01:04it with a semitransparent color, then I position the ellipse using
01:08Canvas.SetLeft and Canvas.SetTop and then finally I add the ellipse to the
01:13canvas Children collection.
01:15Let's see what this application looks like when you run, Ctrl+F5.
01:21Now I just tap anywhere on the screen and it adds multiple ellipses.
01:25Remember these gestures also work with the touch digitizer on a Tablet, I'm not
01:30going to demonstrate this code running on a Tablet but trust me it works.
01:34There might be circumstances where you need to disable certain gestures on an element.
01:39Switch back to Visual Studio and then open this TappedGestures.xaml file, find
01:47the image, here it is.
01:50So I can say, IsDoubleTapEnabled='False' and another one I can set is
01:55IsHoldingEnabled, I am going to set that one to False.
01:58I didn't mention the HoldingGesture but you can see it over here in the Event list.
02:06Next I want to open this RightTappedGesture.xaml file.
02:10Again, I have an image and I'm going to right tap on this.
02:16In this image I'm setting up a RightTapped event handler and then in the code,
02:22press F7 to show the code, what I'm doing is turning on the visibility of an
02:27existing text block on the screen.
02:30Right tap and hold are sometimes used to show context information on the screen.
02:34Press Ctrl+F5 to run the application, move to this demo and then I'll right tap
02:40to see the extra information on the bottom of the screen.
02:44For even more sophisticated gestures, look into the manipulation API, which is
02:49the topic of the next movie.
Collapse this transcript
Manipulations
00:00The digitizer converts the activity of one or more fingers on the touch screen
00:05into the manipulation gestures.
00:06There is a rotation gesture, a translation gesture and a scale gesture.
00:12Microsoft uses the term gesture and manipulation interchangeably in some parts of the API.
00:16There is also a series of events that are raised when a manipulation starts or ends.
00:21Let me show you how to set up a manipulation.
00:24I'll open this ImageManipulation.xaml file.
00:28It's a simple page with one image.
00:31In the image I have several event handlers set up.
00:34One for the Started event and one for the Completed event, one that gives me
00:39the Delta information about the changes in the manipulation, and one that works with Inertia.
00:45Inside the image element I have a RenderTransform and inside the RenderTransform
00:51I have composite transform named transform1.
00:55This allows me to set the rotation, the size and the location of the image on the screen.
01:01Now before I show you the code, let's come back up and talk about
01:05this manipulation mode.
01:08This tells the digitizer what manipulations I'm interested in.
01:12Currently I'm interested in all the manipulations, but I can limit that, let's
01:16say I choose Rotate, now the digitizer is only going to tell me when the user is
01:22doing a rotate on the screen.
01:23Let's see what else is in there. I have a Scale;
01:28I have several translate items, TranslateX and TranslateY.
01:32There are also some interesting inertia items, RotateInertia and ScaleInertia
01:36and TranslateInertia.
01:39The inertia items allow my code to get notifications after the manipulation is complete.
01:44These are typically used to animate the item on the screen after the user
01:47releases their fingers from the Tablet.
01:49I'll show you an example later.
01:51The rails option, TranslateRailsX and TranslateRailsY, means that the movement is
01:57constrained to the X or Y axes only.
01:59Here is an example, let's say you're dragging an item to the left on the screen,
02:05if you have TranslateRailsX turned on, as soon as you start moving to the left
02:09you're no longer allowed to drag it down the Y-axis up or down the page.
02:14I'll set this back to All and then show you the code.
02:17I'll press F7 to switch to the Code View.
02:22In the ManipulationStarted event I'm grabbing some information about the initial
02:27Scale, the initial Rotation values and the initial Point of the object.
02:33Here in the ManipulationComplete event I'm setting a MessageTextBox.Text to a
02:38string that has the time stamp of when I finish the manipulation.
02:42The ManipulationComplete fires when all of the fingers have left the screen.
02:47In the Delta I'm given this ManipulationDeltaRoutedEventArgs and I'm using that
02:53to get the Cumulative Rotation and Translation and scale and so on.
02:58This is how I move it on the screen.
03:00And notice what I'm doing with those, I'm taking that transform that I have as
03:04part of the image and I'm applying ScaleX, TranslateX, TranslateY and Rotation.
03:08So really I'm just grabbing the information very simply and applying it to the transform.
03:14It's time to switch to my Tablet and see these manipulations in action.
03:18In the app I can drag the image with one finger and the translation transform is
03:23applied to the image.
03:24I can also drag the image with two fingers in contact with the image.
03:28Note that when I lift all of my fingers from the screen, the completion event
03:32fires and updates the time stamp.
03:34I can also use two fingers to scale and rotate the image.
03:38One key factor that separates manipulations from standard gestures is that I can
03:43still drag the image while touching the screen elsewhere.
03:45I'll touch the screen in the blue area with one finger;
03:48then manipulate the image with the other hand.
03:51Finally here is an example of Inertia.
03:54I fling the image to the left and it animates off the screen after I release my
03:58finger. It's time to return to Visual Studio.
04:02I'm going to open this ManipulateMany.xaml file.
04:07This page has two images and two rectangles;
04:11all four of these items are contained inside a canvas.
04:14And what I want to do is manipulate the two images but I don't want the user to
04:20be able to interact with the two rectangles.
04:22The key for making this work is the Manipulationmode property.
04:26By default all elements have the property set to none.
04:29So this rectangle is not participating in manipulation and neither is this rectangle.
04:34But the two images have Manipulationmode is set to All.
04:39Now I could write an event procedure, but I'm going to do it a
04:43slightly different way.
04:44It's common in the framework to have two ways of handling events.
04:47I can write an event procedure or overwrite a class method.
04:51I'm going to press F7 to go look at the code.
04:54On line 28 I'm overwriting a method on the page class, OnManipulationDelta.
05:01This will fire for any manipulation on any element on the page.
05:05But remember, I only have two that are opting in for the manipulations, my two images.
05:11So what am I doing in here?
05:13I'm grabbing the source here in the args.
05:16This is coming from the ManipulationDeltaRoutedEventArgs.
05:20I'm getting the original source and casting it to an image.
05:24Then I'm getting the currentImage as RenderTransform and storing it in this variable.
05:29And then I'm setting the X and Y Translate values, the ScaleX and the ScaleY and
05:36the Rotation values.
05:37This time I'm using the Delta.Scale and Delta.Rotation and Delta.TranslationX
05:40to get these values.
05:43Let's see what this looks like on the Tablet.
05:46In the Apple touch the Manipulation Many button to get to the correct screen.
05:51I can drag each image with a single finger, but I cannot drag either rectangle.
05:59I can drag both images at the same time.
06:02I can even rotate both images simultaneously if I use four fingers.
06:08I'm impressed with the touch API WinRT, it really is top notch.
06:12Using this system makes it easy to add compelling touch features to your app.
Collapse this transcript
9. Input and Controls
The application bar
00:00The app bar is a unique feature of WinRT apps.
00:04Many built-in apps like this Store App use them.
00:07The app is not visible by default;
00:10it appears when a user swipes a finger from the top or bottom edge of the screen.
00:14Mouse users can right-click to see the app bar.
00:18Keyboard users can use Windows key Z to show the app bar.
00:21Here in the Store, they are showing a top app bar with two choices, the Home
00:26button and the my app button.
00:29I'll switch to the SkyDrive app and bring up the app bar.
00:32Here they are using a bottom app bar with five buttons.
00:36You can press the upload button to add a file to your SkyDrive folder.
00:40Note the standard look for these buttons, they all have the Win app round look
00:45with the white icon.
00:46Keep that in mind, I'll show you how to style your button the same way.
00:50Here in the WEATHER app, they have a top app bar and a bottom app bar.
00:55Let's see how to make this in your app.
00:58I'm using a project showing app bars for the example;
01:01let's start by looking at the main page.
01:03We've seen this UI in several other videos.
01:06I have buttons down on the left side of the screen that load children pages and
01:10they load the children pages into this frame.
01:13Now app bars go on pages, so it's possible that I could have one app bar on the
01:17parent and other app bar on the child that's loaded in this frame.
01:22The first demo is going to show a page that has no app bars;
01:25second demo we'll look at will be this empty app bar.
01:28Let's run the application and see the first demo.
01:31I'll click on this button, Child with not AppBars and then I'll right-click on the page.
01:35Nothing happens because there are no AppBars yet.
01:39Next I'll go look at the XAML and EmptyAppBar.
01:42It's trivial to add an AppBar, here is all the XAML you need to do.
01:46Inside your page element you'll use a Page.TopAppBar and then specify an AppBar
01:53inside the TopAppBar.
01:55And then inside this area you put any valid XAML that you want.
01:59Right now as you can see I have no XAML in there.
02:01What does this look like when I run the application?
02:06Here is the page and I'll right-click. At first glance this example is not very impressive.
02:12All I see is an empty black bar appear across the top of the page.
02:16But this is remarkable.
02:17With a trivial amount of XAML I added the AppBar and it just works with the
02:21mouse, the keyboard and swipe gestures.
02:24Plus it appears with the standard Win 8 app bar animation.
02:28Now let's see how to customize the AppBar content.
02:31Return back to Visual Studio and I'll modify this AppBar element by saying
02:37Background='Orange', rerun the application, open up the empty top AppBar and
02:46right-click, that's very bright.
02:48What you're seeing here is I can customize the background color and the size of
02:53the AppBar and of course I can put controls on the AppBar.
02:57Back to Visual Studio and I'll look at these controls in the bottom AppBar.
03:03In my XAML I've created a section Page.BottomAppBar and then within the AppBar
03:09it contains a Stackpanel.
03:11And within a Stackpanel I have a button, a TextBlock, a slider and a ComboBox.
03:17You can see them in the Visual Studio designer without having to run the
03:20application by clicking on the AppBar.
03:23So now when I'm clicking at the grid I'm looking at the grid in the designer,
03:27but when I move up and click on the AppBar section, visual studio shows me the
03:31AppBar across the bottom.
03:32That way I don't have to keep running the app just to see what my UI looks like.
03:37It is fully functional.
03:39When I run the app and move to bottom AppBar and right-click to bring up the
03:43bottom AppBar, I can interact with the button, drag the slider and choose an
03:47item from the combo box.
03:48That's how you get the information into your application.
03:52Next I'm going to open this file MonitorAppBarEvents.
03:55Again I'll scroll down to the bottom of the screen here and then I'll click
04:02on my bottom AppBar.
04:04See I have two AppBars in this one;
04:06I have a TopAppBar and a BottomAppBar.
04:09And inside the Stackpanel, I have three buttons.
04:11Here is a button with no style applied and two buttons that have a style.
04:15Now what am I talking about styles?
04:18Microsoft includes some interesting button templates for the AppBars, by
04:21applying this template you get a nice formatted look and custom icon
04:25displayed on the buttons.
04:27These button styles are included in the StandardStyles.xaml file.
04:31Let me open up this file and I'll scroll down to this section.
04:36By default all of the styles are commented out inside this document.
04:40You can see here is one called SkipBackAppBarbutton and
04:43SkipAheadAppBarbuttonStyle, I've uncommented a few of these. Here is the one I'm
04:48going use, SaveAppBarbuttonStyle.
04:51So by using this key on my button, I automatically get this nice round circle
04:56and the save icon, and when I use the YesAppBarbuttonStyle, I get the round
05:00circle and the check mark, very nice.
05:03I also want to show you in this file, this MonitorAppBarEvents that we can list
05:07in for events when the AppBar opens and the AppBar closes.
05:11And I'm doing that up here on the AppBar itself, the Opened event and the Closed event.
05:16Of course you can put any code that you want;
05:19I put some very simplistic code in there.
05:21All I'm doing is changing the main background color of the grid and showing some
05:25text that shows when I open and close the AppBar;
05:28let's see what this looks like in motion.
05:30I'll click here, I'll right-click to bring up the AppBar;
05:35there is my fancy buttons.
05:36The background color changes and it shows I opened at 5:03. I'll click in the
05:42document to close the AppBar and it goes back to the magenta color and shows
05:46that it was closed at 5:03.
05:48I'll do that one more time, it's so fun. Open, close.
05:53So far all the AppBars you've seen in this demo are located in the child page.
05:57I'll add a few AppBars to the parent page and see what happens, switch back to Visual Studio.
06:01I have defined some extra AppBars, some fancy looking AppBars, in this folder.
06:07For simplicity sake, I made these custom controls.
06:10I have a XAML file that starts with AppBar rather than a user control or page
06:15and I have defined my own UI in here.
06:18That's called my AppBar buttons and I also have another one called AppBarNav,
06:22which again has this AppBar root element.
06:25That means that I can just add this with a single line inside my main page and
06:29not have 40 lines of XAML at the very top of my page from each of the AppBars.
06:34I'm going to show you what I mean.
06:36I'll go to MainPage and I'll scroll up to this section.
06:38Let me move this up a little higher so it's easier to see.
06:41I'll uncomment this, Ctrl+K, Ctrl+U and now you see I'm just using my AppBarNav
06:48and my AppBarbuttons.
06:49Now you saw it briefly popped up on the top of the screen, that's my fancy UI.
06:54I've got my own custom buttons with a blue border and some icons for the bottom AppBar;
07:00I have a number of mail buttons and video buttons and things like that.
07:03You want to see this in action? Of course you do.
07:10Now when I right-click I'm getting the top and bottom bars.
07:14Now if I move to an item that has a bottom bar and I open it, you'll see that I
07:19get the bars for this one, this one had a top and bottom bar.
07:22Here in this example when I went to bottom AppBar, I get the TopAppBar from the
07:27main page and the BottomAppBar from the child page.
07:30This means you can mix and match for whatever the context situation is in your application.
07:35I'll summarize what we've seen.
07:36AppBar is one of the unique features of Window 8 Apps.
07:39You should strongly consider adding them to your app.
07:42Put controls on the AppBars that are needed by the user but don't need to be
07:46shown on the current screen.
07:48By tucking in the controls into the AppBar you can offer a full screen view
07:51of your page for the users, but the controls are accessible with a flick of the finger.
Collapse this transcript
Working with text
00:00There are two families of text controls; block controls, which are read only,
00:05examples would be text block and rich text block; and box controls which are
00:10intended for text input, examples would be text box and rich edit box.
00:15I'm in this project working with text.
00:17Let's start by looking at the code in TextInput.xaml.
00:21This first control on the page is a standard TextBox.
00:24It's used for gathering data entry from the user.
00:28The next control is a PasswordBox.
00:31It's used to gather information from the user, but we don't show the text as they type.
00:35By default it uses a character to mask out the text.
00:39You can customize that character by using this property PasswordChar and
00:44supplying your own character.
00:45Let's see what these two look like.
00:47Ctrl+F5 to run the application, your standard Textbox and then type the word
00:52hello here and hello here.
00:54You can not see what you're typing, but here I have customized the character
00:58that's masking the text.
00:59Now let's look at this TextBox.
01:01I've written some code that will select items or read the selection.
01:06Let me select a few words and then I'll click on the Get Selected.
01:10As you can see I'm pulling that text out and showing it over here in the screen.
01:14I can also set the selected text to any value or I can programmatically select
01:19all, let's see how you could do that.
01:21Return back to Visual Studio and we'll look at the code behind for this button.
01:26Press F7 to look at the code behind, very straight forward code.
01:30I have a TextBox, it has a SelectedText property.
01:33I can retrieve that and store in the string and then output that to another
01:37control on the page.
01:38Here I want to select a portion of the text in the TextBox.
01:43I'm using a little bit of code here on line 36 to programmatically set the
01:47focus on the control.
01:48If the focus is not in the control, you can't read the values.
01:52So I programmatically moved the focus there and then I select these characters.
01:56In this example, I set the focus there with the same code and then I just
02:00call SelectAll to read all the characters, like I said it's very
02:03straightforward code.
02:04There are a couple of interesting properties on the TextBox.
02:07For one there is a property called IsSpellCheckEnabled='True'.
02:11It places a red squiggle on your misspelled words and provides a context menu
02:15with suggestions and other options.
02:17It also auto corrects common mistakes.
02:20Let's see that in action.
02:21Here is my SpellCheck TextBox, and you can see I'm getting red squiggle, I can
02:30right-click and choose have and it fixes my spelling.
02:34Now this one it might have some trouble with, it can't find the word trouble,
02:37which is what I've been trying to type, but it did find tarp.
02:40I have a tarp, sounds good to me.
02:42There is also a projection feature, I have a screen shot of that;
02:46here is what it looks like.
02:48Predictions suggest a word while you type, this is true by default.
02:52But it's activated only when input is coming from the software keyboard.
02:56In this example I was using the software keyword to type the word bargain.
03:00This suggestion pops up;
03:01then I can touch the black rectangle to enter the word automatically.
03:05Next, let's look at display only text or read only text.
03:10You've probably seen a million text blocks by now if you've been doing
03:14anything with WinRT.
03:15This is a basic text block.
03:17You specified a text and perhaps a foreground color and font size and things like that.
03:23You have one color and one font.
03:26It's not very well known, but the TextBlock also supports mixed content.
03:30Here is an example in the UI and this is what it looks like in your XAML.
03:36I have not specified any colors or typefaces in the TextBlock itself.
03:41But within the TextBlock I created something called a Run.
03:45This first Run is in blue, so I specify the FontStyle, the FontSize for the run,
03:51the FontFamily for the run and the Foreground, and then the text that I want to
03:56show up in that run.
03:57Then I build another run which is also added to the text block and this one is
04:01dark orange and 60 font.
04:04And by using this CharacterSpacing I'm able to spread out the distances
04:08between the letters.
04:09Finally, I have another one here with yellow and these three letters.
04:13There is also another TextBlock control that lets you mix fonts, it's called the RichTextBlock.
04:19It's very similar to the regular TextBlock with one enhancement.
04:22It can overflow text into separate elements, which enables multicolumn text or
04:27other custom layouts.
04:29Here is my example, here is my Rich Text Block and then in these other two
04:33columns in the grid I have these controls called the RichTextBlockOverflow and
04:38there is another one over here.
04:40So what I do in the first RichTextBlock is I specify all of the text.
04:45Here instead of using a Run, I'm using a Paragraph.
04:48So I created a paragraph and set its FontSize and its color and then another
04:51paragraph, a different FontSize and FontsFamily, and different color, and then I
04:56put some text in there; many, many paragraphs in this text block, it won't all
05:01fit in the first column.
05:02So then what I do as I specify, here, this overflow content target and I tell it
05:08to use another element on the page called over1.
05:10So the text that doesn't fit in the first TextBlock moves into this
05:14RichTextBlockOverflow.
05:16And it also has an OverflowContentTarget, which moves any text that doesn't fit
05:20in the second column into this third column.
05:23Now since this doesn't have any further overflow, any further text that's inside
05:27this TextBox will not be shown on the screen if it doesn't fit.
05:31Here is what that looks like, it flows down the page, gets to the bottom of the
05:35page and then flows into the top of this overflow.
05:38In summary, there are a few controls for showing read only text.
05:41Pick the text block for simple or mixed format text;
05:45choose the RichTextBlock for rich text that overflows into other regions of the page.
05:49For editable input use the TextBox or RichEditBox.
Collapse this transcript
Toggling states with checkbox and toggle controls
00:00Toggle controls are elements which can be in one of two states.
00:04When you need to offer an on/off choice in your app, these are the controls to choose.
00:09I'll start by looking at the code in this TogglebuttonPage.
00:13A toggle button is an on/off switch that looks like a traditional button.
00:17You can see it's called Togglebutton and what I've set in here is one called
00:21Content Bold and one has Content Italics
00:24I wired up two event handlers, when you check or press the toggle button
00:30this code will fire and when you uncheck or press the button the second
00:33time, then this code will run.
00:35I'm setting the content to Bold and Italics, if you want fancier UI you can
00:39create a custom button template.
00:42Let's look at the code behind, press F7, straightforward code and
00:47the BoldToggle_Checked;
00:48I'm setting the FontWeight of a textblock to Bold and then the Unchecked event
00:53I'm setting it back to Normal.
00:55Same with the Italics, set it to Italics and set it back to Normal.
00:59Ctrl+F5 to run the application and I clicked once to toggle on the button;
01:03it's a visual cue to the user that it's toggled on because it has a white background.
01:08When I click on it a second time, it toggles it off.
01:11Note the code is also running as intended.
01:13The toggle buttons has been around for a long time, there is a new element
01:17called toggle switch which is updated to look like a slider that user can slide
01:21back and forth like this.
01:23This works really well with your finger or with the mouse or a stylus.
01:27Here I'm switching the Text Size to large and small.
01:30Here I'm setting the Italics to on and off.
01:32And these next two examples I'm customizing the text.
01:35By default the ToggleSwitch uses the word On and Off when you switch states, but
01:40here I've customized it and put my own header and my own text, so it's
01:45Unavailable and Available, or the text that I chose.
01:48You can even get fancier and choose your own UI.
01:51Here I've placed a picture next to this control in my own custom header up here.
01:56And when I toggle it on, I'm switching out the UI to a different picture, Off, On.
02:01Let's look at the XAML for this, that's in this page, ToggleSwitchPage, let's
02:06look at the first one.
02:07To set the text that shows over the top of the switch, you use the Header.
02:12Unlike the Toggle button there is only a single event here called Toggled.
02:16So my code is going to have to be slightly different because I don't two events.
02:20The key to making this work is that the ToggleSwitch has an IsOn property.
02:24So what code is doing here is when this code fires, I check to see if the switch
02:29is on, if it is I set it to one font size and if it's off, I set it to another
02:33font size, same thing down here.
02:35Back to the XAML, here is how you change the text, you just change the OnContent
02:41or OffContent and since content can be rich, it doesn't have to be just a string
02:46in XAML on this ToggleSwitch.
02:49I'm specifying the Header and then a Stackpanel with some TextBlocks in there.
02:53And for the OnContent I'm specifying an image and for the OffContent I'm
02:58specifying an image with a different file referenced.
03:02Finally we have the CheckBox control, that's over here in this
03:05demo, CheckBoxPage.
03:06CheckBox has been around forever in Windows and traditionally Microsoft has
03:12supported what's called a three-state CheckBox.
03:15In other words it can be in Checked, it can be Unchecked, and it can be Indeterminate.
03:21Use Indeterminate when you don't know whether to show checked or not checked.
03:25The example here would be I have three files selected in File Explorer and I
03:31right-click on them and say I want to see the read-only properties and two
03:35of them are read-only and one of them is not, how do I represent that in a CheckBox?
03:39In that case I'd use Indeterminate.
03:41I've included Microsoft's suggestions for when to use a CheckBox and when not to use a CheckBox.
03:46I'll discuss that one after I show this demo.
03:49Here is how you set a regular CheckBox inside the element and I'll say IsChecked ='True'.
03:55Here is how you do a content and you want to set the value to Indeterminate.
03:58Now there is a bug in the current version of WinRT where this does not work in
04:03XAML, you have to set it programatically in your code behind and that's what I'm
04:07doing here, so I'm saying btn5 and btn6, I'm saying there IsChecked to null.
04:12You're supposed to be able to do it in the XAML, but it doesn't work.
04:15That's going to make it look indeterminate and then this third CheckBox starts
04:19out with a null value and it also allows the user to Toggle between the
04:23three-state, in other words they can Toggle by click on it from Unchecked to
04:27Checked, to Indeterminate.
04:28I'll run the example, go over here to the CheckBox.
04:31This one, I can click once or twice to uncheck it.
04:34The second CheckBox is currently Indeterminate.
04:37I can click on it to make it unchecked, click on it again to make it checked, but
04:41now when I click on it the third time, it goes back to the Unchecked, whereas
04:45this one I can toggle between the three states.
04:49To review what we've seen, there are several toggle controls available.
04:53In most situations if you allow the user to change the state use the ToggleSwitch.
04:59If you are showing an informational state, then try the CheckBox.
Collapse this transcript
Gathering numerical data with a slider
00:00The slider control is a great way to get a continuos range of numeric
00:04values form the user.
00:05They drag their finger across the slider and it provides a number within the
00:09specified min and max range to your code.
00:12The first example is in this page, Basicsliders.xaml.
00:18My UI consist of three sliders and a rectangle.
00:21I'm going to use the three slider values to change the overall color of the rectangle.
00:27Make this a little bit smaller, so it's easier to see and then I'll click on the slider.
00:31I've given each one of these sliders a name and I'll slide this up to make more room.
00:37This is the Redslider, this is the Greenslider and this is the Blueslider.
00:41I need these names so I can access them in my code.
00:43I've also set up a ValueChanged event handler.
00:47Notice how I'm sending all three sliders to the same function.
00:51I know the information I need in my code is a byte value, so I'm also specifying
00:56the minimum to zero and the maximum to 255, F7 to switch the code behind.
01:03Remember that all three of those sliders call this method.
01:06So what am I doing in here?
01:07I'm grabbing the Red, Green, and Blue values, casting them to a byte because
01:13they come out as a double, they come from a double and I'm casting them to a
01:17byte and then I'm passing them to this method FromArgb.
01:20This makes a custom color from the Alpha Channel which I've set to 255 or fully
01:25opaque and then Red, Green, and Blue channels are coming from the sliders.
01:28And then build a solid color brush and assign that to the Fill property of the Rectangle.
01:37And there is my custom color editor.
01:40You know it's like a tool tip automatically, there is my 0 value; there is my 255.
01:46These sliders also work with touch and pen input.
01:51The sliders have some properties.
01:53For one I can change the orientation to make it a vertical slider instead of
01:57a horizontal slider.
01:59I can also do what's called a Snaps To where I can make it when the slider moves.
02:04You see how the tool tip says I'm going from 110 to 120 and then 130, so I can
02:10only get numbers that are within the snap values.
02:13I can't get numbers between 110 and 120, no matter how hard I try.
02:19I'll show you the XAML for that in just a second.
02:22Here, I'm also adding some tick marks, some black marks on the line to
02:25indicate some intervals.
02:27You see how the tick marks are matching the Snap To values.
02:30Let me show you this in the XAML.
02:32This code is over here in sliderProperties.xaml.
02:36This first slider, I just have to change the Orientation equal to Vertical that was simple.
02:41For the Snaps To, I just have to snap to what -- I tell it what I'm going to
02:46snap to, StepValues and then I specify the StepFrequency here.
02:50In this slider I say it SnapsTo the Tick marks and then I specify the TickFrequency.
02:55It ends up working the same as the previous one when I did the SnapsTo, but the
03:00difference is of course I get these black bars on my slider.
03:03For this last demo I want to show you how I can convert data.
03:07Sometimes the tool tip doesn't show the data exactly the way you want.
03:11Perhaps you want to format it and put a percent sign in there or some
03:14extra digits in there.
03:16What I want to do in my example, I'll run the application to show you, is rather
03:20than have numeric data like this, let's say this is a time slider for a video
03:25scrubber, so I want to move back and forward in the video and I want to show
03:29hours and minutes and seconds.
03:30So I've changed the way the tool tip is rendering.
03:33It's still a decimal value underlying the data, but what I'm doing is running
03:37this through a converter that turns it into a string that looks like hours,
03:41minutes and seconds.
03:42Slide this up, it makes more room and let's see how this was accomplished.
03:46So I've set my slider's Minimum value to zero and my Maximum value to 14440 and
03:52then I say I have a toolTipValueConverter.
03:55This is some code that's going to run and change the output of the number into
04:00the string that I desire.
04:01So I have to have a class called HourConverter, which I do, here is my class.
04:05But before I show you the code there, I've instantiated this HourConverter class
04:10somewhere in my XAML.
04:11I've made it a StaticResource.
04:12I scroll up to the top of my app, you'll see it right here.
04:16In my Page.Resources, I've instantiated a copy of HourConverter and I
04:20gave it this name HourConverter so I can use it in my StaticResource lookup down below.
04:25To create the actual converter, you'll have to write a type that implements
04:29IValueConverter and then you have to write a method called Convert and another
04:33method called ConvertBack.
04:35What I'm doing in this code is I'm grabbing the value, incoming value from
04:40the slider, I'm converting it to a TimeSpan from the number of seconds that
04:44I've got, those numeric values and then I'm returning a formatted string with
04:49the hour's portion and then a colon and the minute portion and a colon and
04:54the second portion.
04:55Let's see that one more time in action.
05:00There it is that's my custom formatted tool tip.
05:03It's time to sum up the slider control.
05:05There is not much to say, it's really a simple control for providing numeric values.
05:10If you are interested in learning more about value converters, they are
05:14discussed in the data binding chapter.
Collapse this transcript
Showing web content and HTML with WebView
00:00The WebView control is what is generally called a web browser control.
00:04It hosts HTML content either from a URL or from a string containing HTML.
00:10The WinRT WebView uses the same rendering engine and JavaScript engine employed
00:15by Internet Explorer.
00:16I'll start by viewing the code inside this BasicWebView.xaml file.
00:21It's a simple page and I'm going to add a web view here.
00:27Here I'm specifying the source, this is going to be a location on the Internet
00:33and then I'll close the tag.
00:34That's it, it should host that page within the control. Let's try it up, Ctrl+F5.
00:43Here it is there is my web page.
00:45Now it is a limited browser, it's a viewer, not a full browser.
00:49It doesn't have the ability to right- click on things and download files.
00:53It's a viewer for the content.
00:55It's possible to write code to navigate to a new page;
00:58I'll show you how to do that next.
01:00ChangeURL is the one I'm looking for.
01:02I have a button up here that's going to change the content.
01:05You'll notice that I'm using the same site as before for the Source of the
01:10WebView, but I'm going to change that Source in my code in this Button_Click.
01:14Press F7 to go the code behind.
01:17Very simple code, I call the Navigate method on the WebView, I set up a new Uri
01:23and I specify the location of the site, in this case shazzam-tool.com, let's see
01:29what this looks like.
01:30Ctrl+F5 change URL and click on the button and here we are on the Shazzam Shader Editor.
01:36Shazzam is a tool that I wrote for XAML developers for creating custom pixel shaders.
01:41What I want you to see is that the WebView works with a regular HTML and it also
01:46works with jQuery add-ins.
01:47For instance on this screen if I go to the shader samples, this is a jQuery
01:52plugin for changing pictures.
01:55I'll hover this and click on the next button.
01:58This shows some of the sample shaders that are included with my tool.
02:03Let's click on this button, Navigate to String.
02:05Do you see this content?
02:07This was created programatically by generating a string and telling the WebView
02:11to use the content of that string.
02:13Here is the WebView control named WebView1.
02:16In the code behind I'm calling the NavigateToString method and then I specify
02:22any valid HTML that I want inside that string.
02:26This could get tedious writing hand codeed HTML like this, so it's also
02:30possible for you to put HTML assets in as files or assets in your application
02:36and load them programatically.
02:37Let me show you how to do that.
02:39I have three files that I'm going to use;
02:42the HTML page, a JPEG that's loaded in this HTML page, there is an image tag in
02:47there and the CSS file that is used by this HTML file.
02:51The WebView will do the right thing. If I load the HTML it will see that
02:56it has a CSS and a JPEG inside the same Assets folder and will go ahead and load them.
03:01As you can see, it's regular HTML, there is my link to my Sample.css.
03:06Here is the rest of the HTML.
03:09Now there are a number of pictures in this HTML, but I've only included one
03:13in the Assets folder.
03:15Now to load this is very simple.
03:20Here is my WebView. In the code behind I'm using the same technique I showed you
03:26a minute ago where I'm calling to navigate and I'm creating a new Uri.
03:30Then I'm specifying a Uri with a new prefix ms-appx-web:/// and then the HTML
03:41page comes after that.
03:42The key to making this work is to prefix this ms-appx-web is the designated Uri
03:49prefix to load web resources from the app package.
03:52Ctrl+F5 and there is our HTML page loaded from the internal document.
04:00To recap, the WebView let's you show pages from any available location on the
04:04Internet, plus it is a great control for showing rich content.
04:07Just have your design team create the HTML assets and load them into
04:11the control.
Collapse this transcript
Creating custom user controls
00:00The WinRT Framework ships with dozens of built-in controls.
00:04Chances are high that they have one that covers your common UI scenarios.
00:09But developers and designers are creative.
00:11We're going to think of a control that doesn't exist.
00:14That's why their framework includes the ability to make custom user controls.
00:18We can build a control and reuse it across many pages and projects.
00:22Here is an example of a control I've used on many projects,
00:25the IPAddressControl.
00:27In my XAML you can see that I am driving from UserControl.
00:31To create your own UserControls you can go to your project, Add a New Item and
00:39pick this template, User Control.
00:43This is an IPAddressControl.
00:44Typically an IP address has four integer values.
00:48When I create an interface for data entry it has four text boxes arranged in a horizontal line.
00:54Since I use this a lot in different projects, I decided to put those four text
00:58boxes inside a UserControl, then I just need to add the UserControl one time
01:02instead of rebuilding my UI over and over again.
01:06Here you can see I've got my four text boxes and then a label over here that says IP.
01:11Each of these text boxes has a TextChanged event.
01:14When you type a value in the text box, it's going to fire this event and all
01:18four text boxes are pointing to the same TextChanged event.
01:22Pressing F7 lets me look at that code.
01:25In the TextChanged event, I'm getting the original sender, the text box and
01:29trying to parse whether or not the value that's in that text box is an integer value.
01:34If the result of the integer is greater than 255, then I set it to 255, then I
01:40use the existing results.
01:41If it's less than zero, then I set it to 0, otherwise I just set it to 0.
01:46This way if the user types in a letter, I change it to a 0.
01:49If they typed in 500, I change it to 255.
01:51I'd like the users of my IPAddressControl to be able to set or read the values
01:57from the control programmatically.
01:59To do that I created a property called CurrentIP.
02:03It has a getter and a setter.
02:05A getter retrieves the information in the four text boxes and builds it into a
02:09string with periods between each of the numbers;
02:12then I return that value.
02:14In the setter I take the inbound string and I attempt to split it into four parts.
02:19I split on the period, check to make sure that I got four parts, if not then I
02:23throw an exception, and if I do get four parts then I take each part number and
02:28assign it back to the text box.
02:30That's it for my control.
02:31Now I'll go over to the ShowIPAddressControl and show you how easy it is to
02:36add one to my page.
02:38I'll go to my toolbox and because Visual Studio is smart about user controls, it
02:42knows I've already built and compiled this user control and it has added to the
02:47toolbox, here it is.
02:48Now I can just this up and drag it onto my designer surface.
02:52As you can see I have already got a few existing IPAddressControls up here.
02:55Let me delete this IPAddressControl I just wrote and take a look at this first one.
02:59Here, I am using that property I showed you CurrentIP to set it to 5.5.5.5, great!
03:06Here I'm setting it to another IP address with all 25s.
03:09This third IPAddressControl has its own IP address.
03:12Now I've also got some buttons down here.
03:15When I click on this first button I'm going to programmatically set the IP
03:19address with this string.
03:20Remember, my code takes the string and turns it into a text for the text boxes
03:25and in this button I'm going to read the current IP address from my IP Control
03:29and assign it to the text box.
03:34That worked, the top IP address has changed to the one I set in my code and this
03:39bottom one I'm going to read the value.
03:41Let me change one of these octets to 34 and try to read it. That worked.
03:46Remember, I also had that guard code to make sure I can't type things like the
03:51letter A. I'm going to type the letter A in this text box.
03:54That gets converted to zero.
03:55I'm going to type 100 works, but if I add another 0 gets set back to 255, very nice.
04:00Now this is a nice control but it's not quite ready for working in WinRT.
04:06One of the problems with this control is when I implemented my IP address;
04:11I implemented it as a normal .NET property.
04:14By doing it this way I cannot animate this property, I can't use data binding on this property.
04:20So it's better if I use what's called the Dependency Property for this.
04:23I'll show you how to do this in my other control.
04:25My other control is this one here called GaugeControl.
04:29What I wanted was a gauge value where I could type in a number between 0 and 360
04:34and have this hand move around the screen based on the degree value.
04:39It's like a compass if you think about it.
04:41So what I did is I imported a few assets, I have a picture of a hand and I have
04:48a picture of this black circle in the background with some gradients on it and
04:53the green represents a transparent area.
04:55I'll build my control from these two PNGs.
05:00There is the image, and there is the other image.
05:03I need to write some code to programmatically move the hand around the gauge
05:07dial and I need to create a couple of dependency properties.
05:10I'll show you how to do that.
05:11I'll open up the C# file and I have two dependency properties.
05:15I have a Degree property and a DegreeString property.
05:19So I should be able to set the degree, say 15 degrees and the hand should move
05:23to the 15 degree position.
05:25A syntax for DependencyProperty can be quite complex.
05:28Let's see if we can read through this briefly.
05:31Here I am declaring a DependencyProperty named DegreeProperty and then I'm
05:35registering it with the DependencyProperty system.
05:38That makes it available for a data binding and animation.
05:40I give it a simple name, I specify the type of the property, it's going to be a
05:45double and I specify the affiliated control, the GaugeControl.
05:49And then I set a default value here in the PropertyMetadata and I also specify a
05:54method to call when the value changes OnDegreeChanged.
05:57Now here I have a regular .NET property that just makes some calls to
06:02the dependency system.
06:03This is so C# and VB developers can call my method without having to go to
06:09through the Dependency System to change the value.
06:11And down here we have the OnDegreeChanged, which is setting a bunch of data
06:16values in here and then there's also an OnDegreeChanged, which provides the
06:21derived class a way of handling changes to the DegreeProperty.
06:26Here I'm setting the DegreeString.
06:28Now I have another DependencyProperty down here, I'm not going to show you
06:31the code, but what I'm doing here that was on the DegreeChanged is I am
06:35building a string and I'm using the little degree symbol, so when somebody
06:38gets that formatted version of my string it has this little circle on the end of the degree.
06:43So that's my control, I am going to add it to a page like this.
06:46Again I can drag it from my toolbox if I want it to, here is my control, I gave
06:51it a name of Gauge1 and then in my code behind I'm going to change it
06:55programmatically with this button and with this slider, I will read the Degree
06:58String value out using data binding and place it in this TextBlock.
07:02That's what I'm doing here.
07:04Get the Gauge1, read the DegreeString property.
07:07Remember I couldn't do this unless I made a DependencyProperty and then assigned
07:11it to the text property of the TextBlock.
07:12The other bits are done in code, press F7. When you click on the button, I'm
07:17setting the degree value to 50 and when you move the slider, I'm getting the
07:21slider value and assigning it to the DegreeProperty.
07:24Press F5 to run the app, click on GaugeControl and I'll click on the button,
07:29let's just set it to 50 and you see there is my DegreeString and then I'll move
07:33the slider and it's changing two things.
07:36It's changing the angle of the hand and I'm getting the new DegreeString back
07:40from the control, very nice.
07:43Let's summarize what we've seen.
07:44You can create a user control and build the UI from existing controls.
07:48That's what I did with the IP Address Control.
07:50I put a wrapper around a set of text boxes.
07:54Next you add Dependency Properties to the controls so that other developers can
07:58change the features of your control.
07:59Finally, if you think you've created a beneficial control, you might consider
08:04bundling it into a shareable package and distributing it to other developers.
Collapse this transcript
10. The Application Life Cycle
Surveying the application life cycle
00:00Traditionally a Desktop application, like those that run in Windows 7 can be in
00:05one of two execution states, running or not running.
00:09Widows Store Apps have a third potential state known as the Suspended State.
00:15The Windows RT runtime automatically suspends apps under certain circumstances.
00:20You cannot prevent this from happening.
00:23Why have a suspended state?
00:25It prevents apps from monopolizing system sources.
00:29It also saves the battery because there is less resources draining the power.
00:34When you have an app in full view there is only one app running.
00:39If you have one app in snap view and another in fill view, then you have two apps running.
00:44Two apps is the maximum that can be running simultaneously.
00:48All of the other apps are in a suspended state.
00:52On this slide I have three apps that are suspended.
00:56The basic steps of the application Lifecycle are shown here.
01:00We have an app that is not running, in red.
01:03The user touches the app tile and the up transitions to the running state.
01:08Eventually, the user switches to another app and the operating system
01:12transitions your app to the suspended state.
01:14Don't be scared of the suspended state.
01:18All that reading means is your app's threads are suspended.
01:22It's frozen at whatever state existed when the app transitioned.
01:26All of your data is intact.
01:29Sooner or later the user switches back to your app.
01:33The app instantly transitions back to the running state;
01:36you need to do nothing special.
01:38The app appears and continues running whatever task it was doing when it was suspended.
01:43The operating system can kill an app too.
01:46In this situation, the app transitions from running to not running without
01:51passing through the suspended state.
01:53Apps can also be terminated while in the suspended state.
01:56This usually happens when the operating system detects too many suspended apps
02:01and it starts terminating the stagnant ones.
02:04Let's review what we know.
02:06There are three states; not running, running, and suspended.
02:11There are six transitions between these states, launching and activating,
02:15suspended and resuming, and terminating and killing.
02:21The framework contains lifecycle events that provide us tools to work with these transitions.
02:26Use these events to initialize, save and restore a state if necessary for your app.
02:32There are two ways to get to from not running to running, when the user
02:37presses primary or secondary tile that initiates the launching process, which
02:42calls the on-launched method.
02:44When the app is started by a contract, let's say by the consumer searching with
02:49the search charm, that initiates the activating process.
02:53There are several on-activated methods available.
02:56An app can transition from running to not running under a few conditions;
03:01this is called killing the app.
03:03When the user clicks the End Task button in Task Manager is one example.
03:07Another is when the user logs off or shuts down the computer.
03:11Also, if your app crashes, the app is killed.
03:15While this is bad for your app, it's good for the consumer.
03:19Their computer is not littered with unstable apps.
03:22Note, you will not get any notifications when the app is killed, which means you
03:27cannot write any code to deal with this situation.
03:31An app can transition from suspended to not running.
03:34This is called terminating the app.
03:36Just like the killing apps scenario, you are not notified that the app is being
03:41terminated but that's okay.
03:43You have an opportunity to save a state when the app switches to the suspended state.
03:48Killing an app is a rude activity and is a rare occurrence in your app lifecycle.
03:53Suspending an app is a common occurrence;
03:55it happens all the time as the user switches between apps.
03:59Immediately before the suspension occurs, the suspending event is raised.
04:04This is the time for you to save any necessary app state.
04:07Do it now, because it is possible that the app will be terminated without any
04:12notification before it ever runs again.
04:15Also this is a good time to release any printers, files or other exclusive
04:19resources that the app is holding so that other apps can use them during
04:23your suspension phase.
04:24The resuming event happens during the transition back to the running state.
04:29It is rare to do much on the resuming method because the operating system has
04:33preserved all of your app state anyway.
04:36I'd like to show the suspend and resume cycle in action.
04:39One way to do that is to use the Task Manager.
04:42I like to use the Control+Shift+Escape key combination to bring up the Task Manager.
04:47I'm going to change a few options.
04:50I'm going to show more details.
04:52I am also going to make the Task Manager float on top of all of the other ones
04:57by choosing Always on top.
04:59And then finally to see the suspended status I need to choose View>Status
05:05Values>Show suspended status.
05:08Now I see that I have two apps that are in suspended state, let me switch to those.
05:13I'm going to press Window tab to show my running applications, and then I'm
05:18going to choose the Store App.
05:22In Task Manager you see that the Store App is now active and the Weather App is suspended.
05:27Now I'm going to switch to the Weather App with the Window key tab and choose Weather.
05:35In Task Manager I see that the Weather is now active and if I wait about 10
05:39seconds, the Store application becomes suspended.
05:43Now I will close the Task Manager.
05:47Here are some tips and best practices.
05:49The launching and activating event are perfect for initializing state in your application.
05:55If the user has important data like what level they are playing in the game,
05:59always save that session state during suspension.
06:02Save your state quickly, you only have five seconds to get the job done or
06:06the app is terminated.
06:08The app life cycle for Windows Stores App is a well-designed system that
06:11provides key events for initializing and saving your application state.
Collapse this transcript
Writing and debugging life cycle events
00:00I'll use this project to show you the app lifecycle events and how to
00:04successfully debug the suspend, resume and terminate events.
00:08I'm inside the solution that has three projects, StartupEvents, SuspendAndResume
00:13and UseSuspensionManager.
00:15I start by showing you the code in this first project.
00:17I'll make sure it's the StartUp Project by clicking here and then I'm going to
00:22open up the App.xaml.cs file by double-clicking here.
00:27Remember that the OnLaunched method is called whenever the application starts.
00:31In this case it's when the user clicks on the main tile or a secondary tile
00:36for the application.
00:37What does my code do?
00:38Well I have the Default OnLaunched Code and some place in there it's setting the
00:42first page of the application, it's calling Navigage.
00:46And what I'm doing additionally is I'm sending in some additional arguments here.
00:50What am I sending in?
00:52The information that I got from the OnLaunched method, the
00:56LaunchedActivatedEventArgs.
00:58I'm also listening for the Activated handlers.
01:01In particular I'm listening for the OnSearchActivated.
01:05This is called when the user works with the Search Charm. What am I doing in this method?
01:10I'm also calling the Navigate method and passing in a set of arguments.
01:15Either way the application starts either by launching it or activating it, one
01:20of these methods is going to get called and they're both going to send it to the MainPage.
01:23One of the differences is, is I'm sending a different set of arguments.
01:27In the second one I'm sending the SearchActivatedEventArgs.
01:29And I'm doing that so I can show you whether the user clicked on a tile or
01:35whether they used a search charm.
01:37Now over in my MainPage.xaml inside the C# code, in the OnNavigatedTo method,
01:46I am reading the information from this inbound parameter and checking to see if it
01:52was one of those two EventArgs type.
01:54I'm using this interface to check that, IActivatedEventArgs.
01:58And if it is, I'll show you the kind of startup. Let's see this in action.
02:02I'll press Ctrl+F5 to run the app and it says that I started it with Launch.
02:09Next I'm going to bring up the Charms menu by pressing Windows key+C and
02:14then clicking on Search.
02:16When I click on the Search button, keep your eye what happens in the text on the
02:21page, I'm going to click now.
02:23Now you see it's already activated that with the Search Charm, so you can
02:27differentiate between these two actions.
02:30I'll shutdown the application by pressing Alt+F4, returning to Visual Studio by using Alt+Tab.
02:36Then I'm going to make this next project, SuspendAndResume, my startup project.
02:43Now I'll show you a different set of events, the Suspend Event and the Resume Event.
02:48I'll also show you how to debug these events and later I'll show you how to
02:52persist the session state during the suspend process.
02:54I'll open up App.xaml.cs again and in the app constructor I'm setting up an
03:02event handler for the Suspending Event and the Resuming Event.
03:06Remember that the Suspending Event happens when the App is transitioning to
03:10the Suspended State.
03:12This is where I should save my state.
03:15Let's see what we have down here, here is my OnSuspended method and
03:19my OnResuming method.
03:21The most important thing I need to do in the OnSuspended method is at some
03:24point save the state.
03:27But I'll also like to show you this thing called the GetDeferral.
03:30Without the Deferral the app gets suspended as soon as the code in the
03:33OnSuspending handler is finished.
03:36This is not ideal if you are making asynced calls within the OnSuspended
03:40handler, because your threads get suspended before they can finish the Async job.
03:45You use the Deferral to indicate you are doing something Async in this method,
03:49and then at some future point you have to call deferral.complete to say you
03:54are ready to be suspended.
03:55Now note you only get five seconds to write code inside the OnSuspending Event.
04:01The deferral does not give you any extra time.
04:04If you exceed the five second limit, your app is terminated. Let's debug this.
04:09I've got a breakpoint in the OnSuspending and the OnResuming.
04:14Remember to do a breakpoint you press F9 to turn it On or Off.
04:17I'll press F5 to run the application with the debugger attached.
04:22I'll switch back to Visual Studio and I want to simulate the suspend event cycle.
04:27To do that I can use this toolbar with the Suspend, Resume and Suspend and
04:32shutdown, this last one simulates a termination.
04:35Now if you do not see this menu, right-click and choose Debug Location.
04:40Let's see this happen.
04:42I'm going to click Suspend and my suspend code is running.
04:47I'll step to the code, get my referral, save my state, referral complete and now
04:52at this point my app is in Suspended mode.
04:55To show what happens when you Resume, choose this item.
04:59As expected it is my breakpoint.
05:02And then the last demo choose Suspend and shutdown.
05:06There is my un suspending event,
05:09Now of course this is great seeing these events but I'm not actually saving any state.
05:14So let's talk about the kind of state you can save and we'll use a class that
05:18Microsoft provides called the SuspensionManager, which makes it relatively easy
05:21to write the SuspendandResume code.
05:24To do that I'm going to go to this last project and make it my StartUp.
05:29I'll start with this TypesOfState.
05:31Now this lists the different kinds of state that you can have in an application;
05:35Application Settings, User Settings, Application Data and User Data and
05:39finally Session State.
05:41Most of these are important to the app, but they're not what we're talking
05:45about in this demo.
05:46What I'm talking about in this demo is the session state, that's important for
05:49what the user is doing at the moment and you might want to restore when you
05:53restart the app, if the app was ever terminated, like the item that was selected
05:57in a list box, or some partial data that they filled in a data entry page.
06:02You have to decide what's important at the moment to save in Session State.
06:06To save it I'm going to use a class called the SuspensionManager class.
06:10Now for some obscure reason the SuspensionManager is not part of the WinRT class
06:15hybrids, instead it is part of the Visual Studio Templates and it can only be
06:19added to new projects by creating one of these templates.
06:22Now I've included it here in this project, if you want your own copy, just
06:26grab it out of here.
06:28Let's see how you save state.
06:29I'm going to go to my MainPage and show you the Main UI.
06:34I have a list box and when you click on an item in the list box, it fires the
06:38SelectionChanged event.
06:41I'll press F7 to show you that code.
06:45When the user selects a new item, I'm calling the
06:48SuspensionManager.SessionState.
06:51Now remember I added the SuspensionManager class to my project, I'm not going
06:55to show you the code that's inside the SuspensionManager, I'm just going to
06:58show you how to use it.
07:00So what am I saying here is I have my own unique identifier, WeekDayIndex, and I
07:04want to store the selected numeric value in there.
07:06Now at some future point when we come back into this page, I'll check to make
07:11sure that the session state contains that key.
07:14And if it does, I'll retrieve the information here, cast it to an end, because
07:19that's what I need, I need an integer value, and then I'll make my WeekDayListBox
07:22used as SelectedIndex.
07:26So that's getting and retrieving the value.
07:28Now of course I need to save this in the SuspendedEvents, so let's go show you that.
07:32Over to App.xaml, here we go inside this file and down here I say when you're
07:40suspending, call the SuspensionManager. SaveAsync, that's all you need to do.
07:46The SuspensionManager figures out where to save the file and does it automatically.
07:50Now of course in the StartUp on the OnLaunched, I'm also restoring the Async.
07:57So I'm restoring it here, I'm saving it here.
08:01I'm also checking what the PreviousExecutionState of the application.
08:04If the application was terminated or the application was not running, then I want
08:09to go ahead and restore the SuspensionManager values.
08:11So that should be good enough.
08:13I've got some breakpoints in my code and watch this run.
08:16I'll press F5 to run the application with the debugger.
08:19You see that I'm restoring the Async values here in the StartUp and then inside
08:23the OnNavagatedTo, I'm retrieving the value.
08:28Here is my application, I'll pick Tuesday from my list.
08:31You see that the SelectionChangeEvent is firing, I'm saving the data.
08:36I'll just suspend the application at this point, so we'll say Suspend, and I see
08:42my code is saving that information and then I am done.
08:47So at this point I could shutdown the application and restart it, press F5, and
08:54I'm restoring that Async value and then I see if it works correctly.
08:58I'll come over here, it worked, I'm restoring this.
09:05I'm getting some error message from Visual Studio about the debugging code;
09:08you can go ahead and ignore those.
09:11And there in the UI is my selected item.
09:14It's nice that Microsoft provides such a rich set of events for our apps.
09:18Using the SuspendEvent to Save SessionState is the key to make the user love
09:22your app even if it terminates unexpectedly.
Collapse this transcript
Startup and custom splash screens
00:00The Splash Screen is the first screen shown to your users when starting the app.
00:04Every Windows Store App has one and it is shown automatically during Startup.
00:09To show you this example, I'm going to use this ChangeDefaultSpalshScreen
00:14project, make sure it's your startup project and then, I'm going to look at this
00:19SplashScreen.png file that's inside this Assets folder.
00:22This is the default file that Microsoft includes.
00:26The green color indicates the transparent color.
00:28I'm going to start the application to see what it looks like, Ctrl+F5 and
00:34there's the Splash Screen and there is my main UI.
00:37We choose Alt+F4 to switch back to Visual Studio and then what I'm going to do
00:42is I'm going to open up my Package. appxmanifest file and I'm going to look at
00:48the settings for my Splash Screen.
00:51Right now, I'm using the default Splash Screen, if I want to keep using the one
00:56that Microsoft provides, I can just change the background color here.
00:59Let me show you this.
01:01I'll do pound (#) and then I'll run the application again.
01:04Don't blink and there is my new Splash Screen with a slightly different color.
01:08Again, I'll shut down the application to return, that's not a very
01:12interesting experience.
01:13So let's change out this png file.
01:16I'll do that by right-clicking on my Assets folder, Add>Existing Item and then
01:25I'm going to go to my exercise files and inside the exercise files is an Assets
01:30folder and inside there is a Images folder, which contains another folder called
01:36Splash and I'm going to open this SplashScreen.png file.
01:42Now it says I already have a file with that name so I'll go ahead and replace it.
01:49And now when you run the application, again don't blink, there is our
01:53custom Splash Screen.
01:55Now notice how fast that Splash Screen disappears.
01:58That's good, it gets the user to starting the application as soon as possible,
02:02but how does the framework know when to dismiss the default Splash Screen?
02:05The answer is in the App.xaml file.
02:08I'll shut down this application, Alt+F4, return back to Visual Studio, visit my
02:16friend the App.xaml.cs file.
02:20This line of code right here, Window. Current.Activate tells the WinRT runtime
02:25that we are done and it's time to dismiss the Splash Screen.
02:29Now let's talk about the Startup process.
02:31You have to call Window.Current. Activate within 15 seconds of the app starting
02:37or the operating system considers your app to be unresponsive and will kill it.
02:41What do you do if you need longer than that to start your application?
02:44The best solution is to rethink your Startup process;
02:47perhaps you can start another page and switch to the Main Page later.
02:51This pattern is sometimes called the extended splash screen.
02:55Let me show you a simple example.
02:57I'll change my Startup project to this ExtendedSplashScreen, I'll open up
03:03App.xaml in the C# file, double-click on that, the first thing I want you to see
03:10is I'm loading a different page. I'm not starting with the Main Page anymore.
03:14I'm loading this file called FakeSplash.
03:16Now usually it's called Extended Splash but I want to make a point here.
03:20I'm calling it the FakeSplash.
03:22It's going to look like the real Splash Screen but its going to run some code
03:26to retrieve the data.
03:28It's going to look like the real Splash Screen, but it's not.
03:31So here it is, FakeSplash.xaml, you can see that I've got a blue background.
03:38I have the text Fake Splash at the top so we can see it during the demo.
03:43It's hard to see it but I've also add a ProgressRing, which will rotate while
03:49we're in the process of showing this screen, you see that I've set the IsActive to True.
03:53Now let's go look at the code behind for FakeSplash.
03:57Remember this is first screen that's loaded.
04:01When you navigate to this page, I'm going to instantiate the code to get my data
04:05that I need for the start up of the application.
04:07I'm going to call a method called GetSlowData.
04:10If you look at the code for that, you'll see that it's just waiting for five
04:13seconds. Then when that's done, I navigate away from the Fake Splash Screen,
04:18to the screen that I really want to see, which is my MainPage. Here is what it looks like.
04:23I'll press Ctrl+F5, there is the Fake Splash, and then after five seconds, it
04:30switches to the Main Page.
04:32The Splash Screen is important to show at the application launch to reassure the
04:35users that the app has started.
04:38Using the Splash Screen image file, it is easy to have a brand experience at
04:42the same time.
Collapse this transcript
Managing application settings
00:00Saving your app settings is incredibly easy in WinRT.
00:04I'm in a project called UsingApplicationSettings and I'm going to open this
00:09MainPage.xaml file by double-clicking.
00:12I'm going to hold down my spacebar and use my mouse button to drag this up
00:17where I can see it.
00:18On the left side of the screen is a Get Newsletter toggle. The user can turn
00:22this on or off to indicate whether they want to receive a newsletter.
00:26Then I'll save that setting by clicking on this button to the Local Settings.
00:31Local Settings means that they're stored on the local computer only.
00:35Then in this button over here I'll read that information let me show you how easy this is.
00:40We'll double-click on this button and here is the code.
00:44I start by working with the ApplicationData class I'm calling .Current.
00:49That gives me the current application engine and then I ask for a subset of the
00:54data in there called Local Settings right here.
00:57I stored that in this variable and then I'm going to add my own item to the Local Settings.
01:03I do that by going to the Values collection, which is a dictionary.
01:06So I provide a dictionary key called HasNewsletterSubscription and then over
01:11here I choose what to store, this toggle switch IsOn property.
01:17Later when it's time to read the information, I again go out the ApplicationData
01:21class, get the Current store, ask for the Local Settings and then down here
01:28I read that information back using the same key, then I check whether I got a
01:34value back, I check for not null.
01:36And if it's not equal to null, then I retrieve the value and put it in this text box.
01:40Otherwise I say it's not in the Local Settings.
01:44Now I'll switchback to the UI and show you the next section.
01:48With a roaming profile, the settings can be synchronized across machines.
01:53Imagine your application is an E-book reader and you want to save the page the
01:57user is currently reading.
01:59So in this section on the left side of the page, I have two text boxes for the
02:04Users Name and their Country that they live in and then I have a button to Save
02:08to Roaming Settings and a button to Read the Roaming Settings.
02:11So let's see what the code looks like for Roaming.
02:13It's nearly identical.
02:14I started with the ApplicationData, I asked for .Current, I specify this time
02:19I'm looking for something in the RoamingSettings.
02:21Then I assign two values using this key, UserName and Country.
02:27To read the values my code is slightly different.
02:30I'm getting the Current RoamingSettings here and then I'm retrieving this string
02:34values for the UserName and text.
02:37Now these could come back as null, so what I did in these examples, I put a
02:41catch block down here and if there is nothing in this values, it will run this
02:45code down here and it will say Not in Roaming.
02:48Now let's see what this looks like.
02:50If I run the application by pressing Ctrl+F5. I'm running the app on a local
02:55machine, I'll choose to sign up for the newsletter and save the settings and
03:00then I'll say, Get the Local Settings. I want the newsletter.
03:03Now I'll come down here and write in my Name and my Country and I'll save that
03:09to Roaming Settings.
03:11Before I click on this let me click on this button here to show you there
03:15is nothing in roaming.
03:16I'll click the Save in Roaming and I'll come back up here and click this button
03:20and that data is there.
03:21Now I can go to another machine and I'm going to simulate that by shutting down
03:25this application, Alt+F4.
03:29Now that I'm back in Visual Studio, I'll test this on another machine so we can
03:33check the roaming profile by choosing RemoteMachine and then pressing Ctrl+F5 to
03:39move this to the surface.
03:41Now that I'm inside the application, I'm going to click on the Get Local
03:45Settings and you'll notice that there is nothing set in the Get Local Settings.
03:50and I'm going to click on the Get Roaming Settings button and you'll see that I
03:55retrieve the information that I saved on the other computer.
03:58The Application Data Class makes it simple to read and write values to
04:01the desired location.
Collapse this transcript
11. Tiles and Toast
Configuring basic tiles
00:00Everytime a Windows 8 user logs on to their computer, they see the Start screen.
00:06The tiles in the screen are a distinctive feature of Windows phones and Windows devices.
00:11Your app will have a tile on the screen.
00:13It's the face of your app and is competing with all the other tiles.
00:17To run an application, you press the Tile.
00:19I'm going to press this Tiles Introduction and the application starts up.
00:24Now all tiles are added to the start screen when you install the application and
00:28the user can take those tiles and remove them and change tile options.
00:33Let me show you what I mean.
00:35I'll press Alt+F4 to close this application and let's look at this tile
00:40here, the News Tile.
00:41If I am on a touch device, I can press and drag down on this to see the options.
00:48If I'm on a mouse computer I right- click, the bottom app bar shows up.
00:53First thing you can do is you can unpin this tile from the start menu or you can
00:57uninstall the application, you can also change the size of the tile.
01:02Let me show you that.
01:03I'll click on the Larger and then I will right-click and set it back to Smaller.
01:10Now let's see if we can do that on my tile, I'll right-click. All I have are
01:14options here are Unpin from Start and Uninstall, I need to figure out how to
01:19add a wide tile to my application, I'll return to Visual Studio to show you how to do that.
01:24I'm inside this Tiles Introduction solution and I'm going to be working with
01:28this basic tiles. The one that you saw on the Desktop was this one down here
01:32TilesIntroduction, but we're going to start with this one so I can change
01:34out the tile graphics.
01:36This Logo.png file is the basic logo that shows up on the tile.
01:40Let me double-click on it to show you what's here.
01:42It's a white box in the center.
01:44The green signifies the transparent area.
01:47I'm going to change this logo for my own.
01:49I'll right-click on Assets, I'll chose Add>Existing Item.
01:55I'll go to my exercise files and then open this Assets folder and I'll open the
01:59Images folder and then I'm going to add two PNG files;
02:04Logo150.png and then I'm going to Ctrl+ Click to choose LogoWideRed.png and then
02:13I'll click on the Add button.
02:15Now I'm going to tell Visual Studio to use these new PNG files for my tile.
02:21To do that, I'm going to open up my Package.appxmanifest file.
02:24Let me go to the Application UI section, I'm going to change out the main logo,
02:31the 150x150 logo by clicking here on this dot, dot, dot (...)
02:35button to choose the file.
02:37Now I'm looking for the file inside my project, so I'll go to this Assets folder
02:41and choose Logo150.png.
02:43I'm getting the error message here that says the mixtures of images with and
02:47without the "scale" or " targetsize" qualifiers exists.
02:50What that means is that I've got a logo specified here with no size indicators
02:55and another one specified with a scale indicator, this was created automatically
02:59for me when I added it to Visual Studio.
03:02I'm going to delete this logo file here in Visual Studio and that fixes the problem.
03:10Next I'm going to add my Wide Logo tile.
03:13I go here to Wide Logo and I'll do the same thing I just did.
03:17I'll click on this button down here and choose the LogoWideRed.png file and then click open.
03:22Again it's complaining about the scale and duplicate files, I'll go over here
03:27and delete this LogoRedWide file and then OK.
03:33And now I'm ready to run the application.
03:35I'll press Ctrl+F5 to run. The app has run;
03:40I've just deployed it to the Desktop by doing the same thing.
03:43Another way I could have done this would be to deploy, by going back to Visual
03:48Studio and choose Build>Deploy BasicTiles.
03:52Now I'm going to press the Window key on my keyboard to go to my Desktop, scroll
03:57to the end of the list and now I have the WideTile. I can right-click on this
04:01and choose to make it smaller and you see my custom logo there.
04:05So I have two versions on my tile, one with the blue logo and one with the red logo.
04:11That's the fundamentals of showing simple tiles for your app.
Collapse this transcript
Understanding live tiles, toasts, and the notification system
00:00I want to take a few minutes and talk about the different techniques available
00:04for notifying your users of status changes and error messages.
00:07Inside your application, you can build Custom pages and show the message, or if
00:12you need a model screen, the system Dialogs and Popups can handle the job.
00:16What about notifications that appear when your application is not running?
00:20For these scenarios, WinRT provides the Live Tiles and Toast notifications.
00:25A much touted feature of Windows 8 is the Live Tiles.
00:29What does live mean?
00:30It means you can update the information on the bottom of the tile.
00:34In this example, you can see the news application has an updated headline and in
00:39my mail application, I can see that I have six unread Emails.
00:44Toasts appear and hover over the current visible app.
00:47On the bottom on the screen, you can see that I have the Explore California
00:51app open and the green rectangle represents a toast that's popped up from another application.
00:57So toasts always appear over the current visible application. By default, they're
01:01located in the upper right-hand corner of the screen, they disappear after a
01:05time limit or when the user presses the toast.
01:08You get to chose when a Tile or Toast is updated. There is something called a local update.
01:14In that case, you call a frameworks update method while your app is running and
01:18the Tile or Toast immediately sees the change.
01:21You can also schedule a one time update for Tiles and Toast.
01:26Here you call the frameworks AddToSchedule method, and then the Tile is updated
01:30at that future time.
01:32Now the interesting thing about this is the app does not need to be running
01:35at that schedule time.
01:37The Tile will show the update for three days and then it will revert back to the previous UI.
01:42There's another type of Scheduled Update called a Periodic, this is only
01:45available for Tiles.
01:47You call the framework StartPeriodicUpdate Method in your code, and you have to
01:51specify URL that returns some information to your application, it's going to
01:55return the tile UI to show and the next update time.
01:59At the scheduled time, the tile is updated based on that downloaded xml file.
02:04It also schedules another update based on the information found in the update time file.
02:09Again, the app does not need to be running at the scheduled time.
02:12The final type of Update is known as the Push update. In this case, the update is
02:17pushed to your machine from the Microsoft Server.
02:20Who gets to see this update?
02:22You could broadcast the message to all your users, you can single out one
02:26individual for the message or a sub set of users.
02:29Here is a great example, the Weather app needs to show a storm advisory to some
02:33users, so you'll look to your user base to find out who's in the affected region
02:37and send messages only to those people.
02:40Push notifications are a powerful addition to your app, but it also takes the
02:44most work to set up.
02:45To work with Push, you need to have your application;
02:47you need to have access to the Windows Notification Service.
02:51You need to provide a Cloud service that communicates with the Windows
02:54Notification Service and your app.
02:56Now some developers like to build.netwebservices for this.
02:59The Microsoft Assure Team has a nice tool kit to simplify working with the
03:04Windows Notification Server and they host this service on their servers.
03:08Let's walk through the steps to get a push notification.
03:11An app request a notification channel from the WNS server, this is done
03:16through the WinRT API.
03:17A channel is a unique address that represents a single user on a single
03:21device for a specific app.
03:23Channels expire after 30 days;
03:25therefore if the user doesn't use your app, the channel is purged from the WNS server.
03:30Its best to ask for channel everytime you start your app.
03:34WNS returns a channel URI to your app.
03:38Your App sends a channel URI and any additional information to the Cloud service.
03:42The service stores information for all of your users.
03:47When you are ready to send a notification, your Cloud service creates a list of channels.
03:51In the weather app example, your Cloud service would get the weather advisory
03:55from the authorities.
03:56It looks with data associated with each user and determines who is in
03:59that geographic region.
04:01The Cloud service sends the channels to the WNS server.
04:05It also sends a custom XML needed for the Tile or Toast.
04:09Finally, the Tile or Toast XML sent to each user's computer. This is possible
04:14because at the time the channel is requested from WNS, it stored connection
04:18information about each channel's local computer.
04:21Now the next time the user logs in to their computer, they get the notification.
04:26If you want to read more about WNS, check out the detailed pages on MSDN.
04:30I'll discuss Live Tiles, Secondary Tiles, and Toast in other videos in
04:35this chapter.
Collapse this transcript
Creating live tiles
00:00To create a Live Tile, you write code to change the tile text and images, you
00:05pass your request for a new tile to the operating system.
00:09It guards against direct access to the Tiles. In other words, you can't design a
00:13fancy UI in XAML and use that for the tile. Instead, you must create an XML file
00:18containing the new tile information.
00:20Within that file, you must specify the XML Template name, Values for the text
00:25fields and the locations of images to use on the tile.
00:29The framework contains 47 predefined XML templates broken down into four categories.
00:34There are square templates, two varieties:
00:36static and animated. And two versions of the wide templates:
00:40static and animated.
00:41Here are some examples of Square Templates.
00:44I took these screen shots from the public MSDN help pages. The top left tile is
00:49a picture only tile, the rest are text only templates.
00:52Here, are some examples of the animated squares.
00:55They don't look square in the screen shots, but here is what happens when you
00:59chose these templates; the top half is shown for a few seconds then the
01:02animation scrolls to the bottom half.
01:05After a few more seconds, the top half animates back into view.
01:09These are some illustrations of the wide static template.
01:12My favorite is the one with the five pictures.
01:14You see that one used on the people tile.
01:17And finally, a few examples of the animated wide tiles.
01:20To create a Live Tile, you have to create an XML file.
01:23You can hand code the XML or use an XML literal like shown in this example.
01:28There are three areas to note in the XML file.
01:31You must identify the template name.
01:33Here I'm using the TileWidePeekImageCollection01, which is an animated tile.
01:39You must also specify the image source and the text values.
01:43For convenience, the framework includes the TileUpdateManager class.
01:48This class can retrieve the XML template using the tile template type
01:52enumeration, what you get is the RAxML.
01:56Then you use the XmlDom to replace the text and image locations within that RAxML file.
02:01Let me show you how this is done in Visual Studio.
02:04I'm inside this project called LiveTiles.
02:07All the code is in this MainPage.xaml and MainPage.xaml.cs.
02:12Let's look at the UI first, I'll double- click on this file, it's a simple app.
02:16It just has five buttons and when I click on this first button, I'm going
02:21to create a Live Tile.
02:22I'm going to do that by using a literal XML string.
02:25I'm specifying the tile, the visual, the binding, which includes the template
02:30name, and then I'm specifying that there are two text areas.
02:35The first text area is going to say New Tour Info and the second text area is
02:39going to say, Romance and Wine Country.
02:41Now that I have my XML string, I'm going to create an XML document on line 42
02:45and load that XML string into the document.
02:48Now on line 46, I'm going to request a notification using that document.
02:54So the variable on line 46 contains the potential of notification.
02:58When I'm ready to update, I go to the TileUpdateManager class and I ask it to
03:03create a TileUpdater for my application then I call update on it and I pass in
03:09my notification document and see what this looks like.
03:11I'll press Ctrl+F5 to run the application.
03:16I'll press the Windows key on my keyboard and go look at my current tile.
03:21It says Explore California in LiveTiles, using the WideTile format.
03:25Now I will return back to my application and click on the button.
03:29I'll switch back to the Start Screen, with the Windows key and look at that tile
03:35and switch to the smaller version and there. Did you see they animated into place?
03:42There is my new text.
03:45Now let's go back to Visual Studio.
03:47On the next Button_Click, I'm going to use the GetContentTemplate.
03:51Remember that this goes out and gives me the RAxML so I get an XML document from
03:55the framework and I tell it what tile type I'm looking for in this case, it's
03:59the TileWidePeekImageCollection01.
04:03This contains just RAxML with some place holders in it, so next, I need to use
04:07some technique to go into that XML file and change the values.
04:10One way to do that is to use the XML DOM, so here on line 56, I'm getting all
04:15the text nodes in that document and on line 57, I'm getting all the image nodes.
04:19Then I go into the text node, I get the first item in the textNode and I add
04:24some text to it, Vacation Pics.
04:27Do the same thing with the next textNode.
04:29And then this bit of code down here is going to swap out the sources for some
04:34images that I have inside my application.
04:36If you look inside my Assets folder, you'll see I have these files called
04:39Abstract 1, 2, 3, 5 and 8.
04:41Down here, I specify an image node and then I'm setting the attribute source
04:47equal to this location for that file.
04:50Do that for several of my images and then down here I do the same things I did earlier.
04:55I get my notification passing in my xmlDoc.
04:57And then I create my application and then finally, I create the tile updater and update.
05:02Now notice up here, you could specify an expiration time for this tile and it
05:06would revert back to the original tile.
05:08Let's see what this one looks like.
05:10Switch back to the application, I'll click on the second button, I'll switch
05:13back to the start screen with the Windows key. Remember this is the WideTile
05:18format so I'm going to right-click and choose Larger.
05:23And there are my new tiles.
05:24I've got five pictures on it and it's animated so then it rotates up and shows
05:28the two bits of text, and then it animates down to show the pictures again.
05:34Back to Visual Studio.
05:36Here is how you would schedule an update.
05:37You're going to get the content like before, you're going to change the tile
05:42UI like before, you'll schedule a due time in the future, in this case six
05:46hours in the future. Then I'm going to use the ScheduledTileNotification and I
05:50pass in the xmlDoc and the future time and then once again I call a
05:54CreateTileUpdaterForApplication.
05:55Now I'm not going to sit here and wait for six hours to show this demo, so you
06:00have to run it on your own time.
06:02Finally, I wanted to show something else you can add to a Live Tile.
06:05They are called Badges.
06:06Now badges are a numeric or a graphical glyph that is shown on the bottom of the
06:10tile. Again you need to use XML to create a badge.
06:13Here is an example.
06:14I want to use a badge and I want a numeric value of 42.
06:18So I'll start again by loading that into an XML document, this time I'll ask for
06:22a badge notification, not a tile notification.
06:25And then I'll say Badge Update Manager, create the badge updater for the
06:29application and then the rest of the code is the same.
06:32Before I show the demo, I'll show you the way you put a symbol on there.
06:36Here I'm going to use badge value = newMessage.
06:38Now there are about eight of these strings that you can use, and I'm picking
06:42the newMessage string.
06:43Look in the documentation to see the different keywords you'll use here.
06:47Other than that, the code is the same, so I'll switch back to the application
06:50and I'll say add a number badge, I'll switch back to this start screen with the Windows key.
06:58And there is my numeric badge, 42.
07:00For the final demo, I'll change to Symbol Badge, back to the start screen,
07:05it's the little email icon.
07:08So this concludes the Live Tile section.
07:10You can also create addition tiles for the start screen, these are known as
07:14secondary tiles and that's the topic for another video in this chapter.
Collapse this transcript
Adding secondary tiles to the Start screen
00:00Some of the Windows Store apps allow you to pin a new tile to the start screen.
00:04This People App is a good example.
00:06Here's my friend Albert.
00:08I'd like to know more about his update so I'm going to pin his information
00:12to the start screen.
00:13I'll right-click to bring up the app bar and I'll choose Pin to Start.
00:17I'm prompted to approve the pinning process.
00:20This is to prevent apps from adding tiles without my approval.
00:23I press the pin to start button then press the Window key on my keyboard to
00:28return the start menu and scroll to the end and there is Albert.
00:32Now watch what happens when I return back to this application.
00:35Go back to people and go back to my main screen, then I'm going to go to my
00:41start screen and click on this tile.
00:44The People App takes me to his specific information, not the main screen.
00:48Let me show you to do this with our application.
00:51I've already installed an app called Create a Secondary Tiles.
00:54I'm going to open this application and here are the different tours that we
00:58offer for California.
00:59I'd like to pin this Gold Country to the start menu, so I'll right-click to
01:03select it and I'll choose Pin to Start and again I'm prompted so I'll click Pin
01:08to Start and for the theme parks, I'll right -click to choose this one and Pin to Start.
01:12I'm going to change the text to For the Kids.
01:17Do you see what's happening in the background?
01:20It shows the image, shows the text and this was actually going to appear on my secondary tile.
01:25So I'll click on Pin to Start then the Window key to go back to my start screen
01:30and there's my For the Kids and Gold Country and watch what happens when I click on this.
01:35It takes me into the application, it shows me tour details for Gold Country and
01:41if I click on this tile, it takes me into my application and shows me tour
01:44details for the Theme Parks.
01:46Let's see how you accomplish this in code.
01:48I mean this project called Creating Secondary Tiles.
01:51I'm going to start by showing you the UI for the all tours.
01:55There's a grid view in here.
01:56You'll learn more about grid views later in this course.
01:59The key parts are in the code behind.
02:01I'll press F7 and switch the code behind and here's where I'm going to create my secondary tile.
02:08When the user clicks on the button and they have selected the tile, I'll grab
02:12that selected tile, verify that it's not equal to null.
02:16If it's not, then I'll create a secondary tile and these next five lines are required.
02:21You must provide a tile ID.
02:23This becomes your unique identifier for the tiles so when you come back into the
02:28app, you can use that to go to the correct UI.
02:31So I'm grabbing the Tour ID for the Tile ID, the short name and the display
02:36name, I'm getting that from my selected tours.
02:38I can also pass in any additional arguments I want.
02:41I can put them in a string and they're comma delimited information.
02:45This is custom information that's passed in when the user clicks on that tile.
02:49Then finally, I'm going to pick the image.
02:51I'm doing that with the selectedTour.TourImage.
02:54This contains a URL that maps to this picture.
02:57Next, I'll set a couple options, I want to show the name on the logo and I want
03:02to set the background for the text to dark.
03:05Now that I've got my tile ready to go, I just say RequestCreateAsync.
03:09This is what shows to Pin to Start user interface.
03:13You can also call tile update manager to create XML for a live tile for
03:17your secondary tile.
03:18Next, I'd like to show the code that determines which tour to show on the details page.
03:23Now this code jumps from file to file so it might be tricky to follow.
03:28This is about as simple demo as I could create.
03:30You are going to want to put a breakpoint or two in the code to follow along
03:33with what is happening.
03:34To help with debugging, Microsoft provides a special debugging option in Visual Studio.
03:39I'm going to double-click on Properties.
03:42I'm going to click on the Debug Tab on the left side and then check this Do
03:46Not Launch Check Box.
03:49By default, Visual Studio immediately starts the app when you start debugging.
03:53This is great in most circumstances but it's not ideal when debugging secondary
03:57tiles or when an app is started at a later time by another process.
04:02Choosing the Do Not Launch delays the launch of your application.
04:06When you start the debugger, the application will simply wait.
04:09When you launch the application from the secondary tile, it'll then jump into
04:13the debugger and you can hit your breakpoints to see the secondary tile
04:16arguments passed in and troubleshoot any issues that you may have.
04:19Let's give it a try.
04:20We'll open up App.xaml and then the C# version.
04:25We're going to look in the OnLaunched method, which has a breakpoint.
04:28Here, I'm passing in a list of arguments to the main page.
04:32That's coming from this LaunchActivatedEventArgs.
04:35Let me run the application, I'll step to the code and show you what's happening.
04:38I'm going to debug the app, start debugging or press F5 and across the bottom of
04:44the screen, you'll see an orange bar that says Ready.
04:47Now it's ready for me to go to the Start menu, press the Windows key, click
04:52on this For the Kids tile and that brings me into the app and starts
04:56debugging my application.
04:57Now what I'm getting from this argument here?
05:01The Kind is launched and I have a tile ID of three.
05:06Remember those custom arguments I set earlier in the Arg1, Arg2?
05:10Those are being passed in from that secondary tile.
05:13I'm going to step through my code by pressing F5.
05:16Now here, I am on the OnNavigatedTo method inside main page.
05:23So I get to startup apps, and I say, If (args.TileId ="App") then we're going to
05:31navigate off to the AllTours page.
05:34If it's something else, like my secondary tiles and I'm going to navigate off
05:38to my TourDetailPage.
05:39I'm going to press F5 to continue running and there's my details page.
05:44Of course the information from this is being retrieved from my data source.
05:47Let's look at that code.
05:49I'm going to switch over to Visual Studio TourDetailPage.
05:54Inside the OnNavigatedTo method, I'm attempting to parse this parameter,
05:58e.Parameter.ToString.
05:59Now it wasn't clear earlier where this information is coming from. Let's look.
06:03I'll go to the mainpage.xaml.cs and look what's happening here.
06:08When I navigate to the TourDetailPage, I'm passing the TileID in as an argument.
06:13So I'll switch back to this tour page.
06:15So I'm grabbing an argument, turning it into an integer then looking up the tours.
06:20I'm calling GetTours, I'm doing a where clause here to get the first item that
06:24matches this TourID and then I set my data binding information here.
06:29Data binding comes later in the course.
06:31So what we've seen is that the secondary tile is a great way to put a custom
06:35entry point into your app.
06:36You place this on the start screen and it makes your app customizable for
06:40the user.
Collapse this transcript
Sending messages via the toast API
00:00A Toast notification is a floating window that appears on top of any running app.
00:04A Toast can be shown immediately or scheduled for later appearance.
00:08I'm inside of project called Send Toast Notification.
00:11The main UI for this demo is inside BasicToast.xaml. It's a simple UI.
00:17It has four buttons and here's the code.
00:19I'll double-click on this first button.
00:21Here, I'm defining the XML that defines the toast that I want.
00:25So I'll start with the XML literal and specify a toast node, a visual node, the
00:30template that I'm interested in and since this template has two text areas, I
00:34specify the text for TextID 1 and TextID 2.
00:39And then load this XML literal into that document.
00:42Finally, I'm ready to show the toast.
00:43I'm going to use the Toast Notification class and the Toast Notification Manager class.
00:48I first create my toast passing in the XML document, then I go to the Manager
00:53Class and I say Create My Toast Notifier and show the toast.
00:56That's all you need to do.
00:59Before I show the live demo, let's look at the rest of the code.
01:02The other way to do that is to use to GetTemplateContent from the Toast
01:05Notification Manager and I tell it which template I'm interested in the
01:09ToastImageAndText02.
01:11I'll get back the RAxML and store that in this variable.
01:15Then I'll use the XML DOM to modify the text that's inside that template.
01:20So here I'm getting the text and image elements, I'm appending some text so the
01:24two different text nodes and then finally down here, by specifying an image
01:27element and setting the source attribute to that element to this file that's
01:32included inside my package.
01:34This grapes_small.png file.
01:37Then the rest of the code is the same, show the notification.
01:40By default, the toast is only shown for seven seconds or until the user
01:43dismisses it. And you can change that by changing the duration equal to long
01:48and specifying another time amount.
01:50Here, I'm just saying I want a longer time and I'm also specifying a different audio.
01:55I added an audio node here and I specify that I want the audio to loop, if
02:00possible, and I want to use a different audio file.
02:03There are only eight audio files you can use and they're listed in the
02:06Microsoft documentation.
02:08In this example, I'm using Call2 and then the second example down here I'm using Alarm2.
02:13Are you ready to see what these toasts look like?
02:16I'll press Ctrl+F5 to run the application.
02:19Now the toast will appear in the upper right-hand corner of the screen, they'll
02:24stay there for seven seconds or until I dismiss them.
02:27Here's the first test.
02:29That was the default sound and there it fades away.
02:37Here's the one using the image on the tile, I'm going to dismiss this on
02:41early by clicking on it.
02:42Another way you can dismiss it is by clicking on the X button up in the corner.
02:48Did you hear the different sound effect on that one?
02:50Here I change the sound.
02:55And here's the second example of changing the sound.
03:02The toast is the approved way to send alerts to your user.
03:05Used wisely, they can add value to your application.
03:08They are not the place to show advertisements for other services.
Collapse this transcript
12. Data Binding
Using data binding to work with data
00:00Binding frameworks occur in nearly every modern UI system.
00:04I've been using variations of them for nearly 15 years.
00:08If you're coming from another framework, you undoubtedly have some preconceived
00:11ideas about how a data binding system acts.
00:14Let me explain the XAML binding system to you.
00:17Notice I said binding, not data binding.
00:20You'll see why I make the distinction soon.
00:23At the most fundamental level, binding is simply a mechanism for updating one
00:28property from another, rather than having to write the code, to push the data
00:32from the source to the target, the binding framework does the job for you.
00:37The source for the binding can be a property on a .NET class.
00:41This is usually a business entity.
00:43Another common source is a UI Element within a XAML tree.
00:47For example, a property on a toggle switch can serve as a source.
00:51This is why I prefer to call it binding, because the source can be a non-business item.
00:57Also, you can use collections of data as a source.
01:00These are generally used with list control targets.
01:03The target of the binding where the data is loaded is a UI Element.
01:08There are some detailed rules regarding what constitutes a valid target.
01:12For example, the target property must be a Dependency Property.
01:16Review the MSDN docs for more information on Dependency Properties.
01:21Here is a sample of binding in a XAML file.
01:24On line 11, I have a TextBlock that is serving as the binding target.
01:29On line 12, I'm specifying the text property as the binding target property.
01:34At runtime, this text property will be set by the binding framework.
01:39The curly braces indicate that I'm using a mark-up extension. What is that?
01:43It's a class that is invoked at runtime to do some work in this case, to do the binding work.
01:49I'd choose my data source with the ElementName property within the binding and I
01:54indicate that I want the data to come from a text box named TextBox1.
01:59I also denote which property will supply the data, the text property.
02:04So I'm taking some text as the source and I'm applying it to the text in the target.
02:10On line 13 is the source of the data for the binding.
02:14A UI Element named TextBox1.
02:16It is also possible to bind to a non-UI Element.
02:19Line 15 and 16 show a TextBlock that is bound to a business class, the syntax is
02:25simplified for this slide.
02:27It's slightly more complex than shown here.
02:31Data can flow from the source into the target property.
02:34It can do this continuously every time there is an update in the source, the
02:38target gets the update.
02:39Alternatively, you can mark the binding as one time, which only updates the
02:44target once no matter how much the underlying data changes.
02:47There is also a two-way binding, which sends updates back to the source.
02:52The text box uses two-way by default.
02:56The binding framework does an admirable job of moving the data from any class
02:59into the target property, but it cannot keep a continuous flow of updates to the
03:04target unless the data source is written correctly.
03:06For example, if you create your own user control and you want a property on the
03:11control to be an updatable source, it must be written as a dependency property.
03:16When you create updatable business classes, you have to implement the
03:20INotifyPropertyChanged interface in the class.
03:23Sometimes, the source data is stored in the collection.
03:26Usually, this data is shown in the list control of some sort.
03:30In this example, in the code, I have a list of strings.
03:34At some point, I want to add another item to the collection, in this case the
03:38word Wednesday; the blue rectangle represents a list box that is bound to this data.
03:43What most developers would expect is that the list control changes to reflect
03:47the new item added to the source.
03:49In other words, in the blue rectangle in that items control, in that list box, I
03:54should see the new data show up.
03:56Once again, the binding framework is stymied and cannot provide updates to the
04:01target unless you use an appropriate collection class.
04:04One way to create a collection class that handles list updates is to write a
04:08class that implements the INotifyCollectionChanged interface, but the easiest
04:13and recommended way to handle it, is to use the ObservableCollection of T.
04:18This sample shows the trivial change I made to the code to work with
04:21ObservableCollection; see line 39.
04:26Sometimes the data is not in the correct format or shape desired for the binding target.
04:31In this example on the screen, I have some numeric data in my source.
04:36The numbers represent the number of people signed up to take a tour.
04:40I want to use a red brush to color the text if it made certain conditions.
04:44I can write a value converter to change the number into a red solid color brush.
04:50This converter is called whenever the data is updated to the target.
04:54By the way, converters are bidirectional so the converter could modify data as
04:58it heads back to the data source from the target.
05:02The binding framework is one of my favorite features in XAML-based apps.
05:06It does a great job of binding data and moving it efficiently from a source
05:09property to a target property.
05:12It's easy to write in XAML and Visual Studio and Expression Blend have great
05:16binding tools support.
Collapse this transcript
Element-to-element binding
00:00It's time to look at Element to Element Binding.
00:03This type of binding is between elements on the visual tree.
00:06The value from one element is synchronized with another element through
00:10the dependency system.
00:11For this demo, I'm using a project called ElementToElementBinding.
00:13The XAML I want to examine is here in the SetupBindings.xaml.
00:19I'll double-click on this file.
00:21Next, I'll click the Auto Hide button so that I have more room for
00:25the Properties window.
00:26At the top of this page are two text boxes.
00:29I want to bind the second text box to the first text box.
00:34Here is my second text box and it's name is TargetTB.
00:39I want to change this text property.
00:41Currently, it's static text.
00:43I'll start by typing in a curly brace and then choosing Binding from this list.
00:51Next, I want to pick the type of binding, I'm going to use an
00:55ElementName Binding.
00:57Now I have to provide the name of the other element.
01:00The name of my other element is SourceTB.
01:03Let me copy this name and then paste it in here.
01:06Now I selected the source element, I also have to tell it the property on the other element.
01:11That's done with the Path property, Path equal (=) and then the name of the
01:16property on the other text box is Text.
01:19As you can see in the designer, the text matches between the two text boxes.
01:24I will run the application to demonstrate it live.
01:27Ctrl+F5, then click on the Setup Bindings button, type some new text in the top
01:33text box and it matches in the bottom text box.
01:36Now if I go to the bottom text box and add some text, it's not moving back to
01:41the first text box because it's a one-way binding. Let's fix that.
01:45I'll return back to Visual Studio.
01:47Now the way you change this is by adding a mode property to this binding.
01:53You can type it in the XAML if you want, but there's another way to do it
01:57using the Property Grid.
01:59If you move over to the Property Grid, you'll see that there's a yellow
02:02rectangle around the Text property.
02:04That signifies that the information for this property is coming from a binding.
02:09If you click on this little rectangle, you'll see there are some yellow sections
02:13and some green sections.
02:15The yellow sections have to do with binding.
02:17Now I've already created the binding, let me edit it by clicking on this section.
02:22Here you can see I'm doing an ElementName from that Source Text Box (SourceTB)
02:26to the Text property.
02:28And then down here, I can choose More Settings and pick a TwoWay Binding and choose OK.
02:36Now it's modified my Binding.
02:38Let's see if this works. Press F5.
02:41I'll click on the Setup Bindings button again, go to my second text box, type in
02:46some new text then to activate the TwoWay Binding.
02:49I need to move the focus away from this text box.
02:52I'll press the Tab key, and the first textbox updates with the new text. Excellent!
02:57Here's another example of an Element to Element Binding.
03:00This TextBlock is bound to an element called smsTextBox.
03:06But look what I'm doing for the Path.
03:08I'm not using the Text property, I'm using a sub property on the Text property.
03:13The information in the Text property is of type string and the string class has
03:18a Length property so what I'm doing here is asking for the Text.Length property,
03:23that's the information I'm getting.
03:24So I'm going to get a numeric value.
03:27This way, I can tell you how many characters you're typing in the text box.
03:31Let's run the app and see this demo.
03:34Click the Setup Bindings, start typing in here and as I type, you see the number
03:39increase and decrease.
03:41For the final demo, I want to show Binding in Visual Studio and I want to change
03:46the size of this text icon base on the movement of this slider.
03:51We'll go from 30 to 80 in size and I want to increase this.
03:55How am I drawing this happy face?
03:57It's a trick you can do by using fonts.
04:00Here I am in Visual Studio.
04:02Let's click this item and see what it is. It's a TextBlock.
04:06Here's the key part, I'm using the font family Segoe UI Symbol.
04:10This is full of different symbols.
04:12In fact, a lot of the symbols that come from the standard Windows RT buttons
04:16come from this font family.
04:18So I specify a numeric value here, which happens to correspond to this smiley face.
04:23Now what I want to do is bind this font size to the slider.
04:27Let's use the Property Grid to do that.
04:29I'll move over to Properties, type in Font in the search dialog then click on
04:34this black rectangle to do the data binding, choose Create Data Binding, the
04:41Binding type is going to be an ElementName binding and then I have to look
04:46for this FontSizeslider, that's my source, so I'll click here and I'll choose
04:52the Value property.
04:53That's going to be a Double value and then I'll click OK.
04:59Are you ready to see if this works? I am.
05:06I really love the Data Binding in WinRT.
05:09Element to Element Binding is useful in many situations.
05:13Binding to business data is an important technique to master and that topic is
05:16covered elsewhere in this chapter.
Collapse this transcript
Binding a business class to an element
00:00In this project, I'll look at how to bind page elements to business data.
00:05For the project, I'm using BindingToBizData and I'm going to start by showing
00:08you my business classes.
00:10They're inside this file TourDataSource.cs.
00:13This represents our tour company.
00:16I have a class called Tour, which has information about our different tours
00:20including tour name and description, three boolean properties, one that lets us
00:25know whether there is a SeniorDiscountAvailable, one whether it's a KidFriendly
00:29tour and this last one tells us whether it's a MultiDay tour or not.
00:34This integer value represents the maximum number of people allowed per tour and
00:38then I have a string that represents the region.
00:41Last, I have the TourImage property, which represents the location of the
00:46image in my project.
00:47I've also got a TourDataSource, which has a GetTours method that returns an
00:53ObservableCollection of tours and the code in here is instantiating about 20 tours.
00:59You can see them listed down here.
01:00I've also got two properties, FirstTour and LastTour that retrieve one of the
01:06tours created, the FirstTour or the LastTour.
01:10I'm going to use these in one of my pages.
01:13Now I'm going to start with this BasicBinding and look in this code behind.
01:17Before I can use that Business class, I have to retrieve the information, I have
01:22to have an instance of the Tour class.
01:24I can do that in my code, perhaps here in my constructor or I can do it in my XAML.
01:30There has to be some way to instantiate the type in my XAML.
01:34In this first demo, I'll show you how to do it in the code-behind.
01:37So I'll move back here, I'll declare a variable and then I'm going to use
01:43that FirstTour property.
01:45That retrieves the instance of the FirstTour and stores it in this variable.
01:49Now I can assign this directly to a source on a control.
01:53But what's very common to do in XAML is to make this a data context.
01:58I'll set this at the page level at the page data context and then this FirstTour
02:02information becomes available to every single element on the entire page.
02:07I'll say this.DataContext = tour.
02:12Now it's ready to go.
02:13We'll switch over to the XAML to set up the bindings.
02:17Here's my first TextBlock.
02:22I'll set a binding.
02:23The source is already available because I set it for the data context and I said
02:27that it means every element on this page knows about that data context.
02:31I need to provide the property name that I want to bind to, TourName.
02:42Here, we'll do a Binding to Description and then for this toggle switch,
02:49I'll change this IsOn property to a Binding to the is the
02:55SeniorDiscountAvailable property.
02:57I'm going to copy and paste that, so let's go over here, find that property, so
03:02I don't have to type it.
03:03Switch back here and paste it in.
03:07Now let's run this application to see what it looks like.
03:12There's our first TourName, our first Tour Description, and the toggle switch
03:17has been turned on because there is a senior discount available.
03:19I'll switch back to Visual Studio.
03:22The next demo I want to show you is instantiating in the XAML, not in the code-behind.
03:27The benefit of doing this is that the Visual Studio tools can start showing the
03:31data in the designer.
03:33So I'll switch to this BindingWithXamlDataContext.
03:37The first thing I need to do is to bring my tour data classes into scope.
03:41I'll do that by going here to my page and adding an xml name space.
03:45I'll call this data and then I'm going use my DataSources namespace.
03:53Now if I use this prefix, I can work with the classes that are defined inside
03:57this DataSources namespace.
03:59Next, I'll go to the page level and add a DataContext, Page.DataContext.
04:09You see what happened in the top half of the screen?
04:12Now it says Whale Watching and Our eco-tours.
04:14So Visual Studio can now look in that DataSource and retrieve the information.
04:19You probably suspect that down here I have already set up a binding.
04:22Now I had to do something slightly different here.
04:25I had to say LastTour.TourName because that property -- remember that property
04:31on my type, that's called LastTour.
04:33So I'm binding to the LastTour property and then I'm going into that LastTour
04:36property and getting the TourName out of it.
04:39We're here, I'm getting the LastTour. Description and for this toggle, I'm going
04:44to delete this and show you how to use the Visual Studio Designer to get this.
04:48I'll take that out, move to my Property Grid, use the search to find IsOn, click
04:56here on the rectangle to turn on Data Binding.
05:00It already knows about my data context so I can say I have a FirstTour and a LastTour.
05:05So I'm going to drill down into my LastTour, find this SeniorDiscountAvailable
05:12and then click OK, and that wrote out the code for me.
05:15The toggle doesn't switch to on because the SeniorDiscount is set to false and the
05:20underlying data source.
05:21If I change this to FirstTour, then you see it switches to On.
05:26Let's review the key lessons in business binding.
05:29Any .NET class can serve as a source of a binding.
05:32Decide where to instantiate the class in the code-behind or in the XAML, then,
05:36set up the element binding to the desired business class properties.
05:40If you want to be able to see the data in the Visual Studio Designer, you have
05:43to set the sources in the XAML itself.
05:46Binding the business data is an important part of most apps.
05:49The Visual Studio tools and WinRT framework makes it easy to work with your data.
Collapse this transcript
Converting data with ValueConverters
00:01Here in the tour app, I have some pictures showing the different tour features.
00:05I show the senior picture if the tour is marked as having a senior discount and
00:09I show the kid's picture if the tour is kid friendly.
00:12Since the rest of the page is using binding, I want to bind the visibility of
00:16the image to the Boolean data in my data source.
00:19On this first tour, the Wine Country Tour, the Senior Discount is True, so I
00:23want to show this picture.
00:25But the Kid Friendly Boolean value is False, so I don't want to show this
00:29picture and it's the opposite for the Whale Watching Tour.
00:32I'll switch to Visual Studio.
00:34In order to get this page up and the page I'm looking at is called
00:37BoolToVisibilityPage.
00:39I'll select this image and then we'll look at this property here.
00:44For historic reasons the visibility property in UI Elements is not a Boolean
00:47property, instead, the values are visible and collapsed.
00:54This has frustrated XAML developers for years.
00:57When you have a Boolean value in your data source and you want to bind it to the
01:00Visibility property on the target, you have to use a converter.
01:03My converters, in this project, are in this Converters folder.
01:08I have two converters, the top two here in this folder that come from Microsoft.
01:13These are part of the Microsoft templates.
01:15I'll double-click on this BooleanToVisibilityConverter.cs file.
01:20As you can see, it's a class that derives from IValueConverter and that means
01:25you have to write a method called Convert and another method called ConvertBack.
01:30What Microsoft has done in this code is check the Boolean value when it comes in
01:34and either send back a Visible or Collapsed.
01:38So let's review that.
01:40The inbound value is a Boolean, the outbound value is either a Visible or a Collapsed.
01:46Let's use this in my code.
01:48I'll move back to my XAML, select this image, go to the Visibility property.
01:53I want to use a binding here and I also want to use that converter I just showed you.
01:59The easiest way to do this is to use the Property window.
02:02I'll move over here to the Property window, find the Visibility property. Here it is.
02:07Click on the black square and then choose Create Data Binding.
02:12I've already got my data context set on the page, so I will choose this
02:17FirstTour and make this a SeniorDiscountAvailable.
02:22You see that this is a Boolean value.
02:24Next, I'm going to get a converter and use it in the binding.
02:28To do that, I go down to the Converter dropdown and choose to Add a value converter.
02:33Visual Studio looks through my project and shows me all the types that
02:38implement IValueConverter.
02:40I'll choose BooleanToVisibilityConverter, click on OK and then click on OK again.
02:50Let's see what happened in my XAML.
02:52The Visibility property now has a binding to FirstTour.SeniorDiscount.
02:56It's also using a converter and it's doing a static resource look up.
03:01If you scroll up to the top of the page, you'll see a Page.Resources element.
03:06It contains the BooleanToVisibilityConverter with this key value.
03:11I'm using this key value in my binding.
03:13I'll do the same for the other three pictures.
03:17I'll select this kids image and set the Visibility property, use the Create Data
03:22Binding, the same tour, this time I'm going to choose the KidFriendly property
03:28and then when I come to this dropdown, you'll see that I've already added the
03:32converter so I don't need to add it again.
03:34I can just choose the existing one.
03:36Now I've set up all four of my value converters.
03:39You can see the designer is doing the right thing.
03:41It's showing the senior picture for the Wine Country Tour and the kid's picture
03:44for the Whale Watching Tour. And if I run the app, you'll see the same thing.
03:48For my next demo, I'll write a value converter.
03:51Here's what I want to do.
03:52I have this slider and I have this text block that is bound to the slider.
03:57So when I slide this slider, it changes the value that you see in this text block.
04:02What I want to show is instead of these raw numbers, I want to show hours,
04:07minutes and seconds.
04:10Back in Visual Studio, I'll add my own converter.
04:13I'll come to this Converter folder and I'll choose to add a class.
04:19The name of this class will be called HourConverter and I'm going to change my
04:27namespace to match the rest of my items.
04:32Demo.Converters, copy this, come back to this brand new type and I'll change the
04:39namespace and I'll come down to the class and I'll make it implement the
04:43IValueConverter interface.
04:47I'm getting a red squiggle, so I'll press Ctrl+.
04:51to resolve the naming issue.
04:53I'll choose this Using Windows.UI.XAML.Data and then next I'll implement the interface.
05:00Visual Studio has this convenient feature where I can click on the interface and
05:05you can see this little blue nub down here.
05:08I can hover my mouse over this and then choose to implement the interface and
05:13Visual Studio will write the Convert method and the ConvertBack method.
05:18Now I'll write some code in the Convert method.
05:23What this first line of code does is take the inbound value.
05:27This is going to be a double value that represents the number of seconds in one day.
05:31So I am going to take that, cast it to a double, pass it to the TimeSpan class
05:36and create a time span from those seconds.
05:39Then I can format the time span to show hours, minutes and seconds.
05:43I do that by saying return ts.ToString and then I'm going to use a formatter
05:50here in the parentheses. I'm ready.
05:53This is my formatter.
05:54This will be the hour portion, the minute portion and the seconds portion.
05:58Now I'm going to add this converter to my page.
06:07Here, go add, I'm going to set the text property to use a binding.
06:17I've already got a binding active so I'll just add a converter.
06:23I am going to run the application and when I slide this, I now get a nicely
06:30formatted 24-hour time span.
06:33Let me show you a couple of other converters.
06:38Here is an example of taking an initial value, in this case 40, and multiplying
06:43it so I get a higher number, multiplying by a factor of five.
06:49Here I got an example where I take the number and I turn it into a solid color brush.
06:55So it starts out at a yellow brush and when I get to certain threshold to 200, I
06:59switch to an orange brush.
07:01And when I get to another threshold, I switch to a red brush.
07:05So I'm taking a numeric value in and converting it to three separate solid color brushes.
07:11Here is the MultiplyConverter.
07:14What I'm doing in this example is I'm taking a parameter, the number five is being
07:18passed in as the multiplier when I use this MultiplyConverter, let me show you code.
07:25Here it is.
07:26So I'm doing a binding with the converter value of five and then I'm using this
07:30MultiplyConverter and this other one is quite a complex converter.
07:34When I created a converter, I added a number of properties to it.
07:39Check out this NumberToColorConverter class here, it has a LowValue, HighValue
07:45and three Brush properties and I can set those in my XAML.
07:49Let me show you what I mean.
07:51At the top of this page, I'm instantiating my NumberToColorConverter and then
07:58I'm specifying the Low, High values and the default brush to low brush and the high brush.
08:04Notice how over here in the Property Grid, I can set those brushes to different
08:08colors if I don't like the ones I have, very nice.
08:11And then when I run this, when the value goes over 1200, then it switches to the HighBrush.
08:18So what we've seen in this video is that if you have data in your data source
08:22that doesn't match the target type, you can convert it in the Data Binding itself.
Collapse this transcript
Change notification and observable collections
00:00The Binding Framework can automatically update bound controls in
00:04most circumstances.
00:05If you create your data sources using the correct interfaces or classes, the
00:09Binding Framework gets notified when your data changes, then it will update the
00:13data target automatically.
00:15For this demonstration, I'm using a project called Change Notification.
00:19I'll start by looking at these two business classes.
00:22TourUpdaterA is simple class that has two properties, a TourName and a SeatCount.
00:29To simulate updating data in the business domain, I have a DispatcherTimer in
00:33here that is firing every 800 milliseconds.
00:38I have set up an event handler to call this dispTime_Tick event and then
00:43I've started the timer.
00:45I created a random seat number and then every time the DispatcherTimer ticks, I
00:50update that seat number.
00:52This class does not notify the Binding Framework in the correct way, so I'll
00:57never get a notification that the data has changed.
01:00The correct way to implement this is to implement the INotifyPropertyChanged
01:03event, TourUpdaterB does that.
01:07Here's the class, here's the interface.
01:09Now in here I have to create a public event called PropertyChanged, which is a
01:16type PropertyChangedEventHandler.
01:18Then I'll raise that event whenever any of my data changes.
01:22To do that, I created a helper method called RaiseNotifyChanged.
01:26It takes a string, the property name that will be updating and then raises the event.
01:32So let's see how I changed my properties.
01:35Now when I create my TourName property, I write a getter to retrieve the value
01:40and during the setter, when somebody changes my TourName, I set the value in
01:45the local variable and then I RaiseNotifyChanged and specify the property name to change.
01:51Now the Binding Framework knows that I've changed the TourName.
01:54It will read the new value and update the bound control.
01:58I'm doing the same thing in my SeatCount and inside here I'm updating my
02:03SeatCount on the timer tick event, same as the other class.
02:06So let's see what these look like when we run. Press Ctrl+F5.
02:10This is the first class bound element.
02:16We got a value of 129 for the SeatCount.
02:18The timer is firing, but we're not seeing any updates.
02:22However down here, this bound element is seeing the updates every 800 milliseconds.
02:28I'll return to Visual Studio and show you the binding code.
02:33Sometimes you see this Inconsistent Line Endings in text files, I'm going to go
02:37ahead and click on Yes to this and then I'm going to press F7.
02:41Here's the binding code.
02:42I have two text boxes on the page, each one is bound to a separate instance of the TourClass.
02:48This one is bound to TourA and this one is bound to TourB.
02:53And then in the XAML, I specify a Binding to SeatCount.
03:00The other kind of binding notification you want is when you have a collection of information.
03:05I might have six items in the list.
03:07When I delete an item in the list, I'll want to update; the list control is
03:12bound to that to have one less item.
03:14The best way to work with that is to use the observable collection class.
03:18Let me show you the code for this page.
03:21I have two list boxes on this page;
03:24one is called priceListBox and one is called collectionListBox.
03:29One is going to use a list of T and one is going to use an observable collection of T.
03:33And then when you click on this button, what I'm doing is, I'll show you
03:39the code up here first.
03:40I have two variables, private members of my type.
03:44I have a priceList variable, which is a list of decimal values and I have a
03:48priceCollection, which is an ObservableCollection of Decimals.
03:52So they bough contain decimals, the only difference is the collection class
03:56that they're stored in.
03:57This is the one that knows how to notify the binding engine that something has changed.
04:02So what does my code do?
04:03In the constructor, I add three prices to the priceList and three prices to the
04:09collection and then I make the ItemsSources for these two list boxes, their
04:13different collection in the list classes, and then when you click on the button,
04:18I create a random price and then I add it to both lists.
04:23Now when I clicked in the Removebutton, I remove an item at a certain
04:27position in the list.
04:28Let's see what this application looks like.
04:31Press F5, click on Observable, you see the three items added, but now when I
04:38add a new item, it only showed up in the list control that is bound to the
04:42Observable collection.
04:43When I remove an item, it only removes it from the list control that is bound to
04:48the observable collection.
04:50To recap what we've seen, implement the INotifyPropertyChanged interface to
04:55provide notification from your business class to the binding engine and use the
04:58ObservableCollection of T to make an updatable list of data.
Collapse this transcript
What about MVVM?
00:00It's my experience that most UI frameworks end up having a favorite presentation
00:04pattern adopted by the community.
00:06There are many of these patterns to choose from.
00:08Here are few of the more popular ones.
00:11View, Model, Controller or MVC has been used one form or another for decades.
00:15It's extremely popular in the web programming community.
00:18Model, View, Presenter or MVP is a newer pattern that was popularized in
00:23the last fifteen years.
00:24Finally there is Model, View, View-Model, also known by the acronym MVVM.
00:30If you are a XAML developer, you've heard of MVVM.
00:33It is the favorite pattern in our ecosystem.
00:36It takes the presentation layer and breaks it into two parts, the View and the View-model.
00:40Usually the app uses data-binding to synchronize the data from the ViewModel to the View.
00:45This binding can be one way where the data travels from the ViewModel to the
00:49View controls only or it can be two way where data travels back to the
00:52ViewModel from the View.
00:54The ViewModel is responsible for communication with the model Classes in
00:57the Business Domain.
00:59The View Model exposes the data from the model to the View.
01:02Because it acts an intermediary, it can serve as a giant data converter,
01:06changing the model data before handing off to the View.
01:09The blue outline around the View and the View Model signifies that both these
01:12items are part of the Presentation layer.
01:15Anytime you create extra layers in an application-architecture, you have to
01:19justify the cost of the extra development and design effort. MVVM is popular.
01:24What benefits does it provide to the XAML developer?
01:27You'll hear a lot of opinions about this topic.
01:29For me, the number one benefit of the ViewModel is the improved testability of the app.
01:34Since the ViewModel is not a view, you can test it easily with standard unit test tools.
01:40There is no need to use fragile UI test runner tools.
01:43Here's a high level look at Views, View Models and the Binding Framework.
01:48The View Model exposes properties that represent the data available for the View.
01:52The View also contains the methods that do actions for the View.
01:55The View contains the UI elements and little code.
01:58The pattern leverages the binding engine.
02:01The View gets a data context to the View Model.
02:03Then the binding engine binds the View Elements to the View Model Properties and
02:07binds the action controls like the button to the methods in the View Model.
02:12I didn't use MVVM during the course even though I use it in real world projects. Why?
02:17I felt that adding the View Model to the projects would add an extra layer of
02:21complexity to the learning process.
02:22If you decide to use MVVM, there are two popular MVVM frameworks worth
02:26investigating, Caliburn and MVVM Light.
02:30I recommend that you research MVVM and consider using it for your project.
Collapse this transcript
Debugging your XAML bindings
00:00XAML binding is great.
00:01You can synchronize data between two objects in a declarative fashion, but
00:06declarative systems are notoriously hard to debug when things go wrong.
00:10It's hard to put a break point in an XML file.
00:13Microsoft has figured out how to break point Silverlight XAML, but that feature
00:17is not yet available in Visual Studio 2012 for WinRT applications.
00:21I'll show you a couple of techniques that can help you when you
00:24encounter binding issues.
00:25We'll start by looking at TourDataSource.
00:28This is the Tour class I'm going to bind to.
00:30I'm going to bind a TourName and Description.
00:33Like some of the other demos in this course, I'm using this GetTours method to
00:38load up a set of fictitious TourData.
00:41In this DebugThisPage.xaml.cs file, I'm getting the tours, grabbing the first
00:48tour and then taking that value and making it the DataContext of this page.
00:54Naturally, I'm going to do some data binding in the XAML.
00:59Here I'm binding this TextBlock to the TourName, the text property that is, and
01:04down here, I'm binding the TextBlock Text property to the Description.
01:08Now I'll run the application by pressing F5.
01:14And as you can see the two text blocks are filled with the data bound information.
01:18Now let's break this binding.
01:20I'll return to Visual Studio.
01:23I'll break this binding by erasing the last letter of the word Description.
01:27The information about the debugging is going to show up in this Output window,
01:31but it will only show up if I attach a debugger.
01:34So I'll press F5 again to run the application with a debugger attached.
01:40And as you can see now I'm not getting the Description.
01:43The good news is my application doesn't break.
01:46The bad news is I've got a problem that I need to investigate and find out why
01:50the binding is not working.
01:51I'll switch back to Visual Studio, stop the debugger and then I'll look in
01:56this output window.
01:57Microsoft automatically adds debug output to this window.
02:03There's a lot of information here, but the one I'm looking for is in this area,
02:07BindingExpression path error:
02:08'Descriptio' property not found on TourDataSource+Tour.
02:14So that's some of the information you can get.
02:17I'll show you another way to get it using an EventHandler.
02:20To see that I'll go to my App.xaml.cs file and I'll uncomment this line of code.
02:31This goes to the DebugSettings class and asks to listen for the BindingFailed event.
02:37So when the binding fails, this code will run.
02:40I'll show a message box, with the message of the failed binding.
02:44I'll Press F5 to run the application, and there's the broken description.
02:51As you can see, it's the same text that appeared in the Output window during
02:54the last demonstration.
02:56Obviously, I don't want to show this error message to the user.
02:59Because this event handler is attached to the DebugSetting class, it only
03:03appears when a debugger is attached. Let me show you.
03:06Close this, return back to Visual Studio and stop the debugger.
03:11Now I'll run the application by pressing Ctrl+F5, which runs the app without
03:17attaching the debugger.
03:18I still don't see my bound information, but I'm no longer seeing that message
03:22dialog on the screen.
03:23To sum up what we've seen, Visual Studio 2012 provides a dump of the binding
03:28failure in the Output window or in the BindingFailed event handler.
03:32It's a primitive way to debug bindings and it's not as nice as the Silverlight
03:36solution, but it's all we have in this release.
Collapse this transcript
13. Viewing Lists with List Controls
Choosing a control for listing data
00:00There are a handful of controls in the framework that work with a list of data.
00:04Each one is optimized for a particular scenario.
00:07The ItemsControl serves as the base class for all the controls except the Semantic Zoom.
00:12It takes a list of children elements and displays them in a repeated fashion.
00:15Data can come from an unbounded list of children or from a bound DataSource.
00:20The Selector base class is next in line.
00:23It adds the ability to select a child from the list.
00:26The ComboBox is a popular control because it doesn't occupy much space.
00:30It displays only the current selection in the selection box, but the rest of the
00:34list shown on demand in a fly out.
00:36The drop down can be opened and closed by tapping the control.
00:40The ListBox has been stable of UI design for decades.
00:43The ListBox in WinRT exist mostly for nostalgia purposes.
00:47You'll rarely use it in an app.
00:50It's been updated to support touch and other tidbits, but there are other
00:53controls better suited for modern touch interfaces.
00:57In WinRT XAML, we have two primary controls we can use when we need to display
01:01data and some sort of a list, the ListView and the GridView.
01:05Both of these controls are optimized for touch-based interaction and UI virtualization.
01:10The ListView is best for single column vertical lists.
01:14It supports multiple select and fancy data templates.
01:18The GridView is a good choice when you need a horizontal list.
01:21It also supports grouping of data.
01:24The FlipView is designed to show only one item at a time enabling the user to
01:28flip through items just like flipping pages in a book.
01:31Its items are automatically arranged with appropriate margins and snap points to make this work.
01:36It works equally well with touch or mouse.
01:39The Semantic Zoom control is really not an ItemsControl.
01:42It is a container that can hold two items.
01:45These items can be either the ListView or the GridView.
01:49It has two properties, zoomed in view and zoomed out view representing the
01:54two different views.
01:56The View Controls are some of the most useful controls in Windows 8.
01:59They're built for touch interaction and are extremely customizable.
02:03The rest of this chapter shows how to use them in your own apps.
Collapse this transcript
Understanding basic binding in list controls
00:00I'm in this BasicBinding project, I'm going to show you some of the list
00:05controls in this UnboundData.xaml file.
00:07And then later, I'll show you some binding in this BoundListView.
00:11Along the way I'll show you a few of the list controls.
00:14I'll start by opening this UnboundData.xaml file.
00:19In this first column, I have a ComboBox.
00:23A ComboBox contains a list of child elements.
00:26These children are rendering the ComboBox at runtime.
00:30This is considered UnboundData because the children are simply embedded in the XAML.
00:34Here are the children elements;
00:35I have a number of text blocks in there.
00:37Let me run the app and show you what this looks like, Ctrl+F5 and then I'll
00:43click on this dropdown arrow to see my five weekdays in this ComboBox.
00:48Return back to the Visual Studio.
00:50The ComboBox is a popular control because it doesn't occupy much space.
00:55It only displays the current selection in the selection box with the rest of the
00:59list shown on demand in that fly out.
01:01I'll add some more elements to this ListBox by double-click on this button.
01:05And here, I'm going to write one line of code, add an item to that ListBox.
01:12Inside the parenthesis, I'll instantiate a TextBlock and set its Text property.
01:19Now let's run the application to see what happens.
01:24I currently have five items.
01:26I click on the Add Day button, now I have six items.
01:29What do you think is going to happen if I click on that button again?
01:34It adds a seventh item.
01:35Return back to Visual Studio.
01:37Next I want to show you how you can preselect one of those ComboBox items.
01:42I'm going to do this in my XAML.
01:44I'm going to back to XAML view, find my ComboBox and look at this last item
01:51where I have this TextBlock down here.
01:54Each unbound item is automatically wrapped in a Helper class.
01:58In the ComboBox, that Helper class is called the ComboBoxItem element.
02:02I'm going to use that to preselect this Friday item.
02:08I'll take this ComboBoxItem and I'll wrap it around the TextBlock and then on
02:13this first element, I'll say IsSelected='true'.
02:19Now we'll run the application again and you see that my Friday element is preselected.
02:26Let's look at this next column.
02:29Any legitimate UI element can be used for the unbound row.
02:33Here I'm using a few rectangles and a ListBox to build a primitive bar chart.
02:38Return back to Visual Studio to show you this XAML. Here it is. I have a ListBox.
02:45Within the ListBox I've declared several rectangles with different widths with the same height.
02:49Now I can get fancier.
02:53Here's a ListView, which is the successor to the ListBox and inside that, I'm
02:59creating a number of stack panels.
03:01So this Stackpanel has a border and inside the border is an image and inside
03:06that is a TextBlock that shows the Tour 1 Descriptions.
03:09So as you can see, I'm repeating this complex UI four times.
03:15That is one of the major benefits of XAML based UI design.
03:19If you can think of a fancy UI for data, you can put that UI inside a list control.
03:24The problem with this unbound implementation is a factor of scale.
03:28If I had 202 words, I'd have to repeat these elements 200 times.
03:33Data binding is designed to solve this predicament.
03:36I'm only showing the very basics of binding, see the Binding chapter for more details.
03:42Before I switch over to the other page, let me grab this XAML here and copy
03:47it into my clipboard.
03:50Now switch over to this BoundListView.
03:54Inside this page, I have a ListView.
03:57The data from this ListView is no longer coming from any nested elements, it's
04:01coming from this ItemSource.
04:02Now I'm saying to use the Binding and it's going to look for something called
04:06the data context to find this source.
04:08I'll set that in the code behind in just a second.
04:11Down here, I have a ListViewItemTemplate, which currently has a single TextBlock
04:16that says the word Example.
04:18Over in my code behind, in my constructor, I'm using the TourDataSource and I'm
04:24calling the GetTours method on it.
04:26That returns a list of tours that I then assign to the data context, which then
04:31gets loaded into the ItemSource of the ListView.
04:35Here's the DataSource in the C# file.
04:39This is returning something called an ObservableCollection of Tour and as you
04:43can see I'm creating a tour and setting a number of properties on that and
04:47doing that many times.
04:49Let's run the application and see what we get.
04:52I'll click on this Bound Data button and here's my ListView.
04:56You can see I've got a number of items in there and they're all repeating that TextBlock.
05:02Now all I need to do to make this ListView look great is to make a better data template.
05:06I'll return back to Visual Studio, return to my XAML view and I'll replace this
05:13TextBlock with the XAML that I copied earlier.
05:16Now I'm going to change this Image Source to no longer use a hard-coded value.
05:23So I'll change this to use a Binding and I'll do the same thing for this TextBlock.
05:29I'll change the Text value to use a Binding and now let's run the application,
05:37Ctrl+F5, click on the Bound Data, very nice, I have a custom picture for each
05:45row and I have a custom string for each row.
05:48I can still select the items by clicking on them or touching them and unselect
05:53them by clicking on them again.
05:55Now my Binding is complete.
05:58The data is flowing into the page, being loaded into the ListView and a new UI
06:02created for each row based on my custom data template.
06:06Data binding is crucial to real world apps.
06:08It's the glue that holds your data and UI elements together.
06:12In another video, I'll show you more about the ListView control.
Collapse this transcript
Creating vertical lists with ListView
00:00Many apps show data in a single column.
00:03Here in the mail app for example, all the messages for this account are
00:06displayed in a ListView.
00:09Here in Visual Studio, I'm using a project called UsingListView.
00:12I'll start by looking at the XAML and code that's in basic ListView, double
00:17click to open the file.
00:19In this section of the document, I have a ListView.
00:22I'm going to use a data bound ListView.
00:26Remember this course has a chapter on Binding if you're not familiar with the concept.
00:31Where is my data source coming from?
00:33It's over here in this TourDataSource.cs file.
00:39I have a class called Tour, which I'm going to populate with Tour information for
00:43our company and I'm doing that in a method called GetTours.
00:49Now this is a static method that means I can call this method without having to
00:53instantiate the TourDataSource class first.
00:56Inside, I'm creating an ObservableCollection of tours and then populating it
01:01with sample tour information, including a TourImage, whether it's a KidFriendly
01:05tour and the TourName.
01:09Now in my BasicListView.xaml.cs file, I'm going to set the DataContext in the
01:17constructor to the GetTours.
01:19That means any control on this page has access to the data and you might
01:23remember in the XAML, I'm using that data in the ListView.
01:27Now down here, I have a ListView.ItemTemplate.
01:31This is what's applied to each piece of data as it's shown in the ListView.
01:35I'm applying a DataTemplate that consists of a Stackpanel, a border, a picture
01:42that's bound to my TourImage property, a TextBlock that's bound to my TourName.
01:49Let's see what this looks like.
01:51I'll choose to Debug on my local machine and then press Ctrl+F5 to run the application.
01:58As you can see, the ListView is filled with data from the DataSource and wrapped
02:03in my Custom Data template.
02:05To select an item, I click on it.
02:08I can select multiples items and to deselect an item, I click on it a second time.
02:14Now I'm going to switch to my surface and show you how this selection works
02:19with a touch device.
02:21You can simply touch the item to select and then touch again to deselect.
02:26You can select multiple items using this technique.
02:30You can also touch and hold and then slide to the left to select or deselect.
02:37Back in Visual Studio, let's look at how you enable multiple select.
02:42Here's the way you do it, Selectionmode=Multiple.
02:46One of my favorite features of ListView is the built in support for drag and
02:49drop ListView order.
02:50You can configure this option in XAML.
02:53For this demo, I'll change the options in my C# code.
02:55Let me open the code file and look at what I have here in the CheckBox_Checked
03:02and the CheckBox_Unchecked event handlers.
03:05I'll copy this code, bring it up here, say I want to be able to
03:12ReorderItems=true and AllowDrop=true and then we'll run the application again to
03:21see what this looks like.
03:23I'll press Ctrl+F5 to run on my local machine.
03:25I'll click on the Allow Reordering and now I can grab this item, pick up and
03:31move it to a new location.
03:33Let's try this Wine Country.
03:35I'll move it down a few items.
03:37It works with a very little effort on my part and has these lovely animations, too.
03:43Finally, I want to show you how to write code to select an item in the list and
03:47ensure that the list scrolls to the selected view.
03:50Return back to Visual Studio and go to the SelectLastbutton_Click and I'll write
03:57a little bit of code.
04:01What this code does is gets the current items count, subtracts one from it and
04:05then sets the SelectedIndex as a ListView to that item.
04:09I'll run the application and click on the button.
04:15The last item is selected, but you can't tell until you scroll down to the
04:19bottom of the list to see it.
04:21So next thing, I want to write some code to make sure that this item is
04:25visible in the ListView.
04:26Switch back to Visual Studio and I'll write a little bit of code and there you go.
04:33I choose to ScrollIntoView method and I tell it which item I wanted to ScrollIntoView.
04:38Run the application one more time and click on Select last item and the item is
04:46selected and the list auto scrolls to the item.
04:49The ListView is a great tool for vertical lists.
04:52For horizontal lists, you'll want to use the GridView, which is the topic of the next video.
Collapse this transcript
Creating horizontal lists with GridView
00:00If you are familiar with the ListView and you are already up to speed with the
00:05GridView because it's essentially a ListView with a layout engine that wraps
00:09this item into columns, any item that doesn't fit in the first column is
00:13automatically wrapped in the next column.
00:15The GridView has first rate system level animation built in.
00:19When you flick the list, you'll see smooth scrolling as well as a nice bounce
00:24when you reach the end of the list.
00:26I'm in the UseGridView project and I am going to start by looking at the code
00:31in BasicGridView.xaml.
00:34I have a GridView and it's using Binding to set the ItemSource.
00:38It's using the same DataSource as the ListView example in another movie.
00:43Review that movie to learn more about how the DataSource is configured.
00:46My DataTemplate has a Stackpanel and a Border, and the key thing I want you to
00:54note here is that the height of this border is 150 pixels.
00:57That means several of these borders will fit in the first column of the GridView.
01:03Other than that, it's basic Binding.
01:05I've got an image that's bound to my TourImage and a TextBlock that's bound to my TourName.
01:11Let's see what this looks like, I'll press Ctrl+F5 to run the application and
01:15here's my BasicGridView.
01:17I was able to get three items in the first column, then the fourth item was
01:21placed in the top of the second column.
01:24I can scroll back and forth to see all the items.
01:27To select an item with a mouse, you can just click on it once to select and then
01:32click a second time to deselect.
01:35Things are slightly different for selecting in the touch world.
01:38Let's look at the surface.
01:39When you use touch, selection is slightly different.
01:43You can simply touch the item to select and then touch again to deselect.
01:48Well you can touch and hold then slide towards the bottom to select then repeat
01:54the same action to deselect the item.
01:56When I try to drag to the right, while at the beginning of the list, I see a slow stretch.
02:02When I release, the list bounces back to the initial position.
02:06There's also a flick animation provided.
02:10Let's go back to Visual Studio.
02:12A slight change to the DataTemplate can make a nice looking single row
02:16version of the GridView.
02:18Let's open up this file SingleRowGridView and then look at the grid.
02:25The key to this demo is the height of the border.
02:28The height of the border is 430 pixels.
02:31That makes each item take up the full height of the GridView.
02:35Another change I made was to move the text to the bottom, see what this
02:39application looks like when it's running.
02:44This is an impressive colorful view with big text and big pictures.
02:49Windows 8 has many apps that use a similar GridView.
02:52Check out the Store App, SkyDrive or the Photo App.
02:57For the last demo, I'd like to look at the Grouped GridView.
02:59Let me show you what it looks like in the application, then I'll show you the code.
03:04This is a GridView with a group of items, all of the tours in the North area of
03:09California then I have a group of the Central area tours and then a group of
03:14Southern tours and so on.
03:17Let's switch to Visual Studio to see how this is done.
03:19I'll start by looking in my TourDataSource.cs file and scrolling to the bottom.
03:28I've created a Helper class called RegionGroup, which has a property called
03:33RegionName, which is a type string and it has a property called Tours which is of Ilist of Tour.
03:40So what I'm going to do at some point in my code is I'm going to carry the group
03:45and load the subset of items into this IList of Tour.
03:48To do the grouping, I'm going to use link and I'm going to use that in this
03:54GroupedGridView.xaml.cs file.
03:58Here in the constructor of the GroupedGridView, I'm getting the tour information
04:03by calling GetTours and then I'm using the link GroupBy method to specify that I
04:09want to group by Region.
04:10Remember Region means South, North, Central, words like that.
04:15And then I'm going to select those out into that type that I specified earlier,
04:19the Region Group and the key is going to be the string and this is going to be
04:24the individual tours.
04:25There might be three or four tours per region name.
04:28And then I'm going to set the DataContext on the page to region's list.
04:32Now it gets a bit complex here because I'm actually using two data sources and
04:36sending one data source here on the DataContext and then at the top of my page
04:43class, here I double-clicked on GroupedGridView.xaml to get here.
04:48I have this thing called the CollectionViewSource and the reason why I'm using
04:52that is I want to use a source that understands grouping, as I say up here in my
04:57notes CollectionViewSource is better than ObservableCollection of T. So I
05:00specify to use the data from the data context and then to drill into the Tour
05:05section, that sub section and say that the IsSourceGrouped="true".
05:10Now if I use this as a source for my data grid, you'll do the grouping much
05:14easier and you will see that's exactly what I'm doing here.
05:17I'm saying ItemSource=Binding and then I'm telling it to use this TourSource here.
05:24Then the rest of it is just configuring the group styles and DataTemplates
05:29to make it look nice.
05:30Here, I'm creating a HeaderTemplate with a TextBlock that uses the Region name.
05:36Here I'm telling it to use a different panel for laying out the items on the
05:40page, I am using something called the VariableSizedWrapGrid to do this.
05:45This helps arrange the Header elements correctly on the page.
05:50And then down here, I'm using the GridView.Itemspanel to specify a panel that
05:55ranges to children items in a horizontal fashion.
05:58Then finally, here's my regular DataTemplate where I've got my picture and my TextBlock.
06:05Do Ctrl+F5 and click on Grouped GridView.
06:10There's the Header and there's the individual items laid out in a horizontal
06:14pattern in one group and then another header and the next two items are laid out
06:19in a horizontal pattern.
06:21That concludes the GridView section.
06:23It does a very versatile control and one of the few controls that
06:26supports grouping of data.
06:27In the next video, you'll see how to flip through pages of data with
06:31the FlipView element.
Collapse this transcript
Flipping through lists with FlipView
00:01The FlipView is designed to show only one item at a time enabling the users to
00:05flip through the items just like flipping pages in a book.
00:08Its items are automatically arranged with the appropriate margins and snap
00:11points to make this work.
00:13I'm inside this project and I'm using the same TourDataSource I've used in other videos.
00:19This has the GetTours method that returns an ObservableCollection of Tour.
00:24And then in my constructor, I'm calling TourDataSource.GetTours and assigning
00:28that to the DataContext.
00:29That means all my elements on the page can use this source.
00:33In my XAML, I have this FlipView Control and its ItemSource is set to the Binding.
00:39This is the same ItemSource I used for the Grid View and the List View.
00:43And then of course I have to define a template and inside the template I have
00:47some Bound Controls.
00:49Here's a TextBlock that's bound to the TourName, here is a TextBlock that's
00:54getting the Region value.
00:56I'll Press Ctrl+F5 to run the application.
01:00Remember, the FlipView only shows one item at a time.
01:03Here's the first item from the data template.
01:06Notice how I have overlaid a rectangle on top of the picture, made it
01:10semi-transparent so you can see through that.
01:12That's a technique you see often in Windows Store Apps.
01:16If I hover my mouse over the FlipView, you'll see there's an arrow on the right
01:21side of the screen that I can click on to move to the next item.
01:24Now that I moved to the second item, you'll see that there's arrow on both
01:28sides, so I can flip back to the first item.
01:31On a touch device, you just need to swipe left or right with your fingers to
01:35move to the next page.
01:36It's easy to use and adds a unique touch base layout to your app interface.
Collapse this transcript
Switching views with Semantic Zoom
00:00There is a feature of WinRT called Semantic Zoom.
00:03It lets you look at your data in two views, Zoomed Out and Zoomed In.
00:09I'm in the Windows Store App.
00:11When you first open the Store, you are in the Zoomed-In mode.
00:15When I'm in the Zoomed-In mode, the app behaves in this normal fashion.
00:18I can scroll to the left and right.
00:21When I find an app I'm interested in,
00:23I can click on its item, which takes me to a detail view.
00:27I'll click this back to go back out to the Main View.
00:31When you touch the Minus button in the lower right-hand corner or use the pinch
00:36gesture on a touch device, it switches you to the Zoomed-Out View.
00:41Now again, I can scroll back and forth.
00:43But now I'm in the Navigation mode.
00:45When I find a section I'm interested in, let's say here, it doesn't open the
00:51PhotoFunia Application.
00:52It takes me to that section of the Main View. I'll zoom out.
00:59I'll scroll over here to where the Kindle is, click, and that takes me to that
01:03section of the main Grid View.
01:06Both these views are rendered with a Grid View.
01:09The purpose of the Zoomed-Out mode is for navigation.
01:13Notice how the images are smaller and there's less text to look at.
01:17Let me show you how to do that in your own app.
01:20I'll switch to Visual Studio.
01:21I'm going to open this File, BasicSemanticZoom.xaml, double-click here.
01:28What you see in my XAML is a grid with one child element, the SemanticZoom.
01:33Now the SemanticZoom is a container for other grid controls.
01:38It's going to work with a Zoomed-In View and a Zoomed-Out View, we'll look at it in a second.
01:44First, I want to point out that I have this IsZoomOutbuttonEnabled.
01:47That enables that little button with the minus sign on the bottom corner.
01:51Inside my SemanticZoom.ZoomedInView is a GridView with its own DataTemplate and
01:59some big images and borders, 240 pixels x 430 pixels.
02:05Inside the ZoomedOutView, I have another GridView.
02:11Look at the size of this border.
02:13Both the Width and Height are much smaller than the ZoomedInView.
02:18Both of these grids are using data bound data.
02:20Ctrl+F5, here's my main zoomed-in view.
02:27I can scroll back and forth.
02:30I can click on this Minus button to move to the zoomed-out view.
02:34See how much smaller my images are?
02:37When I click on one of these, it will take me back to the zoomed-in view.
02:42Using this system makes it easy to get Semantic Zoom working.
Collapse this transcript
14. Charms and Contracts
Searching your application with the Search charm
00:00Search is fully integrated in Windows 8.
00:03The file system in most Windows Store Apps exposed their data to the Search Charm.
00:08The Search Charm lives on the Charm Bar, which I can open at anytime by pressing
00:12the Windows+C or by swiping in from the right edge of a touch screen.
00:18The Search Charm bar is up here at the top.
00:21Let me click on it.
00:22This People App is an example of an app that uses the integrated search.
00:26When I type my friend's name in the search bar, like Mark, you'll see that the
00:31People App filters down the list to the ones that have the word Mark in it.
00:35Let me try another search and I'll search for Al, and this time I see there are
00:40two friends that match that.
00:41Every app that has opted into the integrated search is listed below the search box.
00:46So you can see People, Amazon, Bing and so on.
00:50I'm going to search for my book on Amazon by typing in HLSL and then clicking
00:55on the Amazon link.
00:56Notice that it switches from the People App to the Amazon App, passes my string
01:01of information into the Amazon App and then it finds it in its catalogue.
01:06Here's my book and I can read more about the book, here.
01:12If I decide I want to buy it, of course I can click here to add to my cart.
01:18Now I'm going to add search to my application.
01:20I am going to switch to Visual Studio and I'm going to start with this project
01:24called SearchCharm.V1.
01:27Currently, it is not search-enabled. I'll show you.
01:29I'll go to Package.appmanifest, go to Declarations and see that I have no
01:36declarations available here.
01:38Now let's run the application and see what happens when I try to search it.
01:42Press Ctrl+F5 to run the application.
01:44My app is on the screen.
01:46I'll do a Windows+C to get the Charm Bar up and I'll start searching this app.
01:53Now watch what happens when I hit the Enter key.
01:56My app does not have any search capability so the Charm Bar switches over to its
02:00default, which is searching through the apps that are installed on the computer.
02:04I'll shut down the app and return back to Visual Studio.
02:08Now what I'm going to do is I'm going to modify this declaration to add
02:12the Search Declaration.
02:13I'll click on this dropdown, find Search and then click on the Add button.
02:18Now I have declared to the Windows Operating System that I am a searchable
02:22application and that I'm agreeing to provide search information to their system.
02:27Now let's see what happens when I run the app this time.
02:30Ctrl+F5, Windows+C to bring up the Charm Bar, type in that same search string.
02:36Do you see what happened?
02:38My app stays on the screen and if you look down the list, that's my application.
02:43So it's showing that it is searching that application and my app is not showing
02:47anything interesting.
02:48So obviously, I need to write some code that returns something to the search
02:52engine or changes my UI in some fashion.
02:55That of course I'm going to in this one here, Explore CA - Search V2.
03:00So I'll shut down the application, return back to Visual Studio and I am going
03:05to switch to the SearchCharm2 and make it my startup project.
03:13I'll start by looking in this App.xaml.cs file.
03:18Now I've already enabled the Search Declaration so that's already set up.
03:21I am going to start by scrolling down here and uncommenting this line of code
03:27here where I'm going to echo the text.
03:29This code is just going to take the search string that comes in from the search
03:32engine and I'm going to echo it back to the screen.
03:35So I'm going to open this page called EchoBackPage and I'm going to pass and
03:39here's the key part, I'm getting this thing called the SearchActivatedEventArgs. Why?
03:43Because the code I'm writing is inside this method OnSearchActivated.
03:48This gets called whenever the search engine tries to run my application.
03:53So it's passing me in the query string here in the args and I'm passing that
03:57in to my EchoBackPage.
03:59So I'll go over here to my EchoBackPage, go to my code behind and in my
04:05OnNavigatedTo method, I just get that parameter that was passed in to me, cast
04:11it to a string and output it.
04:13Ctrl+F5, let's see what this looks like, bring up the SearchCharm.
04:19So far, I'm seeing the main UI.
04:21I'll click on search and then I'm going to type in Hello and press Enter.
04:28Now a couple of things happened.
04:29Again on the search bar on the right hand side of the screen, it highlighted
04:33Explorer CA - Search V2 and it switched to me over to my new page because of my
04:39code told it to go to this page and it echoed the text here on the screen.
04:43Obviously, if I keep typing and press Enter, it's going to echo the new text to the screen.
04:49Now what I should do with this text is run a query inside my application to
04:53find information to show the users.
04:54That's what's coming up next.
04:56Back to Visual Studio, back to my App.xaml.cs and I'll comment this code out and
05:04go down here and say, let's query the data now, by calling this
05:08ShowSearchResultsPaged.
05:11Let's go look at the code in this page ShowSearchResultsPaged and then again
05:17I'll press the F7 key to go to the code behind.
05:22On this OnNavigatedTo event I'm getting the EventArgs again and I'm doing a link query here.
05:31I'm getting all the tours and then I'm doing a where clause where I say if the
05:36tour name contains the parameter that's passed in, I'm doing some case
05:41manipulation here so that I'll do a case insensitive search.
05:44I like for the string that's coming from the search engine and I make that part
05:48of my where clause, then I select that one or two tours and then I build a list
05:54out of that and apply it to the DataContext to this page.
05:57So let's see what this one looks like, Ctrl+F5.
06:01It looks like I have a typo, so we're going to fix this.
06:08I somehow added an extra letter in there.
06:11Okay, it's fixed and I'll do a Ctrl+F5 to run the application, Windows+C to
06:17bring up the Charms, click Search and now I'm going to start typing for the
06:21word red, and this is my custom page and it's showing that there is Bike the
06:25Redwoods available.
06:26I'll type in gold and that shows there's a tour called Gold Country.
06:33I'll type in bike and I'll see that there are three possible tours.
06:38Now the nice thing is I'm taking this value when I click on this and I'm
06:42switching you over to the detail view of that.
06:47I'd like to make one more improvement.
06:49I can provide search suggestions back to the Search Charm.
06:53Then as the user types in the search, they'll see a list of available tours in my app.
06:58To make this work, I'll retrieve an instance of the Search pane class and handle
07:03its on suggestions requested event.
07:05First, I'm going to start by opening up App.xaml.cs and commenting out some code
07:12here, Ctrl+K, Ctrl+C and then down in this line Ctrl+K, Ctrl+U, this is going to
07:19open up this page called Search Suggestions page, which is over here.
07:23Let me open up the code behind.
07:29So we'll start by looking in this OnNavigatedTo.
07:31We've seen this a few times.
07:32I'm getting that parameter again and I'm starting this queryText and then I'll
07:36call this method called DoSearch, which I passed a queryText into.
07:40This requested the comment search I'm going to use for everything and it is down here.
07:46Again, you'll see there's a link query in here.
07:48I'm setting the DataContext.
07:53All right, and then I also have a QuerySubmitted event handler and I've wired up
07:58a handler up here for the search pane.
08:00So you see what I'm doing here is I'm getting the search pane from the operating
08:04system for the current view so that's for my view and then I'm setting up an
08:11event handler for when the query is submitted and I'm also setting up an event
08:15handler for when the search pane requests suggestions from my app.
08:20So in the code here QuerySubmitted, I'm again calling a DoSearch and on this
08:26one, the SuggestionsRequested, that's down here.
08:31I'm returning a list of words to the Search API.
08:34So what I'm doing is I'm getting the tours.
08:37Again, I'm looking for the string that was passed in and then I'm building a
08:42collection for this argument here, the SearchpaneSuggestionsRequestedEventArgs.
08:49I'm saying let's add a new collection to that.
08:52I'm going to append the items that I just found for my query.
08:56I'm going to turn this into a list and send it.
08:58So if I put three items in here, there will be three items that show up at the
09:01bottom of the Search pane.
09:03If I send six items back here, there'll be six items that show up.
09:06Let's see what this one looks like.
09:09Ctrl+F5, again, I'll do Windows+C to bring up the Charms and I'll type in
09:15red like I did before and you can see it's suggesting over here the Bike the Redwoods.
09:22Now it takes a few seconds for it to start querying my application for the
09:26list of other words. Let me try it now.
09:29I'm going to type in the word bike.
09:31It hasn't asked for the information yet. There it goes.
09:37It took about 20 seconds.
09:40Now you see at the bottom right below the search text box are five items;
09:45bike, Bike the Redwoods, Bike the Sierras and so on.
09:49And I can click on this to submit that query, that puts it in the text box and
09:54then of course that resubmits the query back to my application.
09:58The Search Charm is the designated place in Windows 8 to search for any information.
10:02It works across all registered searchable apps including yours.
Collapse this transcript
Sharing app data with the Share charm
00:00I love to share interesting discoveries with my friends.
00:04Windows 8 provides the Share Charm for that purpose.
00:07I'm inside the Windows Store app.
00:09Here's a game called Wordament that I love to play.
00:13It's like playing Boggle with 2000 friends. Here it is.
00:18I'm going to click on the Wordament game and then I'm going to share it with my friend, Ben.
00:22I'm going to do that by pressing the Windows+C key and then clicking on Share.
00:28Now what has happened is the Windows Store has sent some information to the
00:32Share Charm about what it has to share and the Share Charm has looked up the
00:36applications that are willing to accept that information, in this case, the Mail
00:39app and the People app.
00:41So I'll click on the Mail app and you'll see that in the bottom of the page,
00:46the information is being passed over from the Windows app itself.
00:50So the picture of the Wordament game and a link to download it is sent in the
00:54body of the email and of course I come up here and choose my friend.
00:58I click the Send button and he gets the chance to play the game too.
01:03Now let me tell you about how this works.
01:05There are two sides to the sharing process:
01:07the share target, and the share source.
01:10Some apps, like the Mail program, are a share source.
01:15They have a share contract in their app manifest.
01:18When they set up the contract, they specify the file types that they accept.
01:23It's not a trivial job to become a share target and most apps will not need to do this.
01:28On the other hand, most apps like this Store app, will benefit from being a share source.
01:33Being a share source is easy.
01:35Let me show you how to do it.
01:37I'll switch to Visual Studio, this project called ShareCharm and then I'm going
01:42to open this AllTours.xaml.cs file.
01:48Here are the relevant bits of code.
01:51The DataTransferManager is used to send information to the ShareCharm.
01:56Here, I'm getting a copy of it and then I'm wiring up an event handler for
02:00the data requested.
02:02This method is called whenever the ShareCharm wants my information.
02:06So what I'm doing is getting a copy of the tour that was selected on the page
02:11and then setting a couple of properties on the e.Request.Data.Properties.
02:17I'm seeing a Title and a Description here from the information from my tour class.
02:21Of course, this information is coming from this DataRequestedEventArgs.
02:26Now I can also send other information.
02:28That's what I'm doing here.
02:30I am setting up a Uri and saying, I have some information to get from my Uri.
02:35Let's see what this looks like when I run the app.
02:38Press Ctrl+F5, choose an item, Gold Country, press the Windows+C combination to
02:47get the Charms Bar up and then click on Share.
02:50Then go to the Mail app.
02:52My information has passed in.
02:55It says Gold Country at the top and then the app went out and read some
02:59information from that Uri and placed it on the bottom of the Mail app.
03:03Let me show you another example.
03:05I'll return back to Visual Studio and I'll comment out this line and uncomment
03:12the following two lines.
03:14Here, I'm setting up some HTML for that message.
03:18I'll turn on the app again.
03:23I'll choose Wine Country this time.
03:26Open the Charms Bar and the Share Charm.
03:30Click on Mail and this time, my custom HTML package is added to the email address.
03:39Of course again, it says Wine Country, which is the information that was sent
03:42over from my application.
03:44Sharing data with other apps is a very natural process in Windows 8.
03:48Determine the interesting parts of your application and start sharing
03:52them today.
Collapse this transcript
Changing app settings with the Settings charm
00:00The Settings Charm is the place to go in Windows 8 to change
00:04configuration options in an app.
00:05Let me show you how this works in the Windows Calendar app.
00:09I'm inside my calendar and I'll press Windows+C to open the Charm and then click on Settings.
00:16At the top of the list are a number of links that represents all these settings
00:21that I can change inside these applications.
00:23These are implemented as something called a Command or a UICommand.
00:26We'll look at that in a second.
00:28I'm going to click on this one called Options and then I'm going to look at my
00:32three colors and decide that I don't like this blue color.
00:35So I'm going to switch over to a red color.
00:38Now keep an eye on the calendar.
00:39As soon as I click on this item, the calendar updates.
00:43There is no more, are you sure, or OK or Cancel or Apply buttons.
00:49It's an instantaneous change.
00:51Now let me show you how to implement this in your app.
00:54I'm inside the SettingsCharm project and I've opened this FirstPage.xaml.
00:59This is the first UI you're going to see that's loaded inside MainPage.
01:02Inside the XAML on line 13 and 14, I'm instantiating an instance of a control
01:07that I wrote, RedSettingsControl and GreenSettingsControl.
01:11Now these are coming from the local library, which is defined up here in this namespace.
01:16What this does is bring any types that I have declared inside this namespace
01:19into scope so that I can use them inside my XAML.
01:22So I'm instantiating these two controls and giving them some names.
01:27I'll press F7 to look the code behind.
01:30Here in my Loaded event, I'm getting the Settingspane so when the charm is up,
01:35the Settingspane is going to get the current view and I'm going to wire up this
01:39handler for CommandsRequested.
01:40The SettingsCharm is going to come to my app and say, do you have any commands
01:44for me to show in the UI?
01:46I do, and I'm giving them back in this OnCommandsRequested.
01:50Now I'm doing that by adding an ApplicationCommands.Add, creating something
01:55called a SettingsCommand and then this first item is an index.
02:01The second item is the string, Tour Preference that I want to show on the
02:06command and this third item OnRedCommand is the method that gets called when the
02:12user clicks on that Command button.
02:13Let's see what's down there.
02:15It's calling a method called .Show and it's passing in the UICommand into that method.
02:21You can also see I've got a GreenCommand that's doing exactly the same thing
02:24for the green control.
02:25Now let me run the application and show you what it looks like and then we'll
02:30come back and look at those user controls, Ctrl+F5 to run the application.
02:34Here's that blank first page.
02:36Now there are two controls on here, but you can't see them yet.
02:39They haven't been shown.
02:40Now I'm going to do a Windows+C to bring up the Charms, go to Settings and at
02:45the top of this list are my two setting commands with my text and watch what
02:50happens when I click on these items, so I'm going to click on this Red one and
02:53my custom control shows up.
02:55Now of course, I can click on this and drag this item to a new location.
02:59That's changing the settings and then I can click on this Back button, which will
03:03take me back to the Settings pane.
03:05Now I'll click on the Group Tours (green) and see that user control.
03:12Let's go look at some of the UI now.
03:13I'll return back to Visual Studio and we'll go over here and look in this
03:19SettingsControls folder and look at RedSettingsControl.
03:24As you can see, it's just a user control that contains a pop-up and within the
03:31pop-up is a number of UI elements, including down here at the bottom, a couple of
03:36Toggle Switches and I've also got a button here that is the Back button and I
03:42made it look like the back arrow.
03:44Now I put this inside a pop-up so that I can float it over top of the rest of
03:48the UI and I can also animate it into view.
03:51I have also done the same thing in the GreenSettingsControl.
03:55Again it's the UserControl, it has a pop-up and everything else I just showed
03:59you with some different controls.
04:00Now let's look at the code behind for the RedSettingsControl.
04:04There's a lot of code in here, but the key parts are this Show method that was
04:10called by the first page.
04:12I'm passing in that command, remember?
04:14What I'm doing is setting the content of this control to this pop-up and
04:18then showing the pop up.
04:20And then of course, that slides into view.
04:23When the user clicks on the Back button, I'm going to the Settings pane and I'm
04:28saying, would you re-show the SettingsCharm?
04:30Let's run the app again now that we've seen some of these UIs and see what it looks like.
04:35Ctrl+F5, bring up the Charm, go to Settings and now I'll click on this, you'll
04:41see how I animate that item in from the side and when you click on this button,
04:45it's going to take you back to the Settings pane.
04:47Now there are a couple of things to talk about here.
04:50For one, I haven't saved the settings, so you should persist these settings at
04:54some point in the file system so you can read them back later.
04:57The second thing is a lot of the applications out there will actually load the
05:01settings into this area.
05:02That's how the Calendar control worked.
05:04But there's no built-in support for that inside a WinRT application, but
05:09there is a third party library called Callisto out there, which you can
05:14download for free and run and it does have the ability to put something
05:18within the Settings panel itself.
Collapse this transcript
15. Working with Sensors
Working with sensors and devices
00:00Windows 8 and WinRT support a broad range of sensors.
00:04To work with these instruments in your code, you must declare the required
00:07capability in the application manifest.
00:10Otherwise, your application will throw an exception upon access.
00:14If you choose to use a sensor, remember that the consumer may choose not
00:18to install your app.
00:19Also, once the app is installed, you must check to see if the sensor is available.
00:25That's because the user might choose to disable the sensor.
00:28Perhaps, they are trying to save their tablet battery and have turned off the GPS.
00:33You need to check for another reason.
00:35The average desktop computer has a few sensors on hand.
00:39The framework contains a rich set of sensor classes.
00:42Here is the complete list.
00:46The framework also works with devices that are sensor-like.
00:48For example, the webcam and microphone are a couple of devices that are
00:53commonly accessed in apps.
00:55In the next few minutes, I'll discuss a few of the more interesting sensors.
00:59The accelerometer measures the acceleration of the device on three axis' X, Y
01:06and Z. the compass measures the earth magnetic field.
01:08It returns the position of true north and magnetic north in degrees.
01:15The inclinometer class provides information in the form of Yaw, Pitch and
01:20Roll angles, which are common terms used to describe forces affecting objects
01:24moving in 3D space.
01:27The WinRT API has several orientation layers.
01:32I discussed the orientation display properties in another chapter.
01:36Here, I'm talking about a different set of orientation sensors.
01:40The SimpleOrientationSensor uses the accelerometer and other sensors to return a
01:45simple value representing the current orientation.
01:48It has Not Rotated, Face Up, Face Down and several angle values.
01:55If you want your orientation data provided in a rich set of data wrapper
01:59classes, use the OrientationSensor class.
02:02It gives you the information back in the SensorQuaternion and the
02:06SensorRotationMatrix.
02:09Finally, we have these three sensors.
02:11The LightSensor gets a value for the ambient light surrounding the device.
02:15Use it to brighten or dim your app display.
02:17The Location API combines the information from all the location sensors
02:21available on a device.
02:23You get latitude, longitude and altitude values.
02:27If the device has a GPS, you get very accurate values.
02:31If not, it uses the other location items to determine the value.
02:35You just get a less precise result.
02:37The Proximity sensor creates a communication channel with nearby devices or items.
02:42Near Field Communication or NFC is very promising.
02:47It can read information from an NFC chip.
02:49You have to be very close within a few centimeters to pick up the signal.
02:54So there you have it, a summary of the Sensor API available on the WinRT Framework.
02:59Check out the rest of that chapter for details.
Collapse this transcript
Moving elements with the accelerometer
00:00This tilt and bounce demo inside Visual Studio is a nice example of a bouncing ball app.
00:07The direction of the ball is influenced by the X, Y values retrieved from the accelerometer.
00:12Remember, the accelerometer detects movements in the X, Y, and Z planes.
00:18This sample is taken with permission from Charles Petzold's Excellent
00:21Programming Windows 6th Edition Book.
00:24Let's look at the UI in the MainPage.
00:29Let me resize this so it's a little bit easier to see. I'll choose Fits All.
00:33It's a black canvass and in the center, it has a few text blocks inside a blue stack panel.
00:38I'm going to show the X, Y, and Z accelerator values inside this blue area.
00:43The black area is where the ball is going to move around and bounce off, the
00:47edges of the screen.
00:48The code is over here in MainPage.xaml.cs.
00:54Here's the first thing we want to look at, line 40.
00:56This is how you get the accelerometer on the computer.
01:00I'm holding the accelerometer in this variable.
01:03Next, on the loaded event, I'm going to check to see if I got an accelerometer
01:08and if not, we're going to say that one is not available.
01:12Otherwise, we're going to set the CompositionTarget.Rendering event.
01:17What this does is fire the event every time there's a frame update on the computer.
01:22So this is how we're going to animate the ball around on the screen.
01:26Another way we could have accomplished this is by listening for the
01:29accelerometer ReadingChanged.
01:32Every time a reading changes, this event fires.
01:37Here's the code for OnCompositionTargetRendering.
01:40The first thing we're going to do is ask the accelerometer for our current reading.
01:44It will go query the accelerometer and I'll get back a piece of information
01:48stored in this variable.
01:49Then the next part that's interesting is we're going to go to that reading
01:54and get the acceleration in the X, Y and Z planes and store them in these three variables.
02:01Then this is a part I'm going to output to the screen, three separate messages
02:06explaining what those values are.
02:09The rest of the code is interesting, but I don't have time to explain it.
02:12This is the code that bounces the ball off the wall and positions it on the screen.
02:17I'll switch to the surface to see this application in action.
02:22The Tilt and Bounce app is open on the surface.
02:25I'm holding the surface horizontal to the floor.
02:28As I tilt the tablet left, right and other directions, the ball moves around the
02:33screen and bounces nicely off the screen edges.
02:37In the center of the screen are the acceleration values.
02:41The top value is the X acceleration, the middle one is the Y value and the
02:45bottom is the Z value.
02:47As I tilt to the left, the X value is negative.
02:51Tilt to the right and it becomes positive.
02:54The same thing happens when I tilt forward and back for the Y values.
03:00It's harder to see the Z values change on the camera.
03:03I have to tilt the screen to point towards the floor to get negative Z values.
03:07With just a little work, this app could be turned into a fun, casual game.
03:11See what you can do with the code.
Collapse this transcript
Exploring Bing Map and the Location and Compass APIs
00:00I don't know if you've considered using the Bing Map service in your apps.
00:04If not, you should.
00:05Your Mapping software has seen considerable improvement in the last five years.
00:10The Bing Map control makes it a snap to add a map to any page.
00:14Before you can work with the Map control, you need to install the Bing Map SDK.
00:19That's easy to do with the Visual Studio Extension tool.
00:22You'll also need a Bing Map developer account.
00:25Sign up for a free account at bingmapportal.com then get yourself a
00:30credential key for your app.
00:32Without this key the map control won't work.
00:35A word of caution, if you use Bing Map for commercial purposes, you will have to
00:40pay a licensing fee.
00:43In Visual Studio, I've opened a solution called, UsingBingMap that has three projects.
00:48The first project using Bing Map is where I'll demonstrate how to add a Bing Map control.
00:53The second project shows the finished version of that.
00:56The third one is an example I'm planning on running at a surface tablet.
00:59Let's start by opening this using Bing Map and opening this MainPage.xaml.
01:04I want to be able to add a Bing Map control to this designer.
01:08To do that, I need to add the SDK to Visual Studio.
01:11I'll choose tools>Extensions and Updates then I'll go to the Online section.
01:19In the Search box, I'll type in bing.
01:24Here's the item I'm looking for, Bing Maps SDK for Windows Store apps.
01:29The green check mark signifies I've already installed it on my Visual Studio copy.
01:34You'll see a Install or Download button, which you'll click.
01:37Wait for about a minute then, install and restart Visual Studio.
01:42I've already got it installed here.
01:45I'll close this dialog.
01:47Before I can continue, I need to add the location capabilities to my app.
01:52I can do that by opening Package.appxmanifest and choosing
01:57Capabilities, Location.
02:03Also I need to set the processor type before I'm running the app.
02:06This is a limitation of the C++ library referenced by the Bing SDK.
02:10To do that, I would open up Properties then, choose Build.
02:16Since I plan on using the simulator for the first demo, I'll set the
02:19Platform target to x86.
02:22Now I should be able to compile the application, Ctrl+Shift+B. Next I'll go back
02:27to MainPage and add my map control to this designer surface.
02:32I can do that by going to my toolbox, finding the Map control and dragging it on
02:37the designer surface.
02:40You'll get this error message, click OK and then close and restart the designer
02:46MainPage.xaml and then, try that again.
02:49Drag the map over and at this point we have our map.
02:55I need to make some changes to it.
02:57Rather than type all the code;
02:58I'll go over to this finished application and show you the results.
03:03I'll close these designers up here and then I'll open up UsingBingMapsFinished
03:12and open up MainPage.xaml.
03:14Let me make this a little bit taller and let's look at the XAML. Here is my Maps.
03:19I've set a few items down here.
03:21I've given my Map a name, set the Horizontal and VerticalAlignment to Stretch so
03:26it fills up the inside of the page.
03:28Set to ZoomLevel of the Map to 8, the MapType to Road and in this Credentials
03:34section I've added my own credential key.
03:37Remember, you need to get your own credential key.
03:39You can't have mine.
03:42Next let's look at the code behind.
03:44I need to get the location from the device and then use that to center the Map.
03:49Here's the code, in the MainPage constructor, I'm setting up a loaded event.
03:54The reason I need to do that is I need to make an async method.
04:00See how I have the async keyword here?
04:02And you can't use the async keyword on the constructor.
04:04So I created this MainPage Loaded event.
04:07And in here, I'm going to the device and getting the Geolocator.
04:12Then I'm getting the current position of the device and storing it in this variable.
04:17Then I'm telling the Bing Maps to use that as the Center position.
04:20So I use the geoPosition and I say, get the current coordinates, latitude
04:25and longitude value.
04:27Remember you can also get the altitude from your GeoLocator.
04:31This is a one time event.
04:32It's only going to fire when the page loads.
04:35If you want to keep updating the map, you would have to look at
04:38PositionChanged event to do that.
04:41I'm ready to try this on the simulator.
04:43So I choose the simulator from the dropdown, and then press Contro+F5 to
04:48run the application.
04:49There's my Bing Map, you may see a permission dialog pop-up that ask permission
04:54to use the location services.
04:56Now I can drag with my mouse and move to new positions.
05:01I can Zoom In by double-clicking.
05:04And I can also set a New Location with this Set location item.
05:09Let me show you how that one works. I'll click here.
05:12Currently, it's using my real location.
05:15I can simulate any other location by checking this check box and then, typing
05:19in my own values here.
05:21Let's set this to 46 for the Latitude.
05:26Set the Location and close this.
05:27Now you're not going to see any changes because I don't have an event listing
05:31for that, but I can solve that by shutting this down and restarting the app.
05:38Now you see I'm in a different location.
05:42To make this app even better, I'd like to add a compass, that's what this app
05:46does down here, WithCompass.
05:47I'll make this my start-up and show you the code I have in here.
05:52I decided to make a custom CompassControl, here it is.
05:56It has a black, circular background, and a yellow hand that's going to serve
06:02as the compass needle.
06:03It's based on the gauge control I showed you in the custom controls video.
06:08It has some custom code here.
06:12And what I did is I set-up a Loaded event here, too, and I set-up the
06:18CompassControl_Loaded event.
06:20And here I'm getting one of the other sensors on this computer.
06:23I'm working with the Compass, so I say get the default compass and store it
06:26here, and I tell it to update me whenever the reading change.
06:29So down here on the OnCompassReadingChanged, I set the Degree of my control to
06:38the arguments I'm getting from the Reading.
06:39So this is the information coming from the Compass, I get the Reading and I ask
06:44for the HeadingMagneticNorth and then I need to rotate that 180 degrees to make
06:49my compass control work correctly.
06:51That's all we have to show you here, the rest of the demo I'll show you on
06:56the surface tablet.
06:58The app is loaded on to the surface.
07:00The Map control is on the right side and it works the same as it did on the simulator.
07:05The compass is also working.
07:07I'll hold the surface horizontal to the ground and pivot around the center. Excellent!
07:12The compass is moving like it should.
07:15The combination of location services, Compass API and the Bing Map control
07:19provide a nice way to add interactive mapping to your application.
Collapse this transcript
Capturing still images and video
00:00If you've ever created an application that captures media from the computer's
00:04attached devices, you know that it can be a tricky proposition.
00:08When our team makes some big improvements in the media API's it provides a
00:12straightforward way to capture audio, video, and images in your application.
00:17I'm inside a project called CapturingMedia.
00:20The first thing you need to do to work with media is to set the capabilities in your app.
00:25To do that, I'm going to work with my Package.appxmanifest file, double-click on
00:30that file, go to Capabilities.
00:33I check the webcam capability that provides the video and picture features.
00:39To get audio, you have to add the Microphone capability.
00:43Be warned that if you're capturing a live video from the webcam, you'll also
00:48need the Microphone capability.
00:49The easiest way to get a picture into your app is to use the camera capture UI class.
00:55It captures a single photo or video from an attached camera and provides all the
01:00captured UI for your application.
01:01Let me show you the XAML for this first example.
01:05I'll open up CameraUiPage.
01:09This is a simple page, it has an image control and it has a button to
01:15activate the Get Image code.
01:17In my code behind, I'll press F7 to switch to my code behind.
01:22I'm calling from the button, this method that I wrote called CapturePic and I'm
01:27going to use that class image in a few seconds at the CameraCaptureUI class.
01:32Once I have the CameraCaptureUI, I'm going to attempt to get the picture,
01:37CaptureFileAsync and I tell it what kind of picture I want from the webcam, in
01:42this case a photo, and I store that in this file.
01:45Then I create a stream from that file.
01:49I'm going to use that stream to create a BitmapImage.
01:51I'm doing that on these two lines of code here.
01:54I'm setting the source of the stream and then applying the BitmapImage to my image source.
01:58I've got a catch block down here;
02:00you cannot capture a picture if the app is in snap state.
02:03So I'm catching that down here and ignoring it.
02:05Capturing video is just as easy.
02:09Here's a VideoUiPage, the difference is on the UI is that I'm using a
02:14MediaElement instead of an ImageControl.
02:18And I'm also setting up a handle here for MediaOpened.
02:22This event fires when the media stream is fully loaded into the player.
02:26This isn't important for many scenarios.
02:28Imagine you're streaming a file from the web.
02:30You don't want to start showing the video until the framework signals that the file is ready.
02:34That's what this event gives you.
02:36I'm my example, press F7 to switch to the code;
02:40I'm using that to dispose of my video stream I'm opening.
02:44Rest of the code is exactly the same as the previous example with one exception.
02:49This line of code here, when I ask for the CaptureFileAsync I tell I want a
02:54video stream as oppose to an image stream.
02:56Let's see what this looks like on the device.
03:00Here in the capture app, I'll touch to Get Image button to load the first page.
03:05In the center of the screen is my image element.
03:08I'll touch the Get Image button.
03:11This CaptureUI shown on the screen is provided by the framework.
03:15Filling the screen is the feed from the camera.
03:18To take a picture, touch the screen.
03:22Now I can choose to press OK or retake the picture. I'll try Retake.
03:29There's also a Timer option.
03:31I'll press the Timer button;
03:33see how the button has a white background?
03:35That signals that I'm in Timer mode.
03:38Touching the screen activates the Timer.
03:41This is perfect for getting yourself in a group picture.
03:45Next, I'll press the Crop button.
03:47I can size the Crop area with the white handles and move the crop area with my finger.
03:56When I touch OK, the image is returned to my app and added to the app UI.
04:01This camera app is very convenient.
04:03Now let's see what happens with the video stream.
04:06Press the Get Video with CameraUI button.
04:10Press the Get Video button.
04:12I'll turn off the Timer and touch the screen to start capturing the video.
04:18Now I can see the video time code in the lower-right corner.
04:22Touch the screen again to stop the capture.
04:26The video starts to playback immediately in my media element.
04:30I can preview and scrub the video, and when I'm satisfied with the output touch
04:34OK to add the video stream to my app UI.
04:37With a small amount of code, I've added media capture to my app.
04:42Back in Visual Studio, I'm going to switch to this page, CaptureElementPage.xaml
04:50and I'm going to click on this element here, this is called the CaptureElement.
04:55The two examples I demonstrated on the tablet show how easy it is to grab
04:59a media information.
05:01The CameraCaptureUI class does a lot of work showing the capture screen to the user.
05:06That is not a good solution for some types of apps.
05:09Think how video chat programs work.
05:12The live feed from your webcam is shown on the chat program itself.
05:15You don't go to a separate app;
05:17capture the video then return to the chat app later.
05:20That would be pointless.
05:22You need to show the live stream in the chat app.
05:25To enable this type of interface you use this element on the screen,
05:28the CaptureElement.
05:30It allows me to show the live feed within my page itself.
05:35I've got two other elements on the bottom of the screen.
05:39The button starts to live capture.
05:42The user should be able to opt into showing the live feed.
05:45This is good for many reasons.
05:47For one, the user may not be ready to be seen on camera.
05:51For this reason, I believe you should never start the camera and capture
05:54automatically upon an app start up.
05:57The second interesting UI element is this Toggle switch.
06:00Once the camera is alive, enabling this Toggle causes a timer to run taking
06:05pictures every half second.
06:07Let's look at the code. Press F7;
06:12let's start by looking at the Toggle switch code, that's very simple.
06:16When the Toggle Switch is turned on, I start the timer, when it's turned off I stop the timer.
06:22I scroll back up, here's the Previewbutton_Click event handler.
06:29What I'm doing here is I want to switch to the back camera on my surface tablet.
06:34So what I'm doing in this line of code is I'm finding all the devices on my
06:38tablet that can capture video.
06:41And then I'm grabbing one of those videos, the back camera and I'm assigning it
06:47to the setting class.
06:49Then here, I'm initializing the capture.
06:52This starts the capture process and I'm passing in my settings value.
06:56And then once I've got the feed ready, I load that into my capture element on
07:02the XAML page, then I start showing a live preview here.
07:06This CapturePic method gets called on the Timer tick event.
07:10What it's doing is it's creating a JPEG file or a set of property I should say.
07:16Then it's giving an InMemoryRandomAccessStream, it's using the capture object
07:21to capture the current frame into the stream using my properties when I was on a JPEG.
07:28I flash out the stream here.
07:30The rest of this code is creating a BitmapImage and then what this code down
07:34here does is it creates a brand new instance of an image element and then adds
07:38it to a ListView on the page.
07:41Let's go see what this one looks like on the Tablet PC.
07:45To see the demo, I press the capture element button.
07:49At the top of the screen is my capture element.
07:52Press the Show Preview button.
07:54Now I'm giving a live feed into my UI.
07:58To start grabbing frames from the live feed, I'll slide the Toggle switch to the On position.
08:03The frame show up on the right side of the UI.
08:07To stop the frame capture, slide the Toggle Off.
08:11The capture API is fully featured.
08:13There are many areas that I didn't show in this video.
08:16For example, I didn't demonstrate how to capture an audio only stream.
08:20You'll be happy to know that it's just as easy as the other examples.
08:24Once you have the audio stream you can save it to WMA, M4A, and MP3 file formats.
Collapse this transcript
16. Working with Files
Getting file access permission for your app
00:00Windows developers are accustomed to unlimited access to the file system.
00:05This is not the case in the Windows Store app world.
00:07File access is constrained due to security concerns.
00:11The app framework provides a mechanism by which we can specify what locations
00:15our app expects to access.
00:17This information is shown to the user when they are considering installing the app.
00:22The user chooses whether or not they trust the app enough to download and
00:25install, given that it has access to certain file locations.
00:29I'll cover how to declare these file intentions in this video.
00:32Here inside this project, I'm going to modify my request for file access by
00:37double-clicking on this Package.appxmanifest file.
00:40Then I'm going to click on the Capabilities section.
00:45This shows the different capabilities I can request.
00:49I can ask for access to the Music Library and the Pictures Library.
00:53I can also ask for an access to the Videos Library, Removable Files, say on a USB
01:00drive, and on the Document Library on the local computer.
01:05Note, you can only submit an app that accesses a document or removable store if
01:09you have a business store account.
01:12Individual store accounts cannot create apps which use these capabilities.
01:15Now let me go look at the Declarations page.
01:19One thing you might want to do with your app is have a specific file extension
01:23that means something to your app.
01:25To work with that, you have to declare what is known as a File Type
01:28Association. Click here and then click on Add and then over in this section I
01:35have to specify a group name and I have to specify the extension type, from my
01:42example it will be .tour.
01:44And then if I have another file extension my app might want to work with, I
01:48click on Add again and I'll do the same thing.
01:52Choose a name and choose a file extension.
01:56Now our application is ready to use the files.
01:58Another video in this chapter shows how to work with a storage file and a
02:02storage folder classes.
Collapse this transcript
Working with storage files
00:00This is the Music Library on my computer.
00:03It has a few folders and a few music MP3 files.
00:06I'm going to show you how to write code to manipulate the folders and files
00:10within this location.
00:12I'll switch to Visual Studio in this project called WorkingWithStorageFiles.
00:16When you're working with files from codes, there are a few classes you'll
00:19need to understand.
00:21The classes used for Windows Store apps are not the same as those you may be
00:25familiar with on the desktop.
00:26The biggest challenge is that definition of file from an API perspective has
00:32been expanded to include resources and remote files.
00:36Most of the code for this demos inside this Demo.xaml file.
00:40Let's take a look at the UI.
00:42Here is the design view.
00:44I have three list of views:
00:46one, two, and the third one you can't see yet.
00:51And I have a number of buttons that are going to Create Folders and Create Files
00:54and List Those Files.
00:56Let's start by creating a folder programatically.
01:00The first thing I'm going to do is create a variable to hold the StorageFolder.
01:05Next, I'm going to load that with the information from one of the known locations.
01:13I'll use this KnownFolders enumeration, which lists all of the areas I can access in my code.
01:20I should say these are potential areas I can access.
01:23I still need to have the permissions to use these folders.
01:26I'm going to choose the PicturesLibrary.
01:28Later, if I want to work with the MusicLibrary, take this one instead.
01:34Of course that's where my files are so that's what I'm going to use in this demo.
01:39Next, I need to create my folder.
01:42To do that, I'm going to create a variable and then I'm going to call the await
01:51keyword and then I'll take my variable folder.
01:56and here I'm going to CreateFolderAsync.
02:02Yes, I need to provide a name for this folder.
02:07And then there's an argument I can pass in called the CreationCollisionOption,
02:14which I'm going to use.
02:15And here I'm saying, if the folder already exist, just replace the existing folder.
02:19Next I'll comment out these two lines and then we'll run the app by pressing Ctrl+F5.
02:25I see I have a build error, that's because I have a typo, this should be a capital F.
02:32Press Ctrl+F5 to run the application and click on the Create Folder button.
02:39And at the bottom of the screen you see a folder is created at 8:03 PM.
02:43Let's see if it was in fact created in the Music folder.
02:46I'll switch to Music and there is my new folder.
02:50Next I'll show you the storage of file class.
02:52That's down here in this CreateFile_Click event handler.
02:56Again I start off by getting the MusicLibrary and I call CreateFileAsync and ask
03:01for a file called BeatTrack.mp3.
03:04My collision option is to generate a unique name if the file already exists.
03:09Once you have a handle on that file and you can do the things like MoveAsync.
03:15Here I can move it to the PicturesLibrary, delete the file or even open the
03:20contents of the file and load them into a stream.
03:24Run the app, click on Create File a few times, switch back to my Music Folder
03:30and there are my three new MP3 files.
03:35The next demo shows how to list the files in a Library.
03:39I'll start off with the MusicLibrary, then I write this foreach loop.
03:43Here I'm getting the files asynchronously from the folder and adding them to this ListView.
03:50Down here I'm doing the same thing but I'm calling GetFolderAsync, which will get
03:54me the child folders of the Music folder.
03:57I'll click on this button, the first column shows the song names and the second
04:03column shows the folder names.
04:05I can also do the same thing by clicking on this button.
04:09I'm getting the files but I'm using a slightly different technique to load
04:12them into the List View.
04:14Here's what I'm doing, I'm calling folder GetItemsAsync and loading that in this variable.
04:21And then, I'm taking that variable and assigning it to the ItemsSource of the FilesListView.
04:26This is binding to the underlying file system.
04:30The last demo shows that I can make the user control prettier.
04:35So what I'm going to do is add some icons to the file folders so it looks nicer.
04:39Let me show you that demo. Here it is.
04:44So I'm listing the folders but this time I'm putting a file icon on the side.
04:49If you want to see how this was done, and examine the XAML, it's over here on
04:53this folder TemplatedListViewFile.
04:55Check this one out, here is the XAML.
05:01The key part is there's a ListView and then there's a DataTemplate and somewhere
05:05in this DataTemplate is the images that's getting loaded.
05:08The storage API is full of interesting methods for working with files and folders.
05:13In the next video, I'll show you how to filter the file system with the
05:16built-in querying tools.
Collapse this transcript
Querying files
00:00This is my Music folder on my PC.
00:03It has a number of MP3 files;
00:06it also has some non-music files like this license.txt and this zip file.
00:11There is also a wav file in here.
00:14If you look inside these children folders, you'll see there are also some files
00:17further down the folder tree.
00:20There are times when you will not want to show all the available files to the user.
00:24The wrong way to filter your list is to get all the files and then filter the result.
00:29It's better to use the File Query features incorporated in the API.
00:33In that way, the operating system can do the filtering for you in a fast
00:36and efficient manner.
00:38Let's see how you do that in your code.
00:42In this project I'm going to look at the Demo.xaml file.
00:46This UI has two buttons on the left side and two list boxes in the center which
00:50I'm going to fill with information from my queries.
00:53Let's look at this first query.
00:55I want to query the files and pull back a subset of the files.
00:59I start by getting a known folder, in this case the MusicLibrary folder, and
01:05then I set up a QueryOption.
01:07This is some of the extra settings that I want to do it with my query.
01:10I want to choose to order them in alphabetical order by Music Properties and I
01:17want to specify a list of extensions.
01:19So I'm only interested in MP3 files and wav files.
01:24So let's pass in a listed string here.
01:26Those two options are stored in here then I go down and I say my
01:30FolderDepth option is Shallow.
01:33And I would like to use the Indexer.
01:35If the operating system has indexed this location, I want to go ahead and use
01:39the Indexer to retrieve the information.
01:41Now this shallow depth means that I'm only looking at the top level.
01:45Later I'll switch it to deep and go look deeper in those children folders.
01:49So now I'm ready to create my query.
01:51I call CreateFileQueryWithOptions and then pass in my option values.
01:55And then I call queryResult.GetFilesAsync.
01:59That gives me back a list of the files from the query and then I can just assign
02:04those as the item source for this file list.
02:07Let's see what this looks like.
02:08Ctrl+F5 to run the app.
02:13Click on the Query Files, there are four files listed, let's go look at my music
02:18folder to see what we have there, four music files.
02:22Okay, so I left out the license.txt and a zip file.
02:25Now I'm going to switch back to my code and make one modification.
02:29I'll switch this to Deep and run the query again.
02:38And this time the results show some of the files in the children folders.
02:41Another thing I like about this API is that you can set up a watcher to monitor
02:46that folder and if there's any changes, your code is notified.
02:50I'm going to do that with this Query and Monitor folders.
02:53Let me show you how it works first and then I'll show you the code.
02:56I'll click here, I'm monitoring my pictures folder.
02:59So I'm going to go back to the File explorer.
03:02Go to my Libraries and go to Pictures.
03:08I have one child folder in my Pictures folder.
03:12I'm going to add another one, I'll call this one, Art and then I'll take a few
03:18of these pictures and move them in to this Art folder.
03:22Now when we go back to my application, you'll see that I was updated in my code
03:27and I was able to show that there's a new folder available.
03:30I also got the timestamp down here that the folder was changed at 9:27 and 56 seconds.
03:36Let's see how you do that in the code.
03:38I start by getting the PicturesLibrary then I set up a variable called the
03:43StorageFolderQueryResult.
03:46When I clicked on that button, I create what's called the folderQuery then, I
03:51wire up this event handler for the ContentsChanged a bit.
03:55Then I call method that I wrote called GetFolders, here's my GetFolders.
04:00My GetFolders is returning a task, which means it can be called in the basic risk
04:05fashion, all my code in this method doing is getting the folders asynchronously
04:10and then assigning them to my FoldersListView.
04:13But if I have one more bit of code to show you, that's here, that's the
04:17query_ContentsChanged.
04:18This fires when you add a folder or delete a folder and all I did in here was
04:24I'd put a message and I call the dispatcher.
04:27I have to do this because the code is going to be running in on background
04:31thread what I enters this and I have to switch over to the UI threads, so
04:35that's why I'm going Dispatcher. RunAsync then, saying update a
04:38messageTextBlock in that page.
04:40And then I call a GetFolders again, which runs this to return the query for the
04:45folders and then I show that in the list box.
04:48There's more to explore in the query API.
04:50Some of the GetFile methods take a query parameter for active filtering.
04:54The important concept to take from this video is that you should leverage the
04:57operating system for fast, efficient querying and file monitoring.
Collapse this transcript
Choosing content with file pickers
00:00There are times when your app should prompt the users to select the file for action.
00:05A photo editing tool, for example, has the user select a file to load into the editor.
00:11In a traditional Windows application, you use the File>Open dialog method to
00:15accomplish this task.
00:17In WinRT, this is done with the File Pickers.
00:20Pickers work with local and network files.
00:23They also work with logical files, which means that a WinRT application can
00:27serve as a file source.
00:29My examples today are in this Demo.xaml file.
00:34Let's look at the code and the user interface.
00:39This has three buttons on the left side and a checkbox in the bottom.
00:45And then over here, it's hard to see, but there's an image control in this
00:47location, image control is called show chosen image.
00:52This first bit of code on the FileOpenPickerbutton_Click event handler is going
00:58to ask user for an image file.
01:01I start off by wiring up an event handler called ImageFailed.
01:05This fires if you cannot load the image for any reason.
01:09Perhaps, the file they pick is in the wrong format.
01:13Here, I have a method named EnsureUnsnapped.
01:17If you try to open a file picker or folder picker when you're in snap mode, it
01:22will throw an exception.
01:23So what this code does is check to see whether you're in the snapped view and if
01:29you're not, it tries to call the TryUnsnap method.
01:33Then I create my FileOpenPicker, store it in a variable.
01:37Here, I'm suggesting a starting location.
01:40The first time the user works with your app, this is where they'll start.
01:44If they open a different location, the application remembers where they're at.
01:48So the next time they open the FileOpenPicker, they'll used their most
01:51recently used location.
01:54This little bit of code here just checks that checkbox.
01:57If the checkbox is checked, I open the picker mode with the thumbnail views.
02:01Otherwise, I open it with the List views.
02:03Then I add a couple of filters in the FileTypeFilter, looking for png and jpeg files.
02:09And then finally, I ask for the file.
02:12Now this is going to show that dialog to the user, the user is going to
02:16pick the file or cancel.
02:17At that point, I'm going to have a variable stored here.
02:20If they canceled, it's going to be null, so I do a check for that.
02:23If they select a file, then I output you picked such and such of file and then
02:28I call this function called SetupImage.
02:31By the way, if you want to have the users work with multiple files, you use this
02:34method PickMultipleFilesAsync instead of PickSingleFileAsync.
02:40My image code is being set up here.
02:42This is the method called SetupImage.
02:45It takes a storage file in as an input parameter, creates a temporary bitmap
02:50image, uses that StorageFile that was passed into, open the file in asynchronous
02:55mode and read the contents, creates a stream out of that.
02:59Then it uses that stream to set the source on what's called the tempImage, this
03:03BitmapImage up here.
03:04And then finally, it assigns that image to the source of my image control.
03:10That was a long-winded explanation.
03:12Let's just see what it looks like.
03:13Ctrl+F5, click here.
03:19User decides to cancel.
03:23My text says, You didn't pick a file.
03:25Now I'm going to click on this checkbox and open that dialog again.
03:28Now you see I just have thumbnails of the pictures.
03:31I'll pick this abstract image and then I'll click on Open.
03:38And there is my abstract image loaded in this location.
03:41Let's try one more time.
03:42I'll pick this great photo over here; that looks nice.
03:47You can also save a file with a File Save Picker and there's also a Folder Picker.
03:53We're returning back to Visual Studio to look at that code.
03:55Here's the code for the File Save Picker.
03:57I create a FileSavePicker dialog.
04:02I choose what I want to say on the Commit button, Save your vacation picture.
04:07I also suggest to start location in the DocumentsLibrary.
04:11I suggest the file name for the user, beach.png and then I add a set of choices
04:18that they could see for their file types that they can save.
04:21Once again, I call the PickSaveFileAsync to open the picker and then of course I
04:27would save the file contents here.
04:28I'm not actually saving the image, I'm just showing you how to open the dialog.
04:32Back to the app, click the second button.
04:35You see I'm in the Documents folder, the suggested file name is beach.png and
04:40here is my list of filter types.
04:42There's only one right now, png file.
04:45My text has been updated, Save your vacation picture.
04:49Now I don't have to stay in this folder, right?
04:51I can click on Files and here is the approved locations where the user can go work with their files.
04:57So I can switch over to the Picture folder if I wanted to and then save it there.
05:01They say, oh no, I don't want to save that.
05:05Never mind, and then cancel.
05:08Back to Visual Studio.
05:09Let's look at the FolderPicker event handler.
05:11Let's start off by creating a picker, picking my location, no pun intended,
05:16saving my file type png, then I choose whether I want to use thumbnail or list view again.
05:23And then, I open my PickSingleFolderAsync.
05:26Here I'll check to make sure I got a folder back.
05:29This little bit of code grants me some future permissions so that the operating
05:33system can get me access to the files a little bit quicker.
05:36And then, down here I tell you which folder you picked or whether you
05:40didn't pick a folder.
05:41So let's see what that one looks like.
05:43Back to the running app, click on Folder Picker.
05:47We'll start here at the Documents level.
05:50I'm looking for PNG files, right?
05:52So if I go look in this Visual Studio 2012, I'm not seeing any PNG files.
05:56If I try to choose this folder, it will tell me down here it picks it but this
06:01says, there is no files in that.
06:04If I want to pick one of the pictures in it, we'll go up here to Pictures, then
06:10Art, and then Choose this folder.
06:13Again, it shows me the folder is picked, then OK, and it says, You picked the Art folder.
06:18So as you've seen, the File Pickers make it easy to ask the user for a file or
06:23a folder location.
Collapse this transcript
Understanding URI file paths
00:00The URI is a common way to locate unique resources.
00:03The URI and URL have been part of the HTTP and web programming world for a long time.
00:09Basically, they are a unique string that identifies a resource on a web server.
00:14When Microsoft created the Windows Runtime, they had to decide how to
00:18identify resources.
00:19Many of you maybe familiar with the GUID or Globally Unique Identifier used in
00:24other Microsoft languages.
00:26While the GUID is used in some parts of WinRT, the URI was a choice for file look up.
00:33The URI is the approved way to get files and other resources in WinRT apps,
00:37which is why I'm discussing it in this chapter.
00:39The XAML shown on this page is loading a picture, the grapes.png file, into an image.
00:46The image source property is set with a URI.
00:50Note, the lack of the beginning forward slash indicates the root of your application.
00:55In this case, the grapes.png file must be part of your project.
00:58While using relative URIs like this is easy, I prefer to use the schema prefixes
01:04shown on the next slide. Why?
01:07Because you cannot use relative syntax when passing the URIs to the WinRT runtime.
01:12Therefore, for consistency, I always use schema prefixes.
01:16There are three approved prefixes you can use to access files.
01:20The first one should look familiar.
01:22It is the HTTP prefix used for accessing web pages.
01:26The other two are Microsoft inventions.
01:29Note how both start with the ms- syntax and have three forward slashes at the end.
01:35With the HTTP prefix, you have access to any resource on the Internet.
01:39There are no cross-domain issues, just specify the URL and watch the magic happen.
01:44Microsoft created a special URI prefix for accessing application files. It's ms-appx:///.
01:54The ms-appx prefix signifies that the resource is part of the app package.
02:00In other words, the files are installed with the app.
02:03The three forward slashes indicate the file path from the project root.
02:08If you use two slashes, the path is relative to the current XAML file.
02:12Just remember, you cannot use relative paths with sending URIs to the runtime.
02:17The ms-appdata prefix signifies that the resource is located somewhere on the user's drive.
02:24Use the temp prefix to work with the local machine's Temp folder for this app.
02:28The temp location is best when you need to write disposable or transitional files.
02:33Use the local prefix to work with files in the apps local data store.
02:37This is typically used for a state that is specific to the machine.
02:41Use the roaming prefix to work with files in the apps roaming data store.
02:45This is synchronized between PCs and is associated with the account of
02:49the logged in user.
02:51This is the best place to store app-specific data which the user may want to
02:54share between machines.
02:56Now let's go see how to use these URIs in our application.
03:01Here I am inside Visual Studio inside this Demo.xaml file.
03:05Here is an image that's using a local path to retrieve a picture that is stored
03:11in my Assets folder.
03:12So over here in my Assets folder, I have beach.png and grapes.png.
03:16It is just a simple matter of using a relative path.
03:19You can see that relative path in a couple of places.
03:23I can go to my XAML and look at it. Here it is.
03:27Another thing I like to do is to use the Editor tool or the Properties tool on
03:32this side to set the path.
03:33The way I can do that is choose the image, open the Properties window, find the
03:39Source property, and then I can click on this down button here to choose another item.
03:46I'll switch that to beach, and now I'll switch back to the designer to see what it looks like.
03:52Again, that's a relative path. Here is the prefix path.
03:54I have to use ms-appx or ms-appdata like we mentioned on the slides.
04:00I'm going to change the URI for this picture in code.
04:05So let's see what's behind this button.
04:07You have to create an instance of a System.Uri.
04:10This is a string we're using here but it needs to be converted into a URI
04:14before I can use it. So that's what I'm doing h