As we know, WinJS applications are built using HTML/JS/CSS. But for a complete application, we will need async operations like SQLite database operations, web-service calls, and other native XML read-write, File read-write which cannot be done directly in WinJS applications.

So we have to use Windows Runtime Component to access Native C# methods as discussed in my previous blog: WinJS application with Windows Runtime Component to access Native C# Code.

Please go through these links.

I will show you how to pass a string as a parameter from our html page to Runtime Component and create a folder with the same name using IAsyncOperation.

Step 1:

Firstly, create a WinJS project as discussed in my previous blog: Create your first WinJS application

Step 2:

We need to do the following things as discussed in my previous article.

Step 3:

In Service class, we have to create IAsyncOperation with the return type string as:

  1. public IAsyncOperation<string> CreateFolder(string folderName)
  2. {
  3. return CreateFolderHelper(folderName).AsAsyncOperation();
  4. }

And its helper class as:

  1. private async Task<string> CreateFolderHelper(string folderName)
  2. {
  3. try
  4. {
  5. StorageFolder folder = ApplicationData.Current.LocalFolder;
  6. await folder.CreateFolderAsync(folderName);
  7. return "success";
  8. }
  9. catch (Exception ex)
  10. {
  11. return "fail";
  12. }
  13. }

Complete code snippet for Service class is:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Windows.Foundation;
  7. using Windows.Storage;
  8. namespace WinRuntimes
  9. {
  10. public sealed class Service
  11. {
  12. #region Asynchronous Interface Methods
  13. public IAsyncOperation<string> CreateFolder(string folderName)
  14. {
  15. return CreateFolderHelper(folderName).AsAsyncOperation();
  16. }
  17. #endregion
  18. #region Helpers
  19. private async Task<string> CreateFolderHelper(string folderName)
  20. {
  21. try
  22. {
  23. StorageFolder folder = ApplicationData.Current.LocalFolder;
  24. await folder.CreateFolderAsync(folderName);
  25. return "success";
  26. }
  27. catch (Exception ex)
  28. {
  29. return "fail";
  30. }
  31. }
  32. #endregion
  33. }
  34. }

Step 4:

Add a script.js file and reference it in default.html:

Now in default.html, lets add two things:

Complete html code snippet for default.html is:
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>WinJSAsync</title>
  6. <!-- WinJS references -->
  7. <link href="//Microsoft.WinJS.2.0/css/ui-dark.css" rel="stylesheet" />
  8. <script src="//Microsoft.WinJS.2.0/js/base.js"></script>
  9. <script src="//Microsoft.WinJS.2.0/js/ui.js"></script>
  10. <!-- WinJSAsync references -->
  11. <link href="/css/default.css" rel="stylesheet" />
  12. <script src="/js/default.js"></script>
  13. <script src="js/script.js"></script>
  14. </head>
  15. <body>
  16. <p>Hello WinJS Async Operations</p>
  17. <input id="txtFolderName" placeholder="Enter folder name"/>
  18. <button onclick="createFolder()">Create Folder</button>
  19. </body>
  20. </html>

Step 5:

In script.js, we create a service object of Service class and call the createFolder function. Here we have used then function to achieve asynchronous programming. From the native part, we will get response either ‘success’ or ‘fail’. If ‘success’, we displayed a success message else display a failure message:

Complete code snippet for script.js is:

  1. var service = new WinRuntimes.Service();
  2. function createFolder() {
  3. var folderName = document.getElementById("txtFolderName").value;
  4. service.createFolder(folderName).then(function (response) {
  5. if (response == "success") {
  6. var msg = new Windows.UI.Popups.MessageDialog("Folder created successfully.", "Success");
  7. msg.showAsync();
  8. }
  9. else {
  10. var msg = new Windows.UI.Popups.MessageDialog("Failed creating folder.", "Fail");
  11. msg.showAsync();
  12. }
  13. });
  14. }

Step 6:

Run the application and enter folder name and click Create Folder button, you will get a success message dialog.

You can see the new folder just created in this directory:

This PC, C, Users, [Your User Name], AppData, Local, Packages > [App package name] > LocalState,

Read more articles on Universal Windows App: