Visual studio 将javascript添加到Silverlight项目生成的html测试页面
VS2010,Silverlight 4.0Visual studio 将javascript添加到Silverlight项目生成的html测试页面,visual-studio,silverlight,Visual Studio,Silverlight,VS2010,Silverlight 4.0 有人知道有没有办法让VisualStudio在编译silverlight项目进行调试时向生成的html测试页面添加javascript(或其他相关内容)?它使用模板吗?如果是,它在哪里?真是个有趣的问题 我尝试过这样做,最终找到了原始但非常有效的解决方案: 首先,您应该使用属性->调试->特定页面来设置特定页面。 我的页面示例: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional/
有人知道有没有办法让VisualStudio在编译silverlight项目进行调试时向生成的html测试页面添加javascript(或其他相关内容)?它使用模板吗?如果是,它在哪里?真是个有趣的问题
我尝试过这样做,最终找到了原始但非常有效的解决方案: 首先,您应该使用属性->调试->特定页面来设置特定页面。 我的页面示例:
<!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>
SilverlightApplication1
注意:空白脚本和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();"" />");
}
}
public部分类主页面:UserControl
{
公共主页()
{
初始化组件();
已加载+=新路由EventHandler(主页面_已加载);
}
已加载无效主页(对象发送器、路由目标)
{
HtmlDocument hostPage=HtmlPage.Document;
ScriptObject obj=hostPage.GetElementsByTagName(“脚本”)[0];
SetProperty(“innerHTML”,“函数alertonclick(){alert('mazing!我是动态javascript!');}”);
HtmlElement ipAddressHtml=hostPage.GetElementById(“fillWithData”);
SetProperty(“innerHTML”,“@”);
}
}
非常重要的一点:我已经测试了这段代码,它只在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!');"" />");
}
}
public部分类主页面:UserControl
{
公共主页()
{
初始化组件();
已加载+=新路由EventHandler(主页面_已加载);
}
已加载无效主页(对象发送器、路由目标)
{
HtmlDocument hostPage=HtmlPage.Document;
HtmlElement ipAddressHtml=hostPage.GetElementById(“fillWithData”);
SetProperty(“innerHTML”,“@”);
}
}
此代码在IE/FF/Chrome/Opera(当然:)下运行良好。对于我来说,我喜欢第一个动态js和html的例子。我非常确信,修复它并使其被其他浏览器接受是可能的,而不仅仅是Opera。
我希望我的例子能帮助你()