Oct
12
2014

Universal Windows Apps with Angular

Starting with Visual Studio 2013 (with update 2), developers can create an app with a single solution to target multiple devices such as PCs, tablets, or phones.  This type of solution can generate universal Windows apps.  These can be written with C#, C++, or JavaScript. For video training on the topic, please check out the free course at the Microsoft Virtual Academy.

If written with JavaScript, web developers who use Angular will be happy to know you can continue to do so while making apps.

For a jumpstart on the development process, I have created a starter solution with a bare-bones shell that properly sets up Angular for use in the project.  The starter solution is named AngularVS (Angular for Visual Studio) and is available publicly at github.

A key factor for Angular to work properly in these types of apps is the use of the the ng-csp directive as seen below.  Not only will this enable Angular to work smoothly, if declared before other popular libraries|frameworks such as jQuery, it helps enable them safely too!

ngcsp

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

Microsoft DevRadio: Developing for Device Features

To play video, hover above and press play button.

Abstract: win8genapp30
Ever wonder how you can develop for device features you really like even though your personal device doesn’t support it? Well in this episode, Michael Palermo shows you the power of the Simulator in Visual Studio and how you can test features for various devices.

Next Steps:
Step #1 – Download Windows 8 and Windows 8 SDK
Step #2 – Download Visual Studio Express for Windows 8
Step #3 – Start building your own 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:

Developing for Windows 8 in 1/2 the time!

    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
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
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).
Mar
29
2012

Unable to Activate Windows Metro Style Application

If you receive the following error in Visual Studio…

“Unable to activate Windows Metro style application”

… or a similar error in Blend…

“Unable to activate application --- on the Local Machine”

… the quickest fix is to delete the contents of the bin directory and rebuild the project.

    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