Windows 8 How to Write to a File

by sunil ravulapalli /13. January 2012 20:17 /windows8 /Comments (0)

This code was tested on Windows 8 developer preview and may be outdated by the time future versions are released.

Following is the function which takes filename and content and writes content into the file.

 function saveStuff(fileName, contentText) {

        var sampleFile;

        // Create file in user's Document library, overwriting the existing file if there is one
        Windows.Storage.KnownFolders.documentsLibrary.createFileAsync(fileName,
         Windows.Storage.CreationCollisionOption.replaceExisting).then(function (file) {
            sampleFile = file;

            // Have file and text to write
            if (sampleFile != null && geoCode != null) {
                // Open file as stream with read/write access
                sampleFile.openAsync(Windows.Storage.FileAccessMode.readWrite).then(function (stream) {
                    // File opened

                    // Prepare and write to the file stream
                    var outputStream = stream.getOutputStreamAt(stream.size);
                    var writer = new Windows.Storage.Streams.DataWriter(outputStream);
                    writer.writeString(contentText);

                    // Save stream to file
                    writer.storeAsync().then(function () {
                        outputStream.flushAsync().then(function () {
                            writeContent('Geocode saved.');
                        });
                    });
                });
            }
        });
    }

You can call it as following:

saveStuff('settings.txt', 'blue');

Windows 8 How to Join Promises

by sunil ravulapalli /13. January 2012 19:53 /windows8 /Comments (0)

This code was tested on Windows 8 developer preview and may be outdated by the time future versions are released.

Currently I am using promises[0]._value.responseText to access by response. But it doesn't feel right at all. I could not yet find any documentation to do it correct way.

 var urls = [
                    "http://dev.virtualearth.net/REST/v1/Locations?q=07008&key=abcdef",
                    "http://dev.virtualearth.net/REST/v1/Locations?q=08830&key=abcdef"
                ];
        
 var promises = urls.map(function (url) {
            return WinJS.xhr({ url: url });
 });
    
 WinJS.Promise.join(promises).then(function () {
                contentPara.innerHTML += promises[0]._value.responseText+ '
'; contentPara.innerHTML += promises[1]._value.responseText+ '
'; }, handleError); function handleError() { writeContent('Error geocoding, please try again later.'); }

Windows 8 Fragment Navigation

by sunil ravulapalli /13. January 2012 17:57 /windows8 /Comments (0)

This code was tested on Windows 8 developer preview and may be outdated by the time future versions are released.

The contents of default.html are as show below. Just include a div called "contentHost".

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>TTips</title>
    <!-- WinJS references -->
    <link rel="stylesheet" href="/winjs/css/ui-dark.css" />
    <script src="winjs/js/base.js"></script>
    <script src="winjs/js/ui.js"></script>
    <script src="winjs/js/binding.js"></script>
    <script src="winjs/js/controls.js"></script>
    <script src="winjs/js/animations.js"></script>
    <script src="winjs/js/wwaapp.js"></script>
    <!-- TTips references -->
    <link rel="stylesheet" href="/css/default.css" />
    <script src="/js/default.js"></script>
</head>
<body>
    <h1>Traffic Tips</h1>
    <div id="contentHost">
        
    </div>
  
</body>
</html>

In default.js, include the navigated as it is. Don'f forget to include this line WinJS.Navigation.addEventListener('navigated', navigated); as shown. When onmainwindowactivated all you have to do is call WinJS.Navigation.navigate('main.html').

(function () {
    'use strict';
    // Uncomment the following line to enable first chance exceptions.
    // Debug.enableFirstChanceException(true);

    document.addEventListener("DOMContentLoaded", function () {
        WinJS.UI.processAll();
    }, false);

    WinJS.Application.onmainwindowactivated = function (e) {
        if (e.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.launch) {
                    WinJS.Navigation.navigate('main.html');
        }
    }

    function navigated(e) {
        WinJS.UI.Fragments.clone(e.detail.location, e.detail.state)
            .then(function (frag) {
            var host = document.getElementById('contentHost');
            host.innerHTML = '';
            host.appendChild(frag);
            document.body.focus();

            WinJS.Application.queueEvent({
                type: 'fragmentappended',
                location: e.detail.location,
                fragment: host,
                state : e.detail.state
                });

        });
    }

    WinJS.Navigation.addEventListener('navigated', navigated);
    WinJS.Application.start();
})();

Javascript - Loop through properties in an object(for .. in)

by sunil ravulapalli /4. January 2012 22:49 /javascript /Comments (0)

Quick Tip
If you want to loop through the properties in an object you can use the for..in loop construct.

var student = { 
    name:"tom",
    age:20
}; 

for (var key in student )
{
  document.write(key + ' = ' + student[key] + '
'); }

The above code will print:
name = tom
age = 20

Javascript - Variable without using the var keyword

by sunil ravulapalli /4. January 2012 22:39 /javascript /Comments (0)

Quick Tip
If you have a function like below where "temp" is declared and assigned without using the "var" keyword, it becomes a global variable!

function doSomething()
{
  temp=1;
}

Windows 8 App Bar using HTML

by sunil ravulapalli /29. December 2011 22:08 /windows8 /Comments (0)

This code was tested on Windows 8 developer preview and may be outdated by the time future versions are released.

Once you create a Blank solution your default.html should look like the code below.
The new parts are the scripts tags and the control div as highlighted.
The scripts files are needed for the App Bar control to work. The actual control is the div with the attribute data-win-control="WinJS.UI.AppBar".

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>MyAppBar</title>
    <!-- WinJS references -->
    <link rel="stylesheet" href="/winjs/css/ui-dark.css" />
    <script src="/winjs/js/base.js"></script>
    <script src="/winjs/js/wwaapp.js"></script>
    <script src="winjs/js/ui.js"></script>
    <script src="winjs/js/binding.js"></script>
    <script src="winjs/js/controls.js"></script>
    <script src="winjs/js/animations.js"></script>
    <!-- MyAppBar references -->
    <link rel="stylesheet" href="/css/default.css" />
    <script src="/js/default.js"></script>
</head>
<body>
     <div id="mybar" data-win-control="WinJS.UI.AppBar" data-win-options="{position:'bottom', transient:true, autoHide:0, lightDismiss:true}">
       <div class="win-right">
            <button class="win-command" id="settingsButton">
                <span style="background-image: url('images/smalllogo.png')" class="win-commandicon"></span>
                <span class="win-label">Settings</span>
            </button>
        </div>
    </div>
</body>
</html>

The default.js file should like below. The highlighted(6 to 8) lines are required for WinJS controls to work.

Lines 13 to 16 are used to wire up the button in the App Bar button with a function.

(function () {
    'use strict';
    // Uncomment the following line to enable first chance exceptions.
    // Debug.enableFirstChanceException(true);
 
    document.addEventListener("DOMContentLoaded", function () {
        WinJS.UI.processAll();
    }, false);
 
    WinJS.Application.onmainwindowactivated = function (e) {
        if (e.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.launch) {
            // TODO: startup code here
            var settingsButton = document.getElementById('settingsButton');
             settingsButton.addEventListener('click', function () {
              WinJS.Navigation.navigate('settings.html');
             });
        }
    }
 
    WinJS.Application.start();
})();

How to turn on logging on oracle client machine

by sunil ravulapalli /24. October 2011 15:44 /oracle /Comments (0)

Put this in your sqlnet.ora file

trace_level_client = 16
trace_unique_client = on
trace_file_client = sqlnet.trc
trace_directory_client = C:\oralogs

Much More Linq to Xml

by sunil ravulapalli /8. September 2011 17:52 /c# /Comments (0)
  
    100
    
    2
    
  

This is how the query looks:

WebReportData webReportData =
(
	from xml in data.Elements("xml")
	select new WebReportData
	{
		Distance = (from element in xml.Descendants("element") 
					where xml.Element("element").Attribute("name").Value == "distance" 
					select element).First().Value,
		Email = (from element in xml.Descendants("element") 
					 where xml.Element("element").Attribute("name").Value == "email" 
					 select element).First().Value,
		Education = (from element in xml.Descendants("element") 
					 where xml.Element("element").Attribute("name").Value == "education" 
					 select element).First().Value,
		Phone = (from element in xml.Descendants("element") 
					 where xml.Element("element").Attribute("name").Value == "phone" 
					 select element).First().Value
	}
).First();

The classes look like this:

public class WebReportData
{
	public string Distance { get; set; }
	public string Email { get; set; }
	public string Education { get; set; }
	public string Phone { get; set; }
}

More Linq to Xml

by sunil ravulapalli /31. August 2011 16:35 /c# /Comments (0)

  
    1
    
      
        mr
      
      
        mr2
      
    
  

  
    2
    
      
        mr
      
    
  

  
    3
    
    
  


This is how the query looks:

IEnumerable people =
from person in data.Elements("person")
select new Person
{
	id = (int)person.Element("id"),
	positions = (
				from position in person.Elements("positions").Elements("position")
				select new Position { title = position.Element("title").Value } 
			 ).ToList()
};

The classes look like this:

public class Person
{
	public int id { get; set; }
	public List positions { get; set; }
}

public class Position
{
	public string title { get; set; }
}

Shelfari

by sunil ravulapalli /30. August 2011 18:30 /books /Comments (0)