将JavaScript添加到生成的Silverlight项目的html测试页面

Phi*_*own 6 silverlight visual-studio

VS2010,Silverlight 4.0.

有没有人知道是否有办法让Visual Studio在编译用于调试的Silverlight项目时将javascript(或其他任何内容)添加到它生成的html测试页面中?它是否使用模板,如果是,它在哪里?

Igo*_*nko 1

非常有趣的问题!
我尝试做类似的事情,最后找到了原始但非常有效的解决方案:

首先,您应该使用“属性”->“调试”->“特定页面”来设置特定页面。我的页面示例:

<!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>
    <title>SilverlightApplication1</title>
    <script type="text/javascript"> 
    </script>
</head>

<body>
    <div id="fillWithData">
    </div>
    <form id="form1" runat="server" style="height:100%">
    <div id="silverlightControlHost" style="height: 100%; text-align:center">
        <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="30%" height="30%">
          <param name="source" value="file:///C:/Users/Igor/Documents/Visual Studio 2010/Projects/MobileTest/SilverlightApplication1/Bin/Debug/SilverlightApplication1.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></div>
    </form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

注意:空白脚本和 div 元素很重要!我们将动态 js/html 加载到其中。

我的 silverlight xaml.cs:

public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();
        Loaded += new RoutedEventHandler(MainPage_Loaded);
    }

    void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        HtmlDocument hostPage = HtmlPage.Document;

        ScriptObject obj = hostPage.GetElementsByTagName("script")[0];
        obj.SetProperty("innerHTML", "function alertonclick() { alert('Amazing! I am dynamic javascript!'); }");    

        HtmlElement ipAddressHtml = hostPage.GetElementById("fillWithData");
        ipAddressHtml.SetProperty("innerHTML", @"<input id=""Button1"" type=""button"" value=""button"" onclick=""alertonclick();"" />");
    }
}
Run Code Online (Sandbox Code Playgroud)

非常重要的注意事项:我已经测试过这段代码,它只能在 Opera 下运行。如果您需要使用其他浏览器测试某些内容,那么您可以将 javascript 直接内联到元素中。

例子:

public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();
        Loaded += new RoutedEventHandler(MainPage_Loaded);
    }

    void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        HtmlDocument hostPage = HtmlPage.Document;

        HtmlElement ipAddressHtml = hostPage.GetElementById("fillWithData");
        ipAddressHtml.SetProperty("innerHTML", @"<input id=""Button1"" type=""button"" value=""button"" onclick=""alert('Amazing! I am dynamic javascript!');"" />");
    }
}
Run Code Online (Sandbox Code Playgroud)

这段代码在 IE/FF/Chrome/Opera 下运行良好(当然:))。
对于我来说,我喜欢第一个同时包含动态 js 和 html 的示例。我非常确定可以修复它并使其可以被其他浏览器(而不仅仅是 Opera)接受。
我希望我的例子能帮助你=)