Playing Youtube video in a Windows 8 Application

Recently, I was working on a C# Windows 8 application that was required to play Youtube videos as one of the features. At first I wasn’t sure if there was a simple way to accomplish the task, but with a little research I found a very nice solution.


First I created a very simple WebView in my *.xaml page:

   1: <WebView x:Name="webView" />


You’ll notice I haven’t specified any dimensions or any other attributes other than a name. I’ll use the name in my *.xaml.cs file to reference the WebView instance.


Now in my *.xaml.cs file, I simply set the contents of the WebView to an iframe that contains the embedded youtube video. The size of the iframe will define the size of my WebView:

   1: string html = @"<iframe width=""800"" height=""480"" src=""" + videoID + @"?rel=0"" frameborder=""0"" allowfullscreen></iframe>";
   3: this.webView.NavigateToString(html);


videoID is a string containing the ID of the video you want. It’s the string that comes after the v=  and before the & in the video URL:




The result is a youtube video embedded in your application with the familiar controls:




Comments (5)

  1. Krzysztof says:

    Hi, I was looking for this solution! Very very thanks:)

  2. Jason says:

    I used a library i found on codeplex called mytoolkit.  It is also available as a nuget package which is how i ended up installing it.  You pass in the videoid and it returns you the url of the mp4 video which i then play on with the mediaplayer framework.  Of course this violates youtube's terms of service.  the project can be found at this url

  3. mohamed eltohamy says:

    i have error in videoID the error be . (the name 'videoID' does not exist in the current context)

  4. Majdi says:

    @Mohamed , u must create a String like this "String videoID="TheVlueOfYouVideoID" ;

  5. manustarfoot says:

    Not for a protected video ! how do for protected video ?