Accepting keyboard input in Silverlight Part 2

So accepting keyboard input isn’t too difficult, however, displaying the corresponding character seems to be a lot harder nut to crack. The basic problem is that the key codes received are ASCII based but that only vaguely relates to the keyboard. For example, a typical keyboard shift+6 would result in ^ but in ASCII the key code maps to ‘. This is before you start worrying about country layouts, e.g. the dreaded location of the double quote. I’m currently looking back to Win Forms to see how that handles the problem. I reflected so code out, I nice enum Keys which maps to the PlatformKeyCode returned in Silverlight. This does a good job of abstracting the keyboard but still doesn’t really help in displaying the correct character. So if I press 1 if get D1, or # (on a UK keyboard) returns oemPipe. This is fine for mapping a keyboard for something like a game, but not very helpful in echoing the character to the screen. So I’m looking at porting the KeyConverter classes, I’m doubtful this will work so I may have to resort to getting help from the browser via some JavaScript. If anyone knows of a better way the please let me know!
 

SATA II (SATA-2) cable

Since aquiring a copy Vista Ultimate from Mix 07 I thought that it would only be polite to install it 😉
I didn’t want to give up on XP in case I lost devices or…games don’t play as well Embarrassed
So I bought a nice new SATA II drive to install a dual boot Vista on. Now I’ve had problems with SATA cables because the sound proofing on my PC can nudge the cable and SATA cables are RUBBISH at staying in place so I knew I had to find a right-angled cable to increase the gap between the cable and the case. I found a cable by akasa that looked right-angled, although it didn’t specify that, so I took a chance on that. What I didn’t realise is the cable has been designed to include a retaining clip! Hurray, so not only is it right-angled it makes a decent attempt at staying in place. So if you have a choice get one of these! Product Code: SATA2-60-BLUV

Accepting keyboard input in Silverlight

I thought I’d tackle one of the most obvious problems with Silverlight today, the lack of a text edit control. So I thought I’d share my first "toe wetting" in this.
 
First off I created simple Silverlight 1.1 project with a canvas and a TextBlock…
<Canvas x:Name="parentCanvas"
        xmlns="
http://schemas.microsoft.com/client/2007"
        xmlns:x="
http://schemas.microsoft.com/winfx/2006/xaml"
        Loaded="Page_Loaded"
        x:Class="SilverlightProjectKeyEcho.Page;assembly=ClientBin/SilverlightProjectKeyEcho.dll"
        Width="640"
        Height="480"
        Background="White"
        >
  <TextBlock x:Name="TextBlockEcho" Text="Hello there"></TextBlock>
</Canvas>
I find it useful to always create something I can see by default.
 
Q1. How to accept keyboard input at all?
Well there is a trick to this. First off I used Intellisense in both the XAML and the code behind to create an event for KeyDown on the TextBlock, but I kept getting a (hidden) exception when running the code. After a quick look around the web I discovered that keyboard event can only be attached to the root canvas. I created a quick handler…
 public void EchoKeyDown(object sender, KeyboardEventArgs e)
        {
            int keyCode = (char)e.PlatformKeyCode;
            if (!e.Shift)
            {
                keyCode = keyCode + 32;
            }
            TextBlockEcho.Text += (char)keyCode;
        }
 and I’ve got a very simple text echo. Now to tackle CTRL,SHIFT, Delete, arrow keys, etc, etc…

Mix 07 UK

I attended Mix 07 UK last week so I thought I’d give a quick review.

Day 1: 11 September 2007

Mix:UK 07 Keynote

I’d watched a number of presentations from Mix LA so the keynote didn’t really contain anything new but it did reinforce the message that Silverlight seems like a viable platform.

Total Experience Design (Paul Dawson, Matt Bagwell)

I’d made a decision to avoid low-level technical session in favour of learning about user experience design so I went along to this session. It was quite interesting and the messages I took away from the session were; people that enjoy an application will get more from it, rich experiences are becoming the norm rather than the exception.

Building Silverlight Applications using .NET – Part 2 (Scott Guthrie)

Joined the second part of this session with the hope that it would be more advanced than the previous part. Although I didn’t get an awful lot from this there were a couple of gems; 1. Multiple file uploads (worth the entrance price) 2. The use of Silverlight as a non-UI tool 3. The reliance on the browser cache (shame but I can understand it).

[Insert really good title here.] (Hoss Gifford)

A nice idea this one and Hoss seems like a good guy with a real enthusiasm for his work. The basic idea was to come clean an explain what goes wrong with (artistic) projects. I didn’t get a lot from this one although my designer g/f enjoyed it, I guess it was one for the non-developers.

Developer Panel

I must admit I got little bored with this one. It was billed as Cloud vs Client but IMO that was an argument for two years ago. I think everyone understands the basic advantages/disadvantages of both and when to combine the two. I did find one of the audience questions interesting, I’ll paraphrase it, "if my country requires that I adhere to strict rules governing the access/security of my customers data, how can enforce that if the data is in the cloud and could potentially be stored in countries where the rules are different, non-existent or even counter the rules of my country?". Tough one.

Day 2 to follow…

Cannot build Silverlight 1.1 in debug :(

I have the latest VPC of Orcas, Silverlight 1.1 Alpha refresh and alpha tools but when I try to build a debug release silverlight project I get…

Error 1 Unexpected debug information initialization error — ‘Failed to find a required export in the runtime.’ SplashScreenDownload2

I wonder if there is some sort of debug version of the dlls that I’m missing. It used to work before the latest refresh, and it also builds in release mode. Just in case anyone has the same problem or has some ideas this is the diagnosting output from Visual Studio (you get the same error if you run csc by hand)

Target "CoreCompile" in file "C:\WINDOWS\Microsoft.NET\Framework\v3.5\Microsoft.CSharp.Targets":
  Building target "CoreCompile" completely.
  Output file "obj\Debug\SplashScreenDownload2.dll" does not exist.
  Task "Csc"
    Command:
    C:\WINDOWS\Microsoft.NET\Framework\v3.5\Csc.exe /noconfig /nowarn:1701,1702 /nostdlib+ /errorreport:prompt /warn:4 /define:DEBUG;TRACE /reference:"c:\Program Files\Microsoft Silverlight\agclr.dll" /reference:"c:\Program Files\Microsoft Silverlight\mscorlib.dll" /reference:"c:\Program Files\Microsoft Silverlight\System.Core.dll" /reference:"c:\Program Files\Microsoft Silverlight\system.dll" /reference:"c:\Program Files\Microsoft Silverlight\system.silverlight.dll" /reference:"c:\Program Files\Microsoft Silverlight\system.Xml.core.dll" /debug+ /debug:full /optimize- /out:obj\Debug\SplashScreenDownload2.dll /resource:SLDL_DLL.DLL,SplashScreenDownload2.SLDL_DLL.DLL /resource:sqlservr.exe,SplashScreenDownload2.sqlservr.exe /resource:fxref.hxs,SplashScreenDownload2.fxref.hxs /target:library Page.xaml.cs Properties\AssemblyInfo.cs "C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\SplashScreenDownload2\SplashScreenDownload2\obj\Debug\Page.g.cs" "C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\SplashScreenDownload2\SplashScreenDownload2\obj\Debug\ProgressAnim.g.cs"
fatal error CS0040: Unexpected debug information initialization error — ‘Failed to find a required export in the runtime.’

Compile complete — 1 errors, 0 warnings
  Done executing task "Csc" — FAILED.
Done building target "CoreCompile" in project "SplashScreenDownload2.csproj" — FAILED

[Edit] Finally gave and reinstalled the VPC. Please note, if you want an error free install ensure that install IE7 before attempting to install Silverlight, I had lots of odd problems with IE6.

My first Silverlight demo

With Mix07 just around the corner I thought I’d have a play with Silverlight 1.0, rather than the more .net like Silverlight alpha. I was a bit disappointed with Expression Blend. I won’t comment too much on the new interface style, I’m sure it’s an acquired taste, it was the lack of XAML intellisense that annoyed me. Ok, sure Blend shouldn’t be used by developers but given Microsoft’s stance of not providing a UI for anything other than the page load trigger I think it’s a bit much to expect the non-developer to use notepad to provide even the simplest of event handling. The change in emphasis is quite odd, I often complain that Visual Studio doesn’t work well for large scale developments but Blend seems only to work if you have a team of designers and developers. Even if you play both roles you’ll still need to use both Blend and Visual Studio, although you can launch one from the other it is hardly an integrated experience. Still tool quibbles aside the Silverlight itself worked well and I’ve not seen anything to stop me wanting to use it in preference to complex AJAX applications.

Reducing vibration noise from a PC

My main PC sits in an upstairs room. Although the machine (was – more on that later) pretty quiet to be around you could hear a very real rumble when downstairs. I needed to come up with a way of isolating the PC from the floor to stop this noise. I’ve been looking into getting some form of rubber matting such as that used to reduce noise in heavy machine shops. Last night I remembered an old HiFi trick, cut a squash ball in half and put that under the PC. I tried it today and it seems to have done the trick, I can’t hear it from downstairs now…I’m sure the neighbours will thank me too Embarrassed.
 
However, the machine has recently developed an annoying vibration noise when closer to it, it seems to resonate through the case as putting pressure on the case seems to stop it. So I’ll need to open it up and try to ascertain what is causing this – I hate any noise from my PCs.
 
 
 

Simple way to Serialize an object to a string

I’m the first to admit that the number of streams and serializing methods can be confusing. I wanted a quick and simple way to test my serialize code by persisting an object into a string. A quick google later and I had found a number of overly complicated examples, so here is my very simple method…
                        XmlSerializer serializer = new XmlSerializer(typeof(MyType));
            string resultingXml = null;
            using (StringWriter stringWriter = new StringWriter(CultureInfo.InvariantCulture))
            {
                serializer.Serialize(stringWriter, myObject);
                resultingXml = stringWriter.ToString();
            }

Odd behaviour with Powershell Get-ChildItem with exclude

Discovered some strange behaviour with the File objects in Powershell. If you pipe the results of Get-ChildItem through foreach and ask for the default $_ you’ll get the file name. However, if you add an -exclude to the gci then the $_ becomes the full path!? Very odd.