In .NET application, to retrieve the user’s current culture is pretty simple using the CultureInfo class. But performing the same thing, in particular using Javascript, is a bit more complex.

So here is a quick code sample to perform this task:

image

As you can see, we just need to access to the languages property of the GlobalizationPreferences class. This property is an array containing all the cultures installed and used on the machine.

Here is the results of the previous call:

image

Simple but very useful ! To see more content about localization, check here: http://msdn.microsoft.com/en-us/library/windows/apps/hh465006.aspx

 

Happy coding!

Today, I’m pleased to announce that a new version of YouTube8, my first Windows 8 application, is available on the Windows Store.

This version is a major update that include a lot of new features and improvements:

  1. No more limitation on video playback: all videos can be played within the application
  2. Full Screen support
  3. PlayTo feature
  4. Multi-resolution (when they are more than 1 resolution available)
  5. Search improvements
  6. And more !

 

Feel free to try it and to share what you think about it ! http://bit.ly/MezRoj

 

Happy testing!

On my spare time, I’ve started a few weeks ago to work on a small Windows 8 application that allow users to look for YouTube videos and play them directly in the application.

I’m proud to announce that the application is now live on the Microsoft Store and any Windows 8 Release Preview users can download it and play with it !

For now, there are some issues with some videos that cannot be played directly in the app but I’ll try to improve this part (and add more features) in the next version !

Here are some screenshots:

Capture d’écran 1

Capture d’écran 2

Capture d’écran 3

Capture d’écran 4

I hope you’ll enjoy it !

C# 5, with it’s async/await pattern, is extremelly useful for asynchronous development. In Windows 8, the good point is that all the APIs which take more than 50ms must be run asynchronously !

But how do you create your own WinRT component ? This might be simple but there are some tricky points that I wanted to highlight in this blog post.

We know that each WinRT components need to be a sealed class so let’s take a look at the following code:

public sealed class Reader

{

    public async static Task<IEnumerable<string>> GetItemsAsync()

    {

        var feedsTitle = new List<string>();

        var client = new SyndicationClient();

        var feeds = await client.RetrieveFeedAsync(new Uri(“http://channel9.msdn.com/coding4fun/articles/RSS”));

        foreach (var item in feeds.Items)

        {

            feedsTitle.Add(item.Title.Text);

        }

        return feedsTitle.AsEnumerable();

    }

}

Even if this code seems correct, it’ll fail to compile due to the following error (click on image to enlarge):

image

The problem here is that each type exposed via your component need to be projected using WinRT. And the compiler is not able to project the Task type.

So we need to create a wrapper that will return (and take in parameters) only types that can be projected. And, for asynchronous operations, the dedicated type is IAsyncOperation<T>, which can be obtained, from a Task object, using the AsAsyncOperation extension’s method:

public sealed class Reader

{

    public static IAsyncOperation<IEnumerable<string>> GetItemsAsync()

    {

        return InternalGetItemsAsync().AsAsyncOperation();

    }

    internal async static Task<IEnumerable<string>> InternalGetItemsAsync()

    {

        var feedsTitle = new List<string>();

        var client = new SyndicationClient();

        var feeds = await client.RetrieveFeedAsync(new Uri(“http://channel9.msdn.com/coding4fun/articles/RSS”));

        foreach (var item in feeds.Items)

        {

            feedsTitle.Add(item.Title.Text);

        }

        return feedsTitle.AsEnumerable();

    }

}

(Note that the method “InternalGetItemsAsync” is set to “internal” to not be exposed).

Now, if you compile, no errors are produced. Plus, the compiler generate a code that can be called asynchronously:

public class Test

{

    public async void GetItemsAsync()

    {

        var items = await Reader.GetItemsAsync();

    }

}

(Here is the C# version, using the async/await keywords)

WinRTLib.Reader.getItemsAsync().then(function (feedsTitle) {

    var feeds = feedsTitle;

});

(Here is the WinJS version, using the Promise)

As you can see, this is pretty simple but extremely useful as soon as you are aware of this little trick: “A WinRT component can only exposed WinRT types so they could be projected !”

 

More info: http://msdn.microsoft.com/en-us/library/windows/apps/br230301(v=vs.110).aspx

 

Happy coding!

 

Most of the sample/blog posts that you can found on Internet show you how to load a file that is contained in local storage. But what do you need to do if you want to access a file that is marked as “Content” in Visual Studio ?

The trick is to access to the files/folder are installed with your application. And to do this, you need to use Windows.ApplicationModel.Package.Current.InstalledLocation:

var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(@”Assets\Test\Test.txt”);

Note the syntax used to access the file (no need to use ms-appx or something else).

Pretty simple isn’t it ? :)

Be careful: I’ve found a bug in the way Windows 8 handle file path with “\” and “@” characters. Indeed, “@” is usually used to escape the “\” character thus, the previous syntax works fine. But don’t try to duplicate “\” character when using “@” or you’ll get an exception. So the following code will not work:

var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(@”Assets\\Test\\Test.txt”);

 

I hope this bug will be fix in the Release Preview !

 

Happy coding!

Here is a simple code sample that show you how you can read the content of a ZIP file:

var localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;

var file = await localFolder.GetFileAsync(“Test.zip”);

var randomStream = await file.OpenReadAsync();

 

using (Stream stream = randomStream.AsStreamForRead())

{

    var zipArchive = new System.IO.Compression.ZipArchive(stream);

 

    // Entries contains all the files in the ZIP

    foreach (var entry in zipArchive.Entries)

    {

        using (var entryStream = entry.Open())

        {

            //

            // Read string file content

            //

            //using (var streamReader = new StreamReader(entryStream))

            //{

            //    var result = await streamReader.ReadToEndAsync();

            //}

 

            //

            // Read string file content (using WinRT APIs)

            //

            using (var inputStream = entryStream.AsInputStream())

            {

                 using (DataReader reader = new DataReader(inputStream))

                 {

                     var fileSize = await reader.LoadAsync((uint)entryStream.Length);

                     var stringContent = reader.ReadString(fileSize);

 

                     // You can also use the other methods available on DataReader to load an IBuffer, Byte Array, etc.

                 }

            }

        }

    }

}

 

Have fun and happy coding!