b-rad blog
adventures in technology
adventures in technology
Feb 13th
Using the Google Android emulator is a good way to test how a website behaves on Android devices.
http://10.0.2.2 is the URL to connect to a website hosted on your local computer. localhost or 127.0.0.1 will not work! This is all defined in the documentation on Android emulator networking.
The Android emulator will not make use of your local hosts file. This is unfortunate when your website relies on host headers to work correctly (eg. SharePoint). What you need to do is edit the hosts file on the Android emulator image itself.
emulator -avd myAvdNameHere -partition-size 128
Located in C:\Program Files (x86)\Android\android-sdk-windows\tools\ on Windows 64bit
Note
The partition-size parameter is needed to expand the image size to prevent an error in step 5. If you try and perform these steps after starting the emulator from the UI you will receive the following error:
failed to copy ‘c:\temp\hosts’ to ‘/system/etc/hosts’: Out of memory
adb remount
Located in C:\Program Files (x86)\Android\android-sdk-windows\platform-tools\ on Windows 64bit
adb pull /system/etc/hosts c:\temp
127.0.0.1 localhost 10.0.2.2 mytesthost
adb push c:\temp\hosts /system/etc
You can now browse to your entered hostname in the Android browser.

Jan 26th
Recently I used a site collection scoped feature to add jQuery support to a SharePoint 2010 site collection. As documented on countless blogs, I used custom action XML similar to the following:
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<CustomAction
ScriptSrc="JqueryCustomAction/jquery-1.4.2.min.js"
Location="ScriptLink"
Sequence="100">
</CustomAction>
</Elements>
Everything seemed to work as intended. Only later did I notice strange JavaScript errors in the SharePoint interface. The most common errors were in a file called cmssitemanager.js when using the Asset Picker dialog as part of a publishing site:
It turns out that SharePoint 2010 has JavaScript that registers a global function named “$”. This conflicts with the same global function which is registered by jQuery and is used as shorthand for the various jQuery functions.
My very quick solution was to reference a second JavaScript file in my custom action. This JavaScript file disables the “$” shorthand function for jQuery. This fixes all the compatibility issues with the only downside being that you now have to replace “$” with “jQuery” when referring to jQuery globally.
Updated script references:
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<CustomAction
ScriptSrc="JqueryCustomAction/jquery-1.4.2.min.js"
Location="ScriptLink"
Sequence="100">
</CustomAction>
<CustomAction
ScriptSrc="JqueryCustomAction/jquery-noconflict.js"
Location="ScriptLink"
Sequence="105">
</CustomAction>
</Elements>
No conflict JavaScript:
// this puts jquery into no conflict mode which will remedy conflicts caused by jQuery when used // with certan publishing features jQuery.noConflict();
Below is a minimal Visual Studio 2010 solution that contains a site collection scoped jQuery feature.
May 6th
I was lucky enough to get an iPad recently. I can truthfully say that it is my best ever gadget purchase. The iPad has not been designed as a general purpose computer and that is its biggest strength. It excels at web browsing, audio/video playback, as a book reader and at gaming. Below I’ll cover some of the apps I’ve been using to help me perform these tasks.
Safari is still the best mobile web browser I have used. The iPad version is basically the same as that of the iPhone. There are some tweaks to make better use of the screen real estate. This is most noticeable in the “tab” view for viewing multiple pages simultaneously. Although Flash is a noticeable absence from the iPad, I have not found it to be much of a problem. All of the video sites I frequent (YouTube, Vimeo, Funny or Die) support either HTML 5 or iPad compatible video. This results in a far better experience than even Flash video on the desktop. A simple pinch zoom brings an embedded video to full screen. It’s great.
It isn’t all good though. I am often left waiting for sections of a page to render even if it has already been recently rendered. This occurs when you are panning around a large website. This may be related to the small amount of memory given to the iPad (256MB). I also find the iPad redownloading a lot of content that you might expect to be cached. This often occurs when you navigate back and may also be due to the small amount of memory.
My media setup is made up of a Thecus NAS with TwonkyMedia server serving to my PS3. This works great for all of the many media formats that can be natively played by the PS3. For other files I use the fantastic PS3 Media Server which serves the same files on my NAS but adds a seamless transcoding function.
I wanted this same experience on my iPad so that it could act as another “screen” for local content. I first investigated DLNA/UPNP clients which would allow me to browse my Twonky server. There are a few of these but the main limiting factor is the iPad and its inability to play content in anything other than its preferred format. After some brief investigations into PS3 Media Server support for iPad, I eventually turned to another iPad app and matching software called Air Video. This works in a similar way to PS3 Media Server by providing a seamless transcoding experience especially for the iPhone and iPad. It isn’t free and it isn’t UPNP compatible but it has successfully transcoded every video I have thrown at it thus far.
I was already a Kindle owner, so for me the Amazon Kindle application for iPad was a must. This app seamlessly turns your iPad into another Kindle compatible device. All of your books will automatically appear on your iPad just as they did on your Kindle. The iPad is a solid book reader. For large format books or for books with detailed illustrations the iPad is far better than my Kindle (international, non DX). For reading basic novels I prefer the smaller and lighter Kindle. One thing I did notice was that I seemed to get less drowsy when reading from the iPad versus the Kindle. This can be good in some situations but is obviously not preferable before bedtime. Another issue which became apparent was that the iPad provided unlimited sources of distraction from the books I was attempting to read.
I use this reasonably priced piece of software to remotely control my Windows 7 machines via RDP. This software is simple to use and quite fast over my wireless-G network. The good thing about this application when compared with others is that it allows for direct interaction. By this I mean that to click a button you simply touch it. Other RDP/VNC tools first make you move the mouse cursor to a position before you can click it. One example of this iTeleport.
This is a rather pricey app that can be used for accessing your computers via VNC. The creators of this app also provide a number of additional OSX/Windows tools for helping you configure your firewall settings, port forwarding/UPNP. These may be useful for beginners but I found they just got in the way. As expected VNC was not as fast as RDP on a Windows machine. Also, the non-direct way of interacting with the host system (via the movement of a virtual mouse pointer) was quite annoying.
iNet provides a number of useful networking tools. This application is actually an iPhone application. I can put up with the iPad’s awful 2x scaling of iPhone apps because i use the wake on LAN function of this app to turn on my computers from my couch.
Although ridiculously priced, this app should be good. There a number of other sports apps in the AppStore that are designed to augment your TV viewing experience. As my sport of choice is F1 I coughed up the money for this app which should provide live timing and track position during practice sessions, qualifying and grand prixs. My first use should be at next week’s Spanish grand prix.
This game will appeal to anyone who likes Diablo or Torchlight. This game has all of the right ingredients and is fun to play but it is let down by its horrible leveling/balancing. It gets easier as you progress through the game to the point that you don’t really feel the need for new gear or attributes. I hope someone can fix this as the iPad is awesome for this style of game.
This is my favorite racer on the iPhone and it’s even better on the iPad. Although it pretends to be a simulation style racer, the car dynamics are far from realistic. This is probably for the best given the limited control inputs available. Heaps of tracks and great graphics make this game worthy of its premium pricing.
This is a fun arcade shooter with a great visual style and even better comical sound effects. I recommend it.
Tower defense games are brilliant on iPad. Field runners is one of the best. It has great graphics and is highly addictive.
This game is amazingly simple. You move either left or right and tap as fast as you can to shoot the never-ending herd of zombies that are trying to eat your brains. Great artwork and well priced.
This game is pretty much the same as it is on every other platform. It still provides some pretty intense arcade style fun. The virtual analogue controllers mean you won’t be as accurate as you would be on other platforms. Still this is worh the money especially if you are yet to try this great game.
This is the iPad version of the best selling iPhone game. In this game you direct planes and helicopters into runways and helipads in an attempt to avert inevitable fiery disasters. In this version they have added some splitscreen multiplayer modes and 3D?!? This game spawned a host of other “path drawing” games and is probably still the best.
This is another very popular game which has come from the iPhone. In it you shoot angry birds at mean pigs in all kinds of breakable environments. I don’t think the iPad has added anything to the enjoyment of this game but it was pretty fun to start with so that’s ok.
So, are there any other apps I should be using? I have omitted the Apple provided apps like iBooks and iWork as these are not yet available to Australians. In fact the iPad AppStore app itself is unavailable. For the moment i am forced to get all my apps through iTunes and then sync via USB. It’s a good thing the iPad is an incredible device because iTunes is rubbish.
Feb 16th
Microsoft announced Windows Phone 7 today. It looks pretty amazing. The only negative point is that it won’t be out until the fourth quarter of 2010.
Below are my notes from the launch:
More information:
http://channel9.msdn.com/posts/LauraFoy/First-Look-Windows-Phone-7-Series-Hands-on-Demo/
Feb 6th
Generating printable documents or reports containing dynamic data is a common requirement of most business systems. In this post I will show you how to create XPS documents dynamically using WPF templates loaded from the file system and populated with data binding. This technique is especially powerful when the output document needs to be either highly dynamic or has lots of design elements. The example document we will create looks something like this:
For a fixed format document the <FixedDocument/> is a logical starting point. The template needed to make our document is as follows:
<FixedDocument
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xml:lang="en-au">
<FixedDocument.Resources>
<BooleanToVisibilityConverter x:Key="visConverter"/>
</FixedDocument.Resources>
<PageContent>
<FixedPage Width="793.76" Height="1122.56">
<!-- Page 1 Begins Here -->
<StackPanel Margin="50">
<Border BorderThickness="5" BorderBrush="Gray" CornerRadius="10"
Padding="20" Width="690">
<StackPanel Orientation="Horizontal" Margin="0 0 0 0">
<Image Source="truck.jpg" HorizontalAlignment="Left"/>
<TextBlock Margin="30 0 0 0" FontSize="50"
Text="{Binding Path=Heading}" VerticalAlignment="Center"/>
</StackPanel>
</Border>
<TextBlock Margin="0 30 0 0"
Text="{Binding Path=CurrentDate, StringFormat='{}{0:d MMMM yyyy}'}"/>
<TextBlock Margin="0 30 0 0"
Text="{Binding Path=Name, StringFormat='Dear {0},'}" />
<ItemsControl Margin="30 20 0 0" ItemsSource="{Binding Path=DotPoints}"
HorizontalAlignment="Left">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,8,0,0" Orientation="Horizontal" >
<TextBlock Text="• " />
<TextBlock Text="{Binding}" TextWrapping="Wrap" Width="400"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<TextBlock Margin="0 30 0 0"
Text="Congratulations, you are entitled to a 50% discount!"
Visibility="{Binding Path=GiveDiscount,
Converter={StaticResource visConverter}}"/>
</StackPanel>
</FixedPage>
</PageContent>
<PageContent>
<FixedPage Width="793.76" Height="1122.56">
<!-- Page 2 Begins Here -->
<TextBlock Margin="50" Text="Nothing to see here."/>
</FixedPage>
</PageContent>
</FixedDocument>
This includes the namespace imports that should be familiar to any WPF developer. One thing of interest in this template is the hard coded width and height of the <FixedPage/>. These values have specified an A4 page in portrait orientation. These have been based on the DPI of WPF (96) and the dimensions of an A4 page.
The XAML template includes standard WPF binding expressions which will be used to populate the document with dynamic data. It also makes use of the BooleanToVisibilityConverter to make easy show/hide functionality through data binding.
System.Windows.Markup.XamlReader provides an easy way of parsing XAML markup into objects:
public static object LoadTemplate(string templatePath)
{
object template;
// get the needed template paths
string absolutePath = Path.GetFullPath(templatePath);
string directoryPath = Path.GetDirectoryName(absolutePath);
using (FileStream inputStream = File.OpenRead(absolutePath))
{
var pc = new ParserContext
{
// It is critical to have the trailing backslash here
// will not work without it!
BaseUri = new Uri(directoryPath + "\\")
};
template = XamlReader.Load(inputStream, pc);
}
return template;
}
It’s critical here to setup the ParserContext so that resources can be referenced from the XAML. In our example we reference an image, but you can also reference things like fonts and other XAML files.
For our example we will use basic object data binding. However this technique would work equally well with other WPF binding sources such as XML. The object we will bind to has properties which correspond to the binding expressions in the template:
public class Data
{
public string Heading { get; set; }
public DateTime CurrentDate { get; set; }
public string Name { get; set; }
public string[] DotPoints { get; set; }
public bool GiveDiscount { get; set; }
}
public static void InjectData(FixedDocument document, object dataSource)
{
document.DataContext = dataSource;
// we need to give the binding infrastructure a push as we
// are operating outside of the intended use of WPF
var dispatcher = Dispatcher.CurrentDispatcher;
dispatcher.Invoke(
DispatcherPriority.SystemIdle,
new DispatcherOperationCallback(delegate { return null; }),
null);
}
In this code we set the DataContext for the FixedDocument to be the object containing the data. The strange thing is the dispatcher code. In a normal WPF application the dispatcher is used to marshal calls from worker threads to the UI thread. It seems that data binding outside of the UI context is not triggered unless the dispatcher is woken from its sleepy state. To do this we give it an arbitrary task.
The ugliest code is left for last. Below is what is required to convert a FixedDocument to an XPS format file ready to be viewed and printed by the user:
public static void ConvertToXps(FixedDocument fixedDoc, Stream outputStream)
{
var package = Package.Open(outputStream, FileMode.Create);
var xpsDoc = new XpsDocument(package, CompressionOption.Normal);
XpsDocumentWriter xpsWriter = XpsDocument.CreateXpsDocumentWriter(xpsDoc);
// xps documents are built using fixed document sequences
var fixedDocSeq = new FixedDocumentSequence();
var docRef = new DocumentReference();
docRef.BeginInit();
docRef.SetDocument(fixedDoc);
docRef.EndInit();
((IAddChild)fixedDocSeq).AddChild(docRef);
// write out our fixed document to xps
xpsWriter.Write(fixedDocSeq.DocumentPaginator);
xpsDoc.Close();
package.Close();
}
When working with XPS files you will find that you always have to work within a Package. The other interesting part of this code is the cast to IAddChild which is needed to add the DocumentReference as content within the FixedDocumentSequence.
WPF controls can only be used in STA threads. This isn’t usually something you have to consider when you are using WPF in a GUI application. However, if you are attempting to use this XPS code deep within some class library then the apartment state of the calling thread cannot be guaranteed. Instead of putting this requirement onto the consumer of your class you can use code like the following:
if (Thread.CurrentThread.GetApartmentState() != ApartmentState.STA)
{
var t = new Thread(() =>
{
// do work here when calling thread is not STA
});
t.SetApartmentState(ApartmentState.STA);
t.IsBackground = false;
t.Start();
t.Join();
}
else
{
// do work here when calling thread is STA
// this removes the overhead of creating
// a new thread when it is not necessary
}
This XPS generation technique is extremely flexible. Some key strengths:
Visual Studio 2008 Solution – XpsDocumentGenerator.zip