Setting Focus to a silverlight 4 application in an MVC 3 view
I'm having trouble with this. While it just means people have to click on the object to get it to respond, I think it looks unprofessional as hell.
Here is the view (including my n'th attempt):
@{
ViewBag.Title = "Show";
//Layout = "~/Views/Shared/_Empty.cshtml";
Layout = "";
}
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-ui-1.8.11.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.alerts.js")" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#HDViewerSL').focus();
});
</script>
<div id="silverlightControlHost" >
<object id='HDViewerSL' data="data:application/x-silverlight," type="application/x-silverlight-2" width="100%" height="100%">
<param name="minRuntimeVersion" value="4.0.60310.0"/>
<param name="autoUpgrade" value="true"/>
<param name="source" value="@ViewBag.HDViewSLXap"/>
<param name="background" value="black"/>
<param name="initParams" value="source=@ViewBag.ImagesUrl,id=@ViewBag.Id"/>
<div style="text-align:center;font-family:Arial;margin-top:50px;">
This page requires Silverlight 4.<br />
<br />
<a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.60310.0" style="text-decoration: none;">
<img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="Get Microsoft Silverlight" style="b开发者_如何学编程order-style: none"/>
</a>
</div>
</object>
<iframe style='visibility:hidden;height:0;width:0;border:0px'></iframe>
</div>
In my Silverlight constructor, I have:
this.GotFocus += new RoutedEventHandler(HDViewer_GotFocus);
This event is never firing.
Anyone know how to get the focus set on the object?
Thanks, David
You can request the plugin to be focused from within you Silverlight application using HtmlPage.Plugin.Focus();
The Loaded event of the MainPage is normally the right place to do this.
I was getting "Invalid Operation Exception" with that code, but I had it in the constructor. At your suggestion @Avee I put it in the constructor instead of in the load event and there was no change. The problem was that I needed to add:
<param name="enablehtmlaccess" value="true"/>
to the Silverlight object's parameters in the hosting page. (ref: http://weblogs.asp.net/albertpascual/archive/2009/04/13/silverlight-exception-the-dom-scripting-bridge-is-disabled.aspx)
Thanks! David
精彩评论