Вы можете создать прокси через ScriptableType
, к сожалению, вы должны сделать это во всех ваших приложениях Silverlight.
[ScriptableType]
public class KeyPressProxy
{
public KeyPressProxy()
{
App.Current.RootVisual.KeyDown += (s, e) => KeyDown(s, e);
}
[ScriptableMember]
public event KeyEventHandler KeyDown = delegate { };
}
Затем вы должны зарегистрировать объект, но убедитесь, что вы делаете это после того, какRootVisual был создан:
private void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = new MainPage();
var proxy = new KeyPressProxy();
HtmlPage.RegisterScriptableObject("KeyPressProxy", proxy);
}
Наконец, вы можете добавить обработчик в свой JavaScript:
<div id="silverlightControlHost">
<object id="silverlightApp1" data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
<param name="onLoad" value="onPluginLoaded" />
<!-- ... -->
</object><iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe>
</div>
<script type="text/javascript">
function onPluginLoaded() {
document.getElementById('silverlightApp1').content.KeyPressProxy.KeyDown = function (s, e) {
alert('pressed');
}
}
</script>