[英]How do you make a ASP.NET Control talk to a Silverlight app on the same page in C#
For example: 例如:
First, say I have a Silverlight app with Windowless=true so that I can place ASP.NET controls on top of it. 首先,假设我有一个Silverlight应用程序,其Windowless = true,以便可以将ASP.NET控件放在其顶部。 Then I place an ASP.NET button on the page.
然后,在页面上放置一个ASP.NET按钮。 How can I have say the text of a control in the Silverlight app change when the user presses the ASP.NET button?
当用户按下ASP.NET按钮时,如何说出Silverlight应用程序中控件的文本发生变化? How would I send the Silverlight app an update message from the C# code that catches the Click of the ASP.NET button?
我如何从C#代码向Silverlight应用发送更新消息,以捕获ASP.NET按钮的单击?
Thanks, Jeff 谢谢杰夫
From what I can tell, based on your question, the Silverlight application is running in a web browser and you have it embedded in an asp.net page? 据我所知,根据您的问题,Silverlight应用程序正在Web浏览器中运行,并且已将其嵌入到asp.net页面中? The code for the asp.net button that you are dragging on the page lives on the server and gets sent to the web browser as html.
您在页面上拖动的asp.net按钮的代码位于服务器上,并以html格式发送到Web浏览器。 When you click the button on the page it is submitting form data back to the server which ASP.NET interprets and calls your button click code.
当您单击页面上的按钮时,它会将表单数据提交回由ASP.NET解释并调用您的按钮单击代码的服务器。 Since that code is executing on the server it cannot get to the silverlight app.
由于该代码正在服务器上执行,因此无法访问Silverlight应用程序。 If you really need to interact with the silverlight application directly on the client you would use javascript in the browser.
如果确实需要直接在客户端上与silverlight应用程序进行交互,则可以在浏览器中使用javascript。
Here is a basic example: http://blogs.vertigo.com/personal/ralph/Blog/archive/2008/05/15/call-silverlight-from-javascript-call-javascript-from-silverlight.aspx 这是一个基本示例: http : //blogs.vertigo.com/personal/ralph/Blog/archive/2008/05/15/call-silverlight-from-javascript-call-javascript-from-silverlight.aspx
From memory, you need to expose a scriptable member from your silverlight application, eg: 从内存中,您需要从Silverlight应用程序中公开可编写脚本的成员,例如:
[ScriptableMember()]
public void ChangeText(string newText)
{
// Update your text control here
}
and register it for scripting from javascript in the constructor: 并在构造器中将其注册以从javascript编写脚本:
public MySilverlight()
{
InitializeComponent();
HtmlPage.RegisterScriptableObject("MyObject", this);
}
You can then invoke it from javascript as; 然后您可以从javascript中调用它;
function ChangeText()
{
var yourObject = getElementById("yourObjectID");
yourObject.Content.MyObject.ChangeText("New Text");
}
Then just wire up the button's client click to invoke the javascript ChangeText method. 然后只需连接按钮的客户端单击以调用javascript ChangeText方法。
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.