Thursday, August 27, 2009

Serving Silverlight 3 apps from IIS6

If you want to serve Silverlight 3 apps from an IIS6 server running on Windows Server 2003 you need to add the following three mime types (I added them at the server level). If you don’t make these changes the web page will show a blank Silverlight window. You can right click to get the Silverlight menu but your app does not run.

.xap     application/x-silverlight-app
.xaml    application/xaml+xml
.xbap    application/x-ms-xbap

I discovered that IIS needs to be restarted before the settings take effect. To restart IIS run IISRESET from a command prompt.

Saturday, August 22, 2009

WPF auto fading error message

Here is a small control template to display an error message. The message will fade away after a couple of seconds. You set the message like this:-

Label1.Content = DateTime.Now.ToString();


Here is the xaml:-



<Window x:Class="TRM_Uploader.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window2" Height="300" Width="300">
<Window.Resources>
<ControlTemplate x:Key="MyErrorMessage"
TargetType="{x:Type Label}">
<StackPanel Orientation="Horizontal"
Name="ErrorMessage">
<Border CornerRadius="4"
BorderThickness="0"
Background="Red"
VerticalAlignment="Center"
Margin="4,0"
BorderBrush="Transparent">
<Label Foreground="White"
VerticalAlignment="Center"
Padding="4,2"
FontWeight="Bold"
Name="Part1"
Visibility="Visible">Error</Label>
</Border>
<!-- This TextBlock accepts the content from the parent 'label'. It is not actually displayed, just used to bind data-->
<TextBlock Name="Part3"
Text="{TemplateBinding Content}"
Visibility="Collapsed"></TextBlock>
<!-- This TextBlock binds to the one above and triggers an animation when it gets updated. -->
<!-- Its required because TemplateBinding does not allow NotifyOnTargetUpdate -->
<TextBlock Margin="2"
Name="Part2"
Foreground="Red"
Padding="3"
Text="{Binding ElementName=Part3, Path=Text,NotifyOnTargetUpdated=True}"
VerticalAlignment="Center"></TextBlock>
</StackPanel>
<ControlTemplate.Triggers>
<!-- When Text property of Part2 is blank hide entire control-->
<Trigger SourceName="Part3"
Property="Text"
Value="">
<Setter TargetName="ErrorMessage"
Property="Visibility"
Value="Hidden"></Setter>
</Trigger>
<!-- This trigger is called when Part2 updates which in turn is updated by Part3's databind.
It uses animation to make the label fade away after a few seconds. -->
<EventTrigger SourceName="Part2"
RoutedEvent="Binding.TargetUpdated">
<BeginStoryboard>
<Storyboard TargetName="ErrorMessage">
<DoubleAnimation Storyboard.TargetProperty="Opacity"
Duration="0:0:0"
To="1.0" />
<DoubleAnimation Storyboard.TargetProperty="Opacity"
Duration="0:0:2"
From="1.0"
To="0.0"
BeginTime="0:0:1" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Window.Resources>
<StackPanel Orientation="Vertical">
<Label Template="{StaticResource MyErrorMessage}"
Name="Label1"></Label>
<Button Click="button1_Click">click to test</Button>
</StackPanel>
</Window>




And the c#



 



public partial class Window2 : Window
{
public Window2 ()
{
InitializeComponent();
}

private void button1_Click (object sender, RoutedEventArgs e)
{
Label1.Content = DateTime.Now.ToString();
}
}

Thursday, August 20, 2009

How to make a bootable USB memory stick in Windows 7

I spent ages on this last night without any success, tonight it took me about 60 seconds. Here is the trick.

First you need to download Windows Enabler from http://www.angelfire.com/falcon/speedload/Enabler.htm. This allows any disabled controls to be re-enabled. Install the program and click on its system tray icon to enable it. The Icon should show ‘on’ to show it’s working :

With Windows Enabler running you can go into Windows Explorer and format your memory stick (obviously this will erase everything on the stick). Normally the “create an MS-DOS startup disk” is disabled for everything except floppy disks but with Windows Enabler running you can click the disabled checkbox to use the option with any drive.

I think the file system needs to be set to “FAT”. Once formatted your stick will be a bootable MS-DOS 4.9.3000 boot disk. Easy!

IE image visibility bug

I found two very strange bugs in IE 8 yesterday (probably in other IE versions as well). I have an image and some text in a SPAN that is made visible with some JavaScript. Here is the HTML :

<img id="ctl00_ContentPlaceHolder1_Image3" src="Graphics/waiting.gif" align="middle" style="border-width:0px;visibility:hidden" />

<span id="ctl00_ContentPlaceHolder1_Label4" style="visibility:hidden">Please wait. Adding results to database.</span>

The Javascript attempts to change the visibility like this :

ctl00_ContentPlaceHolder1_Label4.style.visibility='visible';

ctl00_ContentPlaceHolder1_Image3.style.visibility='visible';

for SPAN tags this works. for IMG tags it has absolutely no effect!

The solution is a horrible hack :-

<script type="text/javascript">

function UploadComplete()

{

ctl00_ContentPlaceHolder1_Label4.style.visibility='visible';

document.all.ctl00_ContentPlaceHolder1_Image3.style.display='none';

document.all.ctl00_ContentPlaceHolder1_Image3.style.display='block';

document.all.ctl00_ContentPlaceHolder1_Image3.style.visibility='visible';

document.all.ctl00_ContentPlaceHolder1_Image3.src='waiting.gif';

window.setTimeout(TurnAnimatedGifBackOn, 500);

}

function TurnAnimatedGifBackOn ()

{

var tmp = document.all.ctl00_ContentPlaceHolder1_Image3;

if(typeof (tmp) == 'object')

{

tmp.style.visibility = 'visible'; tmp.src='waiting.gif';

}

}

</script>

To fix the problem the IMG tag has to have it’s display property toggled from ‘none’ and back to ‘block’.

The second bug is that animated gifs stop animating when a page post back occurs. To fix this problem I’ve used a timer to alter the image 500ms after the page post back. To get the animation working the src tag must be reloaded with the image url.

Other browsers do not seem to have this problem.

Enable Hardware Virtualization on Sony VGN-AR71ZU

It is really annoying to discover that Sony disable hardware virtualization and do not provide a bios setting to allow it to be re-enabled. This prevents you from being able to use Windows Virtual PC/MP Mode in Windows 7. I finally managed to hack the bios and now have full hardware virtualization!

Hacking the bios is actually trivial provided you know the right register to change. Chris Hay has a blog post which describes the change for a VGN-AR71ZU (bios = Phoenix R2090J8). (http://silverlightuk.blogspot.com/2009/04/enabling-hardware-virtualization-on.html)

The trouble I had was creating a bootable USB memory stick. I spent hours messing around last night with absolutely no success. In the end I gave up on the USB stick and found an old USB floppy drive. I downloaded the ms-dos 6.22 disk images from MSDN and made myself a set of three setup disks. I then booted off the floppy, exited ms-dos setup with F3 and ran SYMCMOS as described in Chris’s blog post. The process was not helped by the fact that the USB floppy would not work on Vista or Windows 7 so I had to create the ms-dos setup disks on an old XP machine.