有没有办法使用 ASP.NET 代码隐藏将 Silverlight XAML 插入我的页面? [英] Is there a way to insert Silverlight XAML into my page using ASP.NET code-behind?

查看:11
本文介绍了有没有办法使用 ASP.NET 代码隐藏将 Silverlight XAML 插入我的页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我想根据我的 ASP.NET 网页上的一些数据创建一些 Silverlight 图表.做到这一点的方法,要么使用 Visifire(我目前的东西)或即将推出的 Silverlight 图表,似乎是要创建一些 XAML,或者至少是一些 Silverlight 对象(就像在这个 Visifire 示例中),然后以某种方式将它们塞入页面.

So, I want to create some Silverlight charts from some data on my ASP.NET web page. The way to do this, either using Visifire (my current thing) or the upcoming Silverlight charting, seems to be to create some XAML, or at least some Silverlight objects (like in this Visifire sample), and then stuff them into the page somehow.

奇怪的是,做到这一点的唯一方法似乎是通过 JavaScript!也就是说,我必须做这样的事情:

Strangely, the only way to do this seems to be through JavaScript! That is, I'd have to do something like this:

用户 -> 单击按钮 -> JavaScript 事件处理程序 -> AJAX 调用以从服务器端方法获取 XAML -> 将 XAML 字符串填充到客户端 HTML(如 一般情况下像这样的 Visifire 特定方法).

User -> click button -> JavaScript event handler -> AJAX call to get XAML from server-sided method -> stuff the XAML-string into the client-sided HTML (like this in general or like this for a Visifire-specific method).

这是非常违反直觉的!我更希望能够在 MyPage.aspx.cs 中做这样的事情:

This is highly counterintuitive! What I'd much rather have is the ability to do something like this, inside MyPage.aspx.cs:

protected void MyButton_Click(object sender, EventArgs e)
{
    this.MyFictionalSilverlightControl.Xaml = this.GenerateXamlFromData();
}

那就更好了!我很高兴将 MyFictionalSilverlightControl 保存在 UpdatePanel 中,尽管它效率低下;我只是希望我知道如何制作这样一个虚构的 Silverlight 控件.

That would be much nicer! I'm happy to keep MyFictionalSilverlightControl inside an UpdatePanel, even though it's inefficient; I just wish I knew how to make such a fictional Silverlight control.

有什么想法吗?或者关于如何更好地做到这一点的建议?

Any ideas? Or suggestions on how to do this better?

推荐答案

我建议采用更 AJAXy 的方法:让 JS 请求 XAML,然后运行时将该 XAML 插入到控件中(参见 HTML Bridge)XAML 已从服务器返回.这样你的控件就可以为新的显示做一个很好的动画/过渡.

I'd suggest a more AJAXy sort of approach: have JS request the XAML and then runtime insert that XAML into the control (see the HTML Bridge) after the XAML has been returned from the server. That way your control could do a nice animation/transition to the new display.

不过,你可以做你的目标:

Still, you can do what you're aiming at:

  • Silverlight 服务器控件放入您的更新面板.
  • 编写一个 aspx 页面来生成 XAML(更改 DocType 以简化操作)
  • 将 Silverlight 服务器控件指向 .ASPX 页面:

  • Slap a Silverlight server control into your Update Panel.
  • Write an aspx page to generate the XAML (Change the DocType to make things easier)
  • Point the Silverlight server control at the .ASPX page:

... asp:SilverlightID="Xaml1"runat="服务器"Source="~/ClientBin/myXAMLPage.aspx"最低版本="2.0.30523"宽度="100%"高度="100%">

... asp:Silverlight ID="Xaml1" runat="server" Source="~/ClientBin/myXAMLPage.aspx" MinimumVersion="2.0.30523" Width="100%" Height="100%">

每当用户单击您的按钮时,触发 UpdatePanel 执行此操作.

Trigger the UpdatePanel to do it's thing whenever a user clicks your button.

现在,只要用户单击按钮,就会呈现更新面板(包含 Silverlight 控件),并且该 SL 控件将请求 XAML 文件并呈现它找到的内容.

Now, whenever a user clicks a button, the Update Panel (containing the Silverlight control) will be rendered, and that SL control will request the XAML file and render what it finds.

请注意,此方法使用 XAML 文件实例化控件.这意味着您将无法在 Silverlight 中使用托管代码.如果您需要托管代码,请指定您的 .xap 文件,然后使用 initparam 指定myXAMLPage.aspx",而不是在源中指定myXAMLPage.aspx"页面.在 Silverlight 应用程序中,下载 xaml 文件(WebClient 或其他),然后使用 XamlReader.Load 加载并显示它.

Do note, this approach instantiates the control with a XAML file. This means that you won't be able to use managed code in Silverlight. If you need managed code, instead of specifying the "myXAMLPage.aspx" page in the Source, specify your .xap file and then use an initparam to specify "myXAMLPage.aspx". Inside you're Silverlight application, download the xaml file (WebClient or whatever), and then use XamlReader.Load to load it up and display it.

嗯,埃里克

这篇关于有没有办法使用 ASP.NET 代码隐藏将 Silverlight XAML 插入我的页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆