Silverlight 按钮(使用 Xaml 和 C#)
Test.aspx 页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="XMLPraktijkOpdrachtJavascript.Test" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Test</title>
<script type="text/javascript" src="Javascript/Test.js"></script>
</head>
<body>
<object id="TestPage" width="100%" height="100%" type="application/x-silverlight-2">
<param name="source" value="Xaml/Test.xaml"/>
</object>
</body>
</html>
Test.xaml 页面:
<?xml version="1.0" encoding="UTF-8"?>
<Grid
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Loaded="onLoaded">
<Button Width="50" Height="50" Name="Test">TEST</Button>
</Grid>
Test.js 文件:
// Event function when page gets loaded
function onLoaded() {
alert('In TestPage');
}
没有按钮,它工作正常。有了按钮,就失败了。为什么我无法在 Silverlight 中使用按钮? System.Windows.Controls 已添加到项目中。
Test.aspx page:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="XMLPraktijkOpdrachtJavascript.Test" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Test</title>
<script type="text/javascript" src="Javascript/Test.js"></script>
</head>
<body>
<object id="TestPage" width="100%" height="100%" type="application/x-silverlight-2">
<param name="source" value="Xaml/Test.xaml"/>
</object>
</body>
</html>
Test.xaml page:
<?xml version="1.0" encoding="UTF-8"?>
<Grid
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Loaded="onLoaded">
<Button Width="50" Height="50" Name="Test">TEST</Button>
</Grid>
Test.js file:
// Event function when page gets loaded
function onLoaded() {
alert('In TestPage');
}
Without the button, it works fine. With the button, it fails. Why can't I use a button in Silverlight? The System.Windows.Controls is added to the project.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
阅读您之前问过的一个非常相似的问题后,我'我不确定您是否了解 Silverlight 1 和 Silverlight 2 或更高版本之间的根本区别。
您的
Silverlight 2 及更高版本要求在单独的项目中开发 Silverlight 代码。这些 Silverlight 项目将所有 Silverlight 代码编译到
.xap
文件中。然后,您将 Silverlight 应用程序添加到您的 Web 项目中,这会安排将已编译的.xap
文件复制到您的 Web 项目中的ClientBin
文件夹中。然后,您可以让您的 Web 项目将.xap
文件提供给您的浏览器。在 Silverlight 1 中,.xaml
文件似乎是从您的 Web 项目中提供的。您的项目是否是 Silverlight 1 项目?如果它是现有的应用程序,是否有机会将其迁移到更高版本?如果您一直在忙于维护大型遗留 Silverlight 1 应用程序,我只能说我真的为您感到抱歉。
根本没有理由再编写新的 Silverlight 1 应用程序。它与 Silverlight 的其他版本有根本的不同,并且有很多限制。特别是,Silverlight 1 中没有
Button
,正如有人已经在对您上一个问题的评论中向您指出的那样。这可能是您看到的错误的原因。Having read a very similar question you've asked before, I'm not sure you understand the fundamental difference between Silverlight 1 and Silverlight 2 or later.
Your
<object>
element has the attributetype="application/silverlight-2"
, which specifies Silverlight 2 or later. However, thesource
parameter specifies a.xaml
file, which (I believe) is how Silverlight 1 works.Silverlight 2 and later require the Silverlight code to be developed in a separate project. These Silverlight projects compile all the Silverlight code into a
.xap
file. You then add the Silverlight application to your web project, which arranges for the compiled.xap
file to be copied into aClientBin
folder within your web project. You can then have your web project serve the.xap
file to your browser. With Silverlight 1, it appears that the.xaml
files are served up from within your web project.Is your project a Silverlight 1 project or not? If it's an existing application, is there any chance of migrating it to a later version? If you've been lumbered with maintaining a large legacy Silverlight 1 application, all I can say is I'm really sorry for you.
There is no good reason at all to write new Silverlight 1 applications any more. It's fundamentally different to other versions of Silverlight and it has so many limitations. In particular, there is no
Button
in Silverlight 1, as someone has already pointed out to you in a comment to your previous question. This is likely to be the cause of the error you are seeing.你不能只使用这样的“xaml 文件”。您需要一个“silverlight 项目”来输出可以嵌入到网页中的 xap 文件。
尝试创建一个 silverlight 应用程序,这将创建 silverilght 项目,提示并为您创建一个 Web 应用程序,配置 Web 应用程序以显示 silverlight 项目,并使您更快地启动和运行;)
希望这会有所帮助
you can't just use a "xaml file" like that. You need a "silverlight project" that outputs a xap file which you can embed in your web page.
Try creating a silverlight application, this will create the silverilght project, prompt and create a web application for you, configure the web app to display the silverlight project and gets you up and running more quickly ;)
Hope this helps