Introduction
- The good thing is that since the entire framework is written with asynchronous programming in mind, it is much easier to maintain the responsiveness of user applications. You can at any time write the function with the async keyword for it and use the rest of the functions of the API with await, well many background threads may throttle the memory, but for smaller applications the framework is great! In my opinion, it is better in performance. The Windows Runtime was written on top of native code, Win32 was used as the base, so it makes it much better and easier to write efficient code in Windows Runtime, plus .NET Framework is also supported (not the entire .NET Framework, just a few).
- The Bad thing is that the abstraction layer applied to your application's sandbox is so narrow that you are not even able to communicate or use the directories outside your own application's AppData folder directory. If you try to write the data to somewhere other than that, the OS will prompt you with an "Access is denied" exception. Plus, the currently added capabilities in the application manifest are so reduced that you cannot even utilize the entire machine power, you just design an application. To do something useful you will need to get support from another framework, such as Win32, Native programming in C/C++ or you will need to use .NET Framework itself, there is indeed some support for the .NET Framework in Windows Runtime applications.
That said, now let us consider talking about a few other things before stepping into the writing process of the article. In the future sections, I will be talking about:
- Windows 10, not from a spammy or advertisement point of view, I understand you being shocked by this!
- Windows.Media.Capture namespace of Windows Runtime and what it offers us.
- How to enable capturing the images and audio from our application!
- Storing the images and the video files, using native Windows Runtime APIs, such as StorageFile object.
By the end of this article, you can feel free to download and build the source code package provided along with this article, the source code contains the sample that I made for the sake of this article, it can be used for your own use also. I would recommend that you read the article, vote and leave your feedback if you want me to fix something and explain something in-depth.
I believe before going in-depth about the technical concepts of Windows Runtime and Windows 10 I must share a little knowledge about them, I must teach you the basics of these frameworks and how you should use them when you try to build your own applications that make use of media capturing and recording facilities provided in the Windows Runtime.
In a Windows Runtime application, you must first define all of the capabilities that your application has before the OS would let it do anything. It is similar to declaring the uses-feature or uses-permission in Android programming. If you ever have done Android programming, it notifies the user about your application, plus it allows the user to revoke access to those permissions later. A good abstraction (IMO) but from a developer point of view, there comes another module for you to program! For example, now, you also need to ensure that the application has access to resources, say a camera. The user can always revoke access to the camera from the settings, you cannot alter the settings yourself. The user would need to, you can at most request them to change the settings for your application.
For a basic camera-like application (like the one I will be teaching in this article) you require permissions for a Webcam as well as Microphone. The function StartRecordAsync of the MediaCapture object (discussed later) requires you to allow Microphones also, well you can turn off the microphone but still, permission is required. If you do not allow these permissions, there will be an exception raised.
Every application needs a package manifest; the same as for Android applications. The manifest file contains the information required by the Store to process the application type. It holds the permissions the following (but not only) things in it.
General concepts used in article and sample
Windows 10 Capabilities of application
- Application details, such as:
Display Name.
Application Title.
Entry point: The class that gets executed.
Description of the application. - Visual Assets
The logo and splash screen images for your application. - Capabilities
The permissions your application requires to load and run. - Declarations
Services and other background processes that your application intends to perform; such as Alarm services, File Open and File Save services. - URIs and packaging information for your application.
All of these are required to identify your application, its purpose, its intentions, and its features. Users will be able to see your application when they need it to work for them, such as if you want to show your application as an Internet Client you can set the settings in the manifest and Windows will show the icon in the list for the application that the user wants. That is not just what it is intended to do, but users will also see your application listed on the Store under the category that they are interested in downloading the applications for. Name, Capabilities and Visual Assets are used to preview the applications on a Store. Others are required by the OS for underground works.
I will show you how to edit the manifest (in Design View) to allow your application to get a camera and microphone resources, in a future section.
Further in the article, you will determine that the Windows Runtime uses these capabilities to determine whether or not your application is authorized to use that resource. It provides users with a layer of privacy, that abstracts the applications to use resources without letting the user know. In settings applications, users can manage what applications have permission to and which permission they must be denied access to. For example, you can deny Internet access request to an application that you don't want to have access to the internet, the same applies to other permissions. You can deny a request to access the camera, microphone and file system resources. In my opinion, it is a great feature in the Windows Runtime.
Most of the media related APIs and namespaces are present in Windows.Media.XXXX namespace, audio controllers, video controllers and other similar objects that allow us to work around with the media resources, such as camera and microphone are all present in these namespaces. Separated in their own categories. You can reference them all in your application when you need to.
In this article, I will reference and talk about Windows.Media.Capture namespace only, because this namespace contains the objects and structures required to create our application. Every modern laptop or computer system has a webcam and microphone installed. That is why Windows 10's UAP platform supports these features natively for applications. If the feature is not present, it will simply notify the programmer.
The Windows.Media.Capture namespace actually provides the classes, enums, structures, and other stuff to communicate or workaround with the media devices, such as cameras and microphones and other sensors installed to them.
For example, Windows.Media.Capture namespace's MediaCapture object holds a definition for the ThermalStatus field that provides you with the thermal status of the device. Capturing the data may cause the device to heat, if the device has the feature to expose the thermal status such as a secondary device installed, it will allow you to capture the thermal status and turn off the capturing of the photos or videos. Plus, there is an event included that can be used to determine when the thermal status gets down to cold so that you can continue capturing the videos from that device.
The actual enum for managing the thermal status of a device is MediaCaptureThermalStatus. There are only the following two values for this enum:
Windows.Media.Capture namespace In-depth overview
- Normal (0; zero)
- Overheated (1)
Hopefully, these are the only objects required. You can, however, divide them into much more advanced topics in this namespace, or in the Windows. Media namespace collection. The Windows. Media namespace (collection) allows you to work around with other media types, such as audio and video frames or photo frames in real. Windows.Media.Capture exposes methods to capture the photos, audios or videos. Whereas to work around in-depth with other content would require some other low-level programming, those namespaces provide you with the objects for that.
Visual Studio 2015 comes packed with resources that you will need to build Windows 10 applications, there are many articles already written and ready for you to read and learn how to create your applications in Visual Studio 2015! You will find this CodeProject article helpful and of your own taste.
Visual Studio 2015 is the only IDE (currently) that supports Windows 10 application programming, plus you need to be running Windows 10 to write the software for Windows 10.
Tip
Visual Studio 2015 and Windows 10 apps
If you do not have Windows 10 or Visual Studio 2015, you can still use the code to write applications for Windows 8 and 8.1 or Windows Phone 8 and 8.1 applications! So, keep reading, and provide your feedback at the end in case of Windows 8 and 8.1 applications!
Let us head toward the main topic of this article, writing the source code for the application. This section will be divided into two main sections, one for writing the UI code and the other for writing the back-end code for our application. Both of them will be discussed as only the required sections because the purpose of the article is to teach the controls available in Windows Runtime with XAML (notice that this article covers the C# and XAML, in a later article someday I will show you C++ programming for the same application also) and how you can configure the application to capture the content and preview it on the screen. Plus, capturing and storing the content on your application's data folder.
As this section continues, I will show you the procedure to perform to create the application, so you can consider this guide to be a step-by-step creation of the application for capturing photos, audios and videos.
The first step in this guide is to build the XAML and the XAML code in this sample is just a single Page control with a few other child controls that create the UI for our application. The applications that show the display of a camera are usually using the CaptureElement control in the XAML. The CaptureElement control allows you to bind the Source property to a media device, such as a camera (webcam). The Windows Runtime provides the content from a camera, microphone and other media devices to this control, that is then rendered on the screen. It is similar to previewing a stream of bytes for a video frame on a video element.
Renders a stream from a capture device, such as a camera or webcam.
In the source code for the Page, I will be using this control only, and then programmatically I will provide it with the stream of video frames and other stuff that the Windows Runtime will manage the transfer and rendering on the screen for the user. The good thing for us is that the syntax to create this control is a single line (unless you want to add other functionality to the control also) as in the following.
Writing the application
Building the XAML Page
MSDN documentation for CaptureElement
- <CaptureElement Height="650" Name="captureElement" />
That is enough!
- Capturing photos
- Capturing the videos
When capturing videos:
- Allow users to mute/unmute the microphone.
- Allow them to stop capturing the videos; save the videos.
- Change the video encoding and video quality before starting the recordings.
Plus, a few checks to ensure that the user isn't capturing the photos when the video is being recorded and so on! These checks can be removed if not required and other checks can be added, such as changing the quality when recording the video. There are many things that are advanced topics and not covered in this basic guide.
To create the functionality, I used the AppBar control of Windows applications that enables us to provide the controls in a form of a tray to the user that can be set open and closed when needed by the user. Also, it has a great UI and UX for the user for being easy to use. Another thing you might want to know is that Windows 10 comes with better Icons in the Symbol enumeration. You can find many more great resources in the MSDN documentation for Symbol and AppBar. Furthermore, usually, I created the buttons and commands using theAppBarButton (that inherits from the Button object and thus provides us with all of the events, functions, and members that the Button object has, we are interested in the Click event of the Button object). These buttons and controls would lead us to the creation of the control bar (command bar) for the camera handling, we can use these buttons to change the state of the MediaCapture object that is at the moment connected to the webcam and microphone.
- <Page.BottomAppBar>
- <AppBar Background="Transparent" IsOpen="True">
- <Grid>
- <AppBarButton Icon="Camera" Name="cameraIcon" Click="button_Click" />
- <AppBarButton Icon="Video" Name="videoIcon" Click="button_Click" Margin="70, 0, 0, 0" />
- <AppBarButton Icon="RotateCamera" Name="rotateCameraIcon" Click="button_Click" Margin="140, 0, 0, 0" />
- <ComboBox Margin="230, 10, 0, 0" Name="videoQuality" SelectionChanged="comboBox_SelectionChanged">
- <ComboBoxItem IsSelected="True">Auto</ComboBoxItem>
- <ComboBoxItem>1080p</ComboBoxItem>
- <ComboBoxItem>720p</ComboBoxItem>
- <ComboBoxItem>VGA</ComboBoxItem>
- </ComboBox>
- <ComboBox Margin="320, 10, 0, 0" Name="videoType" SelectionChanged="comboBox_SelectionChanged">
- <ComboBoxItem IsSelected="True">MP4</ComboBoxItem>
- <ComboBoxItem>WMV</ComboBoxItem>
- </ComboBox>
- <AppBarButton Icon="Microphone" Margin="400, 0, 0, 0" Name="muteIcon" Click="button_Click" />
- <AppBarButton Icon="Library" HorizontalAlignment="Right" Name="libraryIcon" Click="button_Click" />
- </Grid>
- </AppBar>
- </Page.BottomAppBar>
The preceding code acts as the command bar for our application, thus enabling us to interact with the UI of the application to initiate various commands and functions in the application.

Figure: Function in application
Why do not set the Source property in XAML?
Handling the events and tasks
1. ThermalStatus property not used
I won't be using the ThermalStatus property of the namespace since for the sake of this guide, there is no need to use it. Plus I am not sure whether my camera supports it or not, but in your cases for your Windows Store applications, you must always consider using that object to determine whether your device should continue capturing the data or does it need some break, to protect it from blowing itself to kingdom come!
Plus, you also get to use the ThermalStatusChanged event in your application to determine whether the camera gets too hot or is it ready for use again. Using these will provide a better UX for your application and a good review too. Although they are not required, blow on the camera by the user when it has been heating and not turning if off, no problem. But, I will personally recommend you do use them for any incidental cases.
2. Facial recognition is provided!
Setting up the application
- private async void init() {
- await capture.InitializeAsync(); // (1)
- capturecaptureElement.Source = capture; // (2)
- await capture.StartPreviewAsync(); // (3)
- #region Error handling
- MediaCaptureFailedEventHandler handler = (sender, e) = > {
- System.Threading.Tasks.Task task = System.Threading.Tasks.Task.Run(async() = > {
- await new MessageDialog("There was an error capturing the video from camera.", "Error").ShowAsync();
- });
- };
- capture.Failed += handler;#endregion
- }
- First of all the InitializeAsync() function initializes the components and creates the instance (note that it, not the same as calling new MediaCapture() in your application) so that you can now use it for rendering purposes in your applications. This is called to determine the user settings also, to determine whether or not the user has allowed your application to use the resources. Read more in the MSDN.
- You bind the capture object to the XAML captureElement control. This then renders the incoming stream of content on screen for the user!
- This activates the streams to be passed to the view for the user to view them.
All of these functions are asynchronous in calls, allowing you to have responsiveness in your applications.
Tip
I have added a handler to the code, in case anything went wrong when capturing the content, it shows you an error message.
Sorry for the same heading, in this section, I will show the code that actually does everything, in the preceding XAML code if you pay attention you will see that every button was attached to the same handler for the Click event. I used it to remove the redundancy since the same thing would be done based on the icon the user used. I wrote the handler in a way to handle the button, based on the icons (in the code I am using the Name that is not visible in the UI) so that the same codebase can be reused.
Handling the events and tasks Jr.
- private void button_Click(object sender, RoutedEventArgs e) {
- if ((sender as AppBarButton).Name == "cameraIcon") {
- // Capture the image
- capturePhoto();
- } else if ((sender as AppBarButton).Name == "videoIcon") {
- // Start recording
- alterRecording();
- } else if ((sender as AppBarButton).Name == "rotateCameraIcon") {
- invertCamera();
- } else if ((sender as AppBarButton).Name == "muteIcon") {
- muteUnmute();
- } else if ((sender as AppBarButton).Name == "libraryIcon") {
- App.RootFrame.Navigate(typeof(Library));
- cleanResources();
- }
- }
- private async void cleanResources() {
- captureElement.Source = null;
- await capture.StopPreviewAsync();
- if (isRecording) {
- await capture.StopRecordAsync();
- }
- capture.Dispose(); // Dispose the resource
- }
- protected override void OnNavigatedTo(NavigationEventArgs e) {
- // Page has become active, load the resources.
- capture = new MediaCapture();
- loading.Visibility = Visibility.Visible;
- init();
- }
Capturing the photos
- The location where to store the file
StorageFile is created, you can specify other settings for the file too, such as name collision cases.
- Image encoding format, JPEG, PNG, etc.
These are required to convert the bytes stream from the capture element to your file system. They are then rendered as a valid image in your machine.
- Whether to capture a photo when a recording video
You can try your luck in both cases, in my sample I ignored the capture when the user was capturing a video!
Continuing from this, you can write the function that handles the request to capture a photo and store it somewhere in the file system. Both of these can be written in one line of code using Windows Runtime APIs. But since we need the additional condition, we will be spanning it a bit longer.
The preceding code has two paths, one to be executed if the user is recording a video and the other if the user isn't. One path shows an error message and the other one that interests us creates a new file at the application data folder named "Photo.jpg" and also notices the CreationCollisionOption.GenerateUniqueName being applied, this enables us to handle what happens if a file already exists. In such cases, the file is given a unique name, such as appending an integer to it.
- private async void capturePhoto() {
- if (!isRecording) {
- var file = await(await ApplicationData.Current.LocalFolder.CreateFolderAsync("Photos",
- CreationCollisionOption.OpenIfExists)).CreateFileAsync("Photo.jpg",
- CreationCollisionOption.GenerateUniqueName);
- await capture.CapturePhotoToStorageFileAsync(ImageEncodingProperties.CreateJpeg(), file);
- } else {
- await new MessageDialog("Application is currently recording your camera, please stop recording and try again.", "Recording").ShowAsync();
- }
- }
You can add flickering, audio effects and other stuff depending on your needs! That was a bit of fancy, so I ignored it.
Recording a video
- The storage file where we will store the recording.
The same as in the preceding, nothing is different. Oh yeah, the format extension only. - The encoding in which the video must be recorded.
You see in the previous section we did the same, we created the image based on a format! The same thing is done in this module too, we create a video encoding to be used, MP4 or WMV. We also pass the quality for the video, 1080p, 720p, etc. These are then used to configure the settings for storing the contents on the file system. The Windows Runtime has us covered, do not worry!
- private async void alterRecording() {
- if (isRecording) {
- // Stop recording
- await capture.StopRecordAsync();
- videoIcon.Foreground = new SolidColorBrush(new Windows.UI.Color() {
- A = 255, R = 0, G = 0, B = 0
- }); // Black
- isRecording = false; // Not recording any more
- } else {
- encoding = getVideoEncoding(); // Get the current encoding selection.
- // Start recording
- var file = await(await ApplicationData.Current.LocalFolder.CreateFolderAsync("Videos",
- CreationCollisionOption.OpenIfExists)).CreateFileAsync(
- string.Format("Recording_{0}-{1}.{2}",
- myEncoding,
- myQuality, (myEncoding == "MP4") ? "mp4" : "wav"),
- CreationCollisionOption.GenerateUniqueName);
- await capture.StartRecordToStorageFileAsync(encoding, file);
- videoIcon.Foreground = new SolidColorBrush(new Windows.UI.Color() {
- A = 255, R = 255, G = 0, B = 0
- }); // Red
- isRecording = true; // Capturing the video stream.
- }
- }
The preceding code captures and stops capturing the video stream and saves the buffer on the file system for further usage. The file created must have the name specified and the format extension. The preceding code is flexible enough to change the extension and other settings based on what the user chose when starting the recording event. I would like to share the content of the getVideoEncoding function also, since that is said in the preceding code, not sharing it here will not be sincere.
This function is executed each time the settings are changed in the UI, please see the command bar and find theComboBox controls that are provided. They are used to change the video encoding and video quality being saved on the machine. Each time the settings are changed, this function is executed to select the current settings for video encodings and quality for the video file being stored.
- private MediaEncodingProfile getVideoEncoding() {
- VideoEncodingQuality quality = VideoEncodingQuality.Auto;
- myQuality = "Auto";
- switch (videoQuality.SelectedIndex) {
- case 2:
- quality = VideoEncodingQuality.HD1080p;
- myQuality = "1080p";
- break;
- case 3:
- quality = VideoEncodingQuality.HD720p;
- myQuality = "720p";
- break;
- case 4:
- quality = VideoEncodingQuality.Vga;
- myQuality = "VGA";
- break;
- default:
- break;
- }
- myEncoding = (videoType == null || videoType.SelectedIndex == 0) ? "MP4" : "WMV";
- return (videoType == null || videoType.SelectedIndex == 0) ? MediaEncodingProfile.CreateMp4(quality) : MediaEncodingProfile.CreateWmv(quality);
- }
Preview mirroring
You can do that programmatically too as in the following.
- captureElement.FlowDirection = FlowDirection.RightToLeft;
Mute/Unmute the microphone
- AudioDeviceController
This member controls the audio controller. In this section, I will be using this member to control the functions of muting and/or unmuting the device. - CameraStreamState
Gets the current stream state for the camera stream. - MediaCaptureSettings
The settings for your capture object. - ThermalStatus
Already discussed previously, it provides you information about the thermal status of your device. - VideoDeviceController
The object that controls the settings for the video controller. I won't explain it here.
- private void muteUnmute() {
- if (muted) {
- // Unmute
- capture.AudioDeviceController.Muted = false;
- muted = false;
- muteIcon.Foreground = new SolidColorBrush(new Windows.UI.Color() {
- A = 255, R = 0, G = 0, B = 0
- });
- } else {
- // Mute
- capture.AudioDeviceController.Muted = true;
- muted = true;
- muteIcon.Foreground = new SolidColorBrush(new Windows.UI.Color() {
- A = 255, R = 255, G = 0, B = 0
- });
- }
- }
The main code that does everything is the alteration in the Muted property of the controller that changes the state and mutes it or unmutes it. The preceding code checks what action to perform based on the current state of the controller.
Of what good will be the camera application if there is no way to view what you saved, and preview what you recorded. Apart from just being able to create an application that captures and stores the images and videos, I want to share the code to be able to create a gallery for those images and to preview the videos also. Anyways, the gallery (since I am not a good designer) does not contain a very elegant style UI for the users, it is just a ListView that previews the content in the form of a list.
Tip
Building the Gallery Library
If you want, you can show the content in a form of GridView also, you can remove the image names and other content and just simply preview the image in an Image control of Windows Runtime APIs.
I created the gallery using a new page control, that has a separate bottom app bar and other details, thus allows us to abstract the camera controls and the library controls from each other. The main idea is that in the library the user must be able to get a list of images that he has captured, a list of videos that he has recorded and to be able to preview. We only need a ListView control or a GridView based on how we want to design everything. We can then bind the list of media files to these lists and show them to the user.
In this section, I will teach you how you can do that so that in your own applications, you can modify the source code and create a separate theme and style for your users or provide them with both the styles (list styled gallery or grid styled gallery). Windows 10 application development is really very easy!
If you have even been developing a dynamic application, you are already familiar with the procedure of Data Binding. It is a technique to bind the controls, the visual elements, to back-end data. The data fills the controls as it is present and as much as it is present. Data binding allows developers to create a single template for their application's layout and then the data fills the template and is provided to the user. I would recommend that you give What is Data Binding (from MSDN) a thorough look before continuing.
In the following code for the application, I am using the same technique, I am creating a template for the controls to be viewed, then I will pass a list of objects (remember, we can also pass a list of single elements or no elements in it, XAML will take care of the rest) and they will be used to create the application's UI based on our data. This way, no matter how many images or videos have been captured, we can display them in the application's UI and the good thing is that we only need to provide the template. XAML will fill in the details itself and will take care of the rendering.
You must, first of all, understand that rendering an image and rendering a video is different and different controls are used to render the images. In the following section, I have created two separate ListViews for each of them, one of them is used to render the images and the other one for rendering the videos. Thus two templates are being used in the application for the gallery. In the list that renders the images, I used an Image control. That enables us to add the Source property to some image resource from our file system. On the other hand, for the list that renders the videos, I am using MediaElement to render the videos on the screen. Similarly, it also provides us with the features to alter the Source property, so we can attach it to a video resource from our file system.
The preceding XAML code is simply the template that I was talking about previously. This will be filled in with the data that we provide. The ItemTemplate allows us to template the items being added to it. It responds to theItemsSource (or Items; that is often not recommended to be used) property and uses their attributes or properties to fill in the data.
Building the Page
- <TextBlock FontSize="20" TextWrapping="WrapWholeWords" Margin="30, 0, 0, 0">This is the area for your application's gallery, images or videos recorded can be viewed in this area.</TextBlock>
- <ListView Name="photos" Visibility="Visible" Margin="0, 100, 0, 0">
- <ListView.ItemTemplate>
- <DataTemplate>
- <Grid Height="100" Margin="10, 2, 10, 2">
- <Grid.ColumnDefinitions>
- <ColumnDefinition />
- <ColumnDefinition />
- </Grid.ColumnDefinitions>
- <Image Grid.Column="0" Source="{Binding Path=Path}" />
- <TextBlock Grid.Column="1" Margin="10, 0, 0, 0" Text="{Binding Path=Name}" />
- </Grid>
- </DataTemplate>
- </ListView.ItemTemplate>
- </ListView>
- <ListView Name="videos" Visibility="Collapsed" Margin="0, 100, 0, 0">
- <ListView.ItemTemplate>
- <DataTemplate>
- <Grid Height="100" Margin="10, 2, 10, 2">
- <Grid.ColumnDefinitions>
- <ColumnDefinition />
- <ColumnDefinition />
- </Grid.ColumnDefinitions>
- <MediaElement Grid.Column="0" Width="100" Source="{Binding Path}" Volume="0" />
- <TextBlock Grid.Column="1" Text="{Binding Name}" Margin="10, 0, 0, 0" />
- </Grid>
- </DataTemplate>
- </ListView.ItemTemplate>
- </ListView>
Twisted words: Why the Source is set in the XAML now?
Binding the data programmatically
- public class Data
- {
- public string Name { get; set; }
- public Uri Path { get; set; }
- }
Done, now we can have the values that we need. (Plus, you can remove the Name field and use the Path only, then you can get the values for the Name and other stuff later! But that is done in advanced topics.) Now we can also create the lists of the objects we need.
- protected override void OnNavigatedTo(NavigationEventArgs e)
- {
- getResources();
- // Set the resources
- photos.ItemsSource = photosList;
- videos.ItemsSource = videosList;
- }
- private void getResources()
- {
- System.Threading.Tasks.Task.Run(async () =>
- {
- var photos = await (await ApplicationData.Current.LocalFolder.GetFolderAsync("Photos")).GetFilesAsync();
- var videos = await (await ApplicationData.Current.LocalFolder.GetFolderAsync("Videos")).GetFilesAsync();
- // Filter files
- foreach (var photo in photos)
- {
- photosList.Add(
- new Data { Name = photo.Name, Path = new Uri(photo.Path) }
- );
- }
- foreach (var video in videos)
- {
- videosList.Add(
- new Data { Name = video.Name, Path = new Uri(video.Path) }
- );
- }
- }).Wait();
- }
I am using a Task to perform this function because if I wouldn't then the following two cases would develop:
- await keyword will continue the previous function from where we came and will apply a null list to the photos and video control. That is not what we want, but that is exactly what awaits does.
- If we ignore the await keyword then we are left with IAsyncResult objects, working with which is again a headache.
Keeping the preceding points in mind, it is better to use the await keyword and let the application's function run asynchronously rather than wait for the task to complete. This allows us to maintain the synchronous calls so that the list applied actually represents the images and videos in the file system. Plus, we don't need to work around with other low-level interfaces for asynchronous function results and so on.
Once the preceding code compiles (press CTRL + SHIFT + B to test) it will provide you with the data that you require from your file system. Also, since we are loading the resources when the user navigates to this page, we also get a +1 feature in which, if we capture new photos or videos, they are also shown in the library; I am calling this gallery a Library!
Tip
Using a GridView instead of ListView
If you are interested in using a GridView instead of a ListView then you can change the content of your XAML page and instead of the ListView control use the GridView. The example of which can be seen below.
This would now render the images in a grid rather than stacking every image in a list, vertically.- <GridView Name="photosGrid" Visibility="Visible" Margin="0, 100, 0, 0">
- <GridView.ItemTemplate>
- <DataTemplate>
- <Grid Height="100" Margin="10, 2, 10, 2">
- <Grid.ColumnDefinitions>
- <ColumnDefinition />
- <ColumnDefinition />
- </Grid.ColumnDefinitions>
- <Image Grid.Column="0" Source="{Binding Path=Path}" />
- <TextBlock Grid.Column="1" Margin="10, 0, 0, 0" Text="{Binding Path=Name}" />
- </Grid>
- </DataTemplate>
- </GridView.ItemTemplate>
- </GridView>
Preview of the application sample
Image library
- <AppBarToggleButton Icon="Video" Click="showVideos_Click" HorizontalAlignment="Right" Name="showVideos" Content="Show Videos" />
Videos library
Points of Interests
What's next?
Plus, if I have any other ideas about this, I will let you guys know. Do leave your feedback if you have any other ideas.
Summary
In this article, we learned about Building Camera App and Gallery in Windows 10.

dev swapPosted Mar 14, 2017, 12:39 PM
Windows 10 does not opens camera in fullscreen mode using Windows.MediaCapture . Any way to do so?
jacob floresPosted Oct 4, 2016, 5:30 PM
Hi im getting en error on the getResources method. error and exception of type system.aggregateexcetion occurred in mscorlib.ni.dll but was not handled in user code additional information one or more errors occurred
Harshad PansuriyaPosted Sep 12, 2015, 3:38 AM
Nice
Hashim ShafiqPosted Aug 30, 2015, 9:45 PM
nice
Santhakumar MunuswamyPosted Aug 29, 2015, 5:15 AM
Good article. Thanks for sharing
Pankaj Kumar ChoudharyPosted Aug 27, 2015, 12:48 PM
Really a great article ,Thanks for share....
Gowtham RajamanickamPosted Aug 27, 2015, 9:50 AM
Good
Yashwanth MuthineniPosted Aug 27, 2015, 5:46 AM
Nice Share
Ankit BansalPosted Aug 27, 2015, 2:21 AM
Good one..Thanks for sharing..
Pooja BaraskarPosted Aug 27, 2015, 2:03 AM
Ofcource a nice article, but awesome writing skills
Rajeesh MenothPosted Aug 27, 2015, 12:56 AM
Thank you for sharing sir
Mohammed IbrahimPosted Aug 27, 2015, 12:06 AM
nice
Karthikeyan KPosted Aug 26, 2015, 11:25 PM
Good one sir...Thanks for sharing
Gowtham KPosted Aug 26, 2015, 10:38 PM
Good one, Thanks for sharing