A WinRT component can be used to include some common code along with embedded resources that can be reused in a Windows Store app irrespective of the language in which it is developed, like C#, JavaScript etc.
In this article, I explain how to read a WinRT component embedded resource file from a JavaScript Windows Store app.
This article consists of the following 3 parts:
- Reading a Text file.
- Reading a Binary file.
- Reading an Image as Base64 encoded string.
Part 1: Reading a text File
Create a WinRT component
- Create a new Windows Store apps class library or WinRT component say with the name "ExampleLib".

- If it is a class library then open its properties and change the output to WinRT component.

- Add a text file named say "Data.txt" in the project root folder with some text and change to embedded resource from its properties.

- Add a new C# code file ResHelper.cs in the project root folder and add the following code in that.
using System;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using Windows.Foundation;
namespace ExampleLib
{
public sealed class ResHelper
{
static async Task<string> ReadResourceAsync(Assembly assembly, string resource)
{
string xml;
using (var stream = assembly.GetManifestResourceStream(resource))
{
using (var reader = new StreamReader(stream))
{
xml = await reader.ReadToEndAsync();
}
}
return xml;
}
public static IAsyncOperation<string> GetFileText(string fileName)
{
return (ReadResourceAsync(typeof(ResHelper).GetTypeInfo().Assembly, fileName)).AsAsyncOperation<string>();
}
}
}
Create a Windows Store app in JavaScript
Use the following to create a Windows Store app in JavaScript:
- Create a new Windows Store app under language JavaScript (blank app is fine) say with name "ExampleApp"
- Add a reference to ExampleLib
- Open Js/default.js and add the following code after line args.setPromise in the activated handler.
|
The complete solution looks like as below:
All done, deploy and run the app, now you'll see the embedded resource content as your main page content. Try with an invalid file name; you'll see an error message.
Part 2: Reading a binary file
Part 1 explains how to read a Winrt component embedded text file. The ResHelper example method GetFileText always returns a string irrespective of the file type. This will not work for binary and image files since they must be managed as bytes. In this part, I'll explain how to send a byte array back to the JavaScript app.
Modify WinRT component ExampleLib
1. Add any binary file ( I've added an image file StoreLogo.png).
2. Change to Embedded Resource from its properties
3. Add the following extra methods in the ResHelper class to return bytes instead of a string.
|
Modify Windows Store JavaScript app ExampleApp
1. Add the following extra code at the end inside the activated handler in the Js/default.js file
|
Deploy and run the app, it'll read StoreLogo.png from the WinRT component and save it in the store app's local folder. The local folder path will be displayed in the textarea. Copy the path and open Windows Explorer, verify the new image.
Part 3: Reading an Image as Base64 encoded string
In this part, I'll explain how to read a base64 encoded string of a WinRT component image resource and display it in the page on the fly.
1. Modify WinRT component project ExampleLib
1. Add the following extra 2 methods in ResHelper.cs that converts a byte array to a Base64 encoded string.
|
2. Modify Windows Store JavaScript project ExampleApp
1. Add the following code at the end inside the activated handler in the Js/default.js file.
|
Deploy and run the app, you'll see the StoreLogo.png image rendered on the fly in the main page.
Example solution attached.

Dinesh BeniwalPosted Apr 18, 2013, 6:56 AM
Good work Raj
Former memberPosted Apr 17, 2013, 10:22 PM
Very nice article !
Mahesh ChandPosted Apr 17, 2013, 10:17 PM
Welcome to C# Corner, Raj!