如何在C#中的同一页面上与Silverlight应用程序进行ASP.NET控件对话

时间:2022-01-15 01:00:36

For example:

First, say I have a Silverlight app with Windowless=true so that I can place ASP.NET controls on top of it. Then I place an ASP.NET button on the page. How can I have say the text of a control in the Silverlight app change when the user presses the ASP.NET button? How would I send the Silverlight app an update message from the C# code that catches the Click of the ASP.NET button?

首先,假设我有一个Windowlight = true的Silverlight应用程序,以便我可以将ASP.NET控件置于其上。然后我在页面上放置一个ASP.NET按钮。当用户按下ASP.NET按钮时,如何更改Silverlight应用程序中控件的文本?如何从捕获Click按钮的C#代码向Silverlight应用程序发送更新消息?

Thanks, Jeff

2 个解决方案

#1


1  

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? 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. 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. Since that code is executing on the server it cannot get to the silverlight app. If you really need to interact with the silverlight application directly on the client you would use javascript in the browser.

据我所知,根据您的问题,Silverlight应用程序在Web浏览器中运行,您将它嵌入到asp.net页面中?您在页面上拖动的asp.net按钮的代码位于服务器上,并以html格式发送到Web浏览器。当您单击页面上的按钮时,它将表单数据提交回ASP.NET解释的服务器并调用您的按钮单击代码。由于该代码在服务器上执行,因此无法访问silverlight应用程序。如果您确实需要直接在客户端上与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

#2


1  

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.

希望这可以帮助。

#1


1  

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? 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. 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. Since that code is executing on the server it cannot get to the silverlight app. If you really need to interact with the silverlight application directly on the client you would use javascript in the browser.

据我所知,根据您的问题,Silverlight应用程序在Web浏览器中运行,您将它嵌入到asp.net页面中?您在页面上拖动的asp.net按钮的代码位于服务器上,并以html格式发送到Web浏览器。当您单击页面上的按钮时,它将表单数据提交回ASP.NET解释的服务器并调用您的按钮单击代码。由于该代码在服务器上执行,因此无法访问silverlight应用程序。如果您确实需要直接在客户端上与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

#2


1  

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.

希望这可以帮助。