Copyright © Microsoft Corporation. All rights reserved.
    The code provided in this post is licensed under the Microsoft Public License (Ms-PL).
Aug
24
2012

How to Use iFrames in WinRT Apps

SNAGHTML25abaef5Did you know you could use iFrames in WinRT apps developed using JavaScript?  Is this suppose to be exciting?  Why would you care?  For the answers, follow along as I take a journey of discovery attempting to add live Twitter feeds to my app.

To set the stage for what I want to do, you must first understand what a Twitter Widget is.  The folks at Twitter have made it real easy for web developers to create a custom, dynamic section of a web site to display tweets based on a user, a search, favorites, or a list.  Once I know my tweet criteria, I can customize the appearance, dimensions, and other relevant options.  When I am done with all my customizations, I can grab the code that will make the magic happen on my web site.  Um… but I want this in my WinRT app.  Will it work?  Lets find out.

I will choose to create a Twitter Search Widget.  Here is a screen capture of my criteria:

SNAGHTML25bbc55e

Once I click the [Finish & Grab Code] button, I see the following:

SNAGHTML25be0be3

Now I will add this code to my project in Visual Studio 2012.  I have just created this project using a blank template. In the default.html file, I will add the Twitter widget code I created above.  Here is what my markup looks like:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>HowTo_IFrames</title>

    <!-- WinJS references -->
    <link href="//Microsoft.WinJS.1.0/css/ui-dark.css" rel="stylesheet" />
    <script src="//Microsoft.WinJS.1.0/js/base.js"></script>
    <script src="//Microsoft.WinJS.1.0/js/ui.js"></script>

    <!-- HowTo_IFrames references -->
    <link href="/css/default.css" rel="stylesheet" />
    <script src="/js/default.js"></script>
</head>
<body>
    <p>Content goes here</p>
    <script charset="utf-8" 
            src="http://widgets.twimg.com/j/2/widget.js"></script>
    <script>
            new TWTR.Widget({
            version: 2,
            type: 'search',
            search: '#win8appdev',
            interval: 30000,
            title: 'Windows 8 Developers',
            subject: '#win8appdev',
            width: 250,
            height: 300,
            theme: {
                shell: {
                    background: '#8ec1da',
                    color: '#ffffff'
                },
                tweets: {
                    background: '#ffffff',
                    color: '#444444',
                    links: '#1985b5'
                }
            },
            features: {
                scrollbar: false,
                loop: true,
                live: true,
                behavior: 'default'
            }
        }).render().start();
    </script>
</body>
</html>

Will it be that simple?  Unfortunately no.  When I attempt to run the application, I get the following error:

SNAGHTML25cae34d

It may not be obvious by the error message what the real issue is.  The reason why ‘TWTR’ is undefined is due to the following script not being executed:

<script charset="utf-8"             
        src="http://widgets.twimg.com/j/2/widget.js"></script>

Why did the script not execute?  Because my default.html page is considered local in context to my application.  The only way an external script would be allowed to execute is if it was executed in a web context.  Well who decided those rules?  Perhaps a better question for now is – How do I know if I am in local context or web context?

Any HTML file that is physically part of a project is considered as local in context. The converse to this is any HTML that resides externally or is remote to a project is considered to be in web context.  How can I execute HTML remotely in my WinRT app?

Using iFrames

I can use an iframe in my HTML to point to an external web page.  To demonstrate, I will remove the script I added to my default.html page (we will return to that code later), and replace the contents of the body tag with the following:

<h1>iFrame Demo</h1>
<iframe src="http://www.palermo4.com"
        width="900"
        height="600">
</iframe>

When I run my application now, this is what I see:

image

If this is all I do, I essentially have a “browser” to my site within my app.  I could also change the source of the iFrame from anchor tags as seen with the following revisions:

<h1>iFrame Demo</h1>
<div>
    <a href="http://palermo4.com" target="framed">Palermo4</a> 
    <a href="http://codefoster.com" target="framed">Codefoster</a>
</div>
<iframe name="framed" width="900" height="600">
</iframe>

This will cause the iFrame to load the respective href value once either of the anchor tags are clicked.

Recommended:  Create an HTML file in the root directory of your project named
msapp-error.html.  This file will be loaded automatically in the iframe when errors occur due source resources not loading or not found!

Now returning to my original objective, I would like to see the code I grabbed from Twitter work in my app still.  I have an idea!  I will take Twitter widget code and put in in a new HTML file in my project.  I will name the file twitterframe.html, and create it in the root project directory.  Here is the contents of that file:

<!DOCTYPE html>
<html>
    <head>
        <title></title>
    </head>
    <body>
        <script charset="utf-8" 
                src="http://widgets.twimg.com/j/2/widget.js">
        </script>
    <script> 
        new TWTR.Widget({
            version: 2,
            type: 'search',
            search: '#win8appdev',
            interval: 30000,
            title: 'Windows 8 Developers',
            subject: '#win8appdev',
            width: 250,
            height: 300,
            theme: {
                shell: {
                    background: '#8ec1da',
                    color: '#ffffff'
                },
                tweets: {
                    background: '#ffffff',
                    color: '#444444',
                    links: '#1985b5'
                }
            },
            features: {
                scrollbar: false,
                loop: true,
                live: true,
                behavior: 'default'
            }
        }).render().start();
    </script>
    </body>
</html>

Now I will return to default.html, and modify the contents within the body tags as follows:

<h1>iFrame Demo</h1>
<iframe src="/twitterframe.html" width="300" height="400">
</iframe>

Will I get my desired output?  Can I trick the iframe to run a page in local context but treat it like it is in web context?  Not the way I am doing it.  When I run my application, I get the same error I received earlier.  However, I was on the right track of thinking.  By using a special moniker preceding the URL, I can ask for the local page to be executed in a web context.  Here is the syntax for that:

<iframe src="ms-appx-web:///twitterframe.html" 
        width="300" height="400">
</iframe>

By adding ms-appx-web:/// before my local page name, I am informing my application to run it in a web context. This crucial step gives me exactly what I want, as seen here:

image

Hooray!  I got my Twitter search widget to work in my WinRT app!  Think of the possibilities with any other social meshing sites or mapping tools!

By the way, you can govern what is allowed to happen in the iFrame by setting the sandbox attribute.  IntelliSense reveals self-describing features:

SNAGHTML261ce5af

I hope you enjoy developing Windows 8 applications with JavaScript and HTML5!  For more resources, be sure to sign up for Generation App! Your idea. Your app. 30 days.

Cheers!

    Copyright © Microsoft Corporation. All rights reserved.
    The code provided in this post is licensed under the Microsoft Public License (Ms-PL).
    Copyright © Microsoft Corporation. All rights reserved.
    The code provided in this post is licensed under the Microsoft Public License (Ms-PL).
Aug
16
2012

Microsoft DevRadio: Adding HTML5 Content

To play video, hover above and press play button.

Abstract: win8genapp30
Michael Palermo is back for today’s show as he shows us how to add simple HTML5 content in Visual Studio  to develop an app for Windows 8.

Next Steps:
Step #1 – Download Windows 8 Release Preview and Windows 8 SDK
Step #2 – Download Visual Studio Express for Windows 8
Step #3 – Start building your own Metro Style Apps for Windows 8

Subscribe to our podcast via iTunes, Zune, or RSS

If you're interested in learning more about the products or solutions discussed in this episode, click on any of the below links for free, in-depth information:

Websites:

Blogs & Articles:

Videos:

Virtual Labs:

    Copyright © Microsoft Corporation. All rights reserved.
    The code provided in this post is licensed under the Microsoft Public License (Ms-PL).
Aug
8
2012

Microsoft DevRadio: Using a Blank Template

To play video, hover above and press play button.

Abstract: win8genapp30
Michael Palermo is back for today’s show as he shows us how to use a Blank Template in Visual Studio  to develop an app for Windows 8.

Next Steps:
Step #1 – Download Windows 8 Release Preview and Windows 8 SDK
Step #2 – Download Visual Studio Express for Windows 8
Step #3 – Start building your own Metro Style Apps for Windows 8

Subscribe to our podcast via iTunes, Zune, or RSS

If you're interested in learning more about the products or solutions discussed in this episode, click on any of the below links for free, in-depth information:

Websites:

Blogs & Articles:

Videos:

Virtual Labs:

    Copyright © Microsoft Corporation. All rights reserved.
    The code provided in this post is licensed under the Microsoft Public License (Ms-PL).
Jul
27
2012

This is FoxIE!

foxieWhat happens when you get the brainpower behind Mozilla Firefox and Microsoft IE together?  You get something very FoxIE!  I value this type of discussion going on at this new series on debuting on Channel 9.  If you have anything to do with web development, you should pay a visit!

    Copyright © Microsoft Corporation. All rights reserved.
    The code provided in this post is licensed under the Microsoft Public License (Ms-PL).
Jul
25
2012

Windows 8 in Half the Time–Getting Started

To play video, hover above and press play button.

Abstract: win8genapp30
Developer Evangelist, Michael Palermo kicks off his new series today where he shows us how we can develop for Windows 8 in under 4 minutes or less. Today’s topic? How to get started developing your Windows 8 app by downloading and using Visual Studio Express

Next Steps:
Step #1 – Download Windows 8 Release Preview and Windows 8 SDK
Step #2 – Download Visual Studio Express for Windows 8
Step #3 – Start building your own Metro Style Apps for Windows 8

If you're interested in learning more about the products or solutions discussed in this episode, click on any of the below links for free, in-depth information:

Websites:

Blogs & Articles:

Videos:

Virtual Labs:

Downloads:

    Copyright © Microsoft Corporation. All rights reserved.
    The code provided in this post is licensed under the Microsoft Public License (Ms-PL).
Jul
20
2012

Launch Your Windows 8 App in 30 Days

win8genapp30The release of Windows 8 is on the horizon, and soon millions of people will be looking for Windows 8 apps. Now’s the time to make sure your app is ready for them.

Begin your 30-day journey to create a great Windows 8 Metro style app. Get started at www.generationapp.com and receive:

· Insider tips and tricks on Windows 8 application development.

· Personal on-the-phone access to a Windows 8 architect*.

· An exclusive one-on-one Metro style design consultation*.

· An opportunity to get expert help from a Microsoft Services Engineer at an App Excellence Lab.

Your new Windows 8 app could be mere days away.

* Offer good only to legal residents in the 50 United States & D.C., age 18 or older to hobbyists, professionals or developers in the field of software tech who sign up for building a Windows 8 application on www.generationapp.com. Offer limited to 250 design consultations per month and 500 technical review consultations per month, on a first come first served basis. Limit of one session of each offer type per person. This offer is non-transferable and cannot be combined with any other offer. This offer ends when supplies are exhausted, and is not redeemable for cash.

    Copyright © Microsoft Corporation. All rights reserved.
    The code provided in this post is licensed under the Microsoft Public License (Ms-PL).
Jul
13
2012

Code Samples for JavaScript Metro Style Apps

What do most developers want when they need to learn something new?  Most of us want code samples, and feature-targeted ones at that.  Thank goodness you will not have to endlessly search  for Metro style app samples via JavaScript.  If you have downloaded a flavor of Visual Studio 2012, the samples are closer than you may have expected.

Update!

Be sure to checkout the developer center download page for more samples!

The “New Project” dialog box in Visual Studio provides an option to easily access samples as seen in the following screen capture:

javascript-metro-samples

Underneath the “Online” option on the left side of the box, there is a “Samples” option with choice of language. In this example, I chose JavaScript.  Notice the first glimpse of app features just waiting for you to choose. 

Upon selecting a sample, you will get the following dialog box with standard disclaimers:

splash-sample-install

After clicking the “Install” button, the sample you selected will be loaded into solution explorer.  A description page (downloaded as a solution item) will be displayed as seen here:

splash-desc

The sample is ready to go!  You can poke around (code is pampered with comments) or run it immediately.

The main message here is this – before you go searching the web all over for that “how to” sample, you might be pleasantly surprised to see it is already available via Visual Studio 2012!

    Copyright © Microsoft Corporation. All rights reserved.
    The code provided in this post is licensed under the Microsoft Public License (Ms-PL).
    Copyright © Microsoft Corporation. All rights reserved.
    The code provided in this post is licensed under the Microsoft Public License (Ms-PL).

Resources

Archives

Team Blogs

Download OPML file OPML