Invalid Pointer error trying to call silverlight method from javascript
From the silverlight app
public MainPage()
{
InitializeComponent();
HtmlPage.RegisterScriptableObject("MainPage", this);
}
[ScriptableMember]
public void StartEditLocation(string name, int? top, int? left)
{
MapTitle.Text = "Set Contract Location - " + name;
LocationEllipse.SetValue(Canvas.LeftProperty, left ?? 30);
LocationEllipse.SetValue(Canvas.TopProperty, top ?? 30);
}
And from the javascript
<object开发者_开发技巧 data="data:application/x-silverlight-2," id="admin_map" type="application/x-silverlight-2" width="100%" height="100%">
<param name="source" value="/ClientBin/AdminMap.xap"/>
<param name="onError" value="onSilverlightError" />
<param name="background" value="white" />
<param name="minRuntimeVersion" value="4.0.50826.0" />
<param name="autoUpgrade" value="true" />
<a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.50826.0" style="text-decoration:none">
<img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="Get Microsoft Silverlight" style="border-style:none"/>
</a>
</object><iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe>
function show_map(top, left) {
var control = document.getElementById("admin_map");
var values = new Array();
values[0] = $('#Name').val();
values[1] = top;
values[2] = left;
if (control)
control.Content.MainPage.StartEditLocation(values);
else
alert('Error loading map');
}
It seems to be a problem actually calling the method because it gets past the if checking to make sure the control exists. I think it must be some syntax error or something simple like that.
You are passing a javascript array instead of the values. Try with this:
control.Content.MainPage.StartEditLocation($('#Name').val(), top, left);
I found the problem, I had the div that the silverlight was in hidden by default, so it was never actually initializing. When I have it visible and immediately hide it on page load using javascript it initializes right away and successfully calls the method.
You could also guard against calling the method on the unloaded silverlight control.
function show_map(top, left) {
var control = document.getElementById("admin_map");
var values = new Array();
values[0] = $('#Name').val();
values[1] = top;
values[2] = left;
if (control)
if (control.IsLoaded) {
control.Content.MainPage.StartEditLocation(values);
}
else
alert('Error loading map');
}
精彩评论