时间:2011-01-06 17:18:34

标签: visual-studio silverlight

1 个答案:

答案 0 :(得分: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>

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

我的银光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();"" />");
    }
}

非常重要注意:我已经测试了这段代码,它在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!');"" />");
    }
}

此代码在IE / FF / Chrome / Opera(当然:))下运行良好 至于我,我喜欢动态js和html的第一个例子。而且我很确定可以修复它并让其他浏览器接受,而不仅仅是Opera 我希望我的例子可以帮助你=)