How To Make Sure the Silverlight App Gets Keyboard focus

After playing with silverlight for a while, I found that the Focus method did not work very well to give the silverlight app focus for keyboard input. You can use a trick in javascript to make sure that the silverlight control has focus to begin with. Then later use the Focus method inside your XAML code behind.

The following sample shows you how to force focus to the silverlight control when your aspx page comes up.  In the body tag add the following javascript (where XAML1 is the ID of your Silverlight control

<body onload="document.getElementById('XAML1').focus()" style="height:100%;margin:0;">

Your Silverlight control tag should already look something like this.

<asp:Silverlight ID="XAML1" runat="server" Source="~/ClientBin/MySilverlightApp.xap" MinimumVersion="2.0.30523" Width="100%" Height="100%" />