Call Silverlight with Javascript
I'm trying to call a public method of a silverlight app via javascript that simply sets a private variable. When I attempt to set the file name, I get a javascript error. fu.Content.myObject is undefined. Can anyone spot the error? http://www.devicity.com/fileuploadertestpage.aspx
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>FileUploader</title>
<style type="text/css">
html, body {
height: 100%;
overflow: auto;
}
body {
padding: 0;
margin: 0;
}
#silverlightControlHost {
height: 100%;
text-align:center;
}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<script type="text/javascript" src="Silverlight.js"></script>
<script type="text/javascript">
function onSilverlightError(sender, args) {
var appSource = "";
if (sender != null && sender != 0) {
appSource = sender.getHost().Source;
}
var errorType = args.ErrorType;
var iErrorCode = args.ErrorCode;
if (errorType == "ImageError" || errorType == "MediaError") {
return;
}
var errMsg = "Unhandled Error in Silverlight Application " + appSource + "\n" ;
errMsg += "Code: "+ iErrorCode + " \n";
errMsg += "Category: " + errorType + " \n";
errMsg += "Message: " + args.ErrorMessage + " \n";
if (errorType == "ParserError") {
errMsg += "File: " + args.xamlFile + " \n";
errMsg += "Line: " + args.lineNumber + " \n";
errMsg += "Position: " + args.charPosition + " \n";
}
else if (errorType == "RuntimeError") {
if (args.lineNumber != 0) {
errMsg += "Line: " + args.lineNumber + " \n";
errMsg += "Position: " + args.charPosition + " \n";
}
errMsg += "MethodName: " + args.methodName + " \n";
}
throw new Error(errMsg);
}
</script>
<script language="javascript" type="text/javascript">
function SetFileName() {
var fu = document.getElementById("FileUpload1");
fu.Content.myObject.SetFileName("10");
}
</script>
</head>
<body>
<form id="form1" runat="server" style="height:100%">
<div id="silverlightControlHost">
<object data="data:application/x-silverlight-2," id="FileUpload1" type="application/x-silverlight-2" width="600" height="600">
<param name="source" value="ClientBin/FileUploader.xap"/>
<param name="onError" value="onSilverlightError" />
<param name="background" value="white" />
<param name="minRuntimeVersion" value="3.0.40818.0" />
<param name="autoUpgrade" value="true" />
<a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40818.0" style="text-decoration:none">
<img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style:none"/>
</a>
</object><iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe></div>
<input type="button" onclick="SetFileName();" value="SetFileName" />
</form>
</body>
</html>
``
[ScriptableType]
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
HtmlPage.RegisterScriptableObject("myObject", this);
}
private string m_FileName = "setthis";
[ScriptableMember]
public void SetFileName(string fileName)
{
m_FileName = fileName;
}
private OpenFileDialog dlg = new OpenFileDialog(开发者_运维技巧);
private void Browse_Click(object sender, RoutedEventArgs e)
{
dlg.Multiselect = false;
dlg.Filter = "MP3s (*.mp3)|*.mp3|All files (*.*)|*.*";
bool? retval = dlg.ShowDialog();
if (retval != null && retval == true)
{
StatusText.Text = dlg.File.Name;
}
else
{
StatusText.Text = "No file selected...";
}
}
private void UploadFile(string fileName, Stream data)
{
UriBuilder ub = new UriBuilder("http://localhost:43439/uploadfile.aspx");
ub.Query = string.Format("filename={0}", m_FileName);
WebClient c = new WebClient();
c.OpenWriteCompleted += (sender, e) =>
{
PushData(data, e.Result);
e.Result.Close();
data.Close();
};
c.OpenWriteAsync(ub.Uri);
}
private void Upload_Click(object sender, RoutedEventArgs e)
{
try
{
UploadFile(StatusText.Text, dlg.File.OpenRead());
}
catch (Exception ex)
{
StatusText.Text = ex.ToString();
}
}
private void PushData(Stream input, Stream output)
{
byte[] buffer = new byte[4096];
int bytesRead;
int totalBytesRead = 0;
while ((bytesRead = input.Read(buffer, 0, buffer.Length)) != 0)
{
totalBytesRead += bytesRead;
StatusText.Text = ((int)(totalBytesRead / input.Length)).ToString("P");
output.Write(buffer, 0, bytesRead);
}
}
}
Does it work if you don't capitalize Content
in the Javascript SetFileName
function?
fu.content.myObject.SetFileName("10");
That's the only obvious difference I see between what my program and your code are doing to call into a Silverlight object.
精彩评论