(待修改)
注意,与Visual Studio无关,因为我觉得那个搭建不方便。
后来发现SharpDevelop可以用来开发Silverlight,但好像不能断点调试。
一、安装:
大部分安装包都可以在Microsoft下载中心找到
http://www.microsoft.com/downloads/zh-cn/default.aspx
然后依次安装(Windows XP系统可能预装了,所以有些可以无视)
1. Windows Installer 4.5 Redistributable - 简体中文
WindowsXP-KB942288-v3-x86.exe
http://www.microsoft.com/downloads/zh-cn/details.aspx?FamilyID=5a58b56f-60b6-4412-95b9-54d056d6f9f4
2. Microsoft .NET Framework 4 (Standalone Installer)
(需要环境:
Windows Installer 3.1 或更高版本
Internet Explorer 5.01 或更高版本)
http://www.microsoft.com/downloads/zh-cn/details.aspx?FamilyID=0a391abd-25c1-4fc0-919f-b21f31ab88b7
3. SharpDevelop 4.0
http://www.sharpdevelop.net/OpenSource/SD/Download/#SharpDevelop4x
4. Upgrade Microsoft Silverlight for a better Web experience
http://www.microsoft.com/getsilverlight/Get-Started/Install/Default.aspx
5. Microsoft Silverlight 4 SDK
http://www.microsoft.com/downloads/zh-cn/details.aspx?FamilyID=298170d6-567c-4b90-9917-490ffcddba87
6. Microsoft Silverlight 4 脱机文档
http://www.microsoft.com/downloads/zh-cn/details.aspx?FamilyID=b6127b9b-968c-46c2-8cb6-d228e017ad74
7. SilverLight Runtime(即浏览器的SilverLight组件)
http://www.silverlight.net/getstarted/
右面的INSTALL NOW按钮:
http://go.microsoft.com/fwlink/?LinkID=149156
二、创建工程
运行SharpDevelop,File->New->Solution...->C#->Silverlight->Silverlight Application。
填写Name,然后按Create创建工程。
发现自动生成了两个xaml文件,分别关联两个cs文件。
双击MainPage.xaml,然后在打开文件的下方按Design的标签页打开设计视图。
点击左面Projects视图旁边的Toos视图,把一个Button控件拖放进去。
三、修改工程属性,运行工程
因为Silverlight SDK4对于SharpDevelop有个bug,详细见
http://community.sharpdevelop.net/forums/p/11826/32400.aspx#32400
需要修改平台属性。
对Projects中的工程名右键,在菜单中选择Properties,打开属性页
点击Compiling页,找到Target CPU的下拉框,
把32 bit Intel-compatible processor改为Any processor。
然后菜单->Build->Set platform->Any CPU。
调试时按Debug Run会打开IE。
如果正常应该可以看到浏览器里出现一个按钮。
四、输出调试信息
好像无法下断点进行单步调试,不过可以输出trace信息到DbgView。
因为System.Diagnostics名字空间的Debug可以把内容输出到VS调试窗口。
所以这样修改代码:
首先切换到MainPage.xaml的Design页,点击右面Properties视图,按Events按钮,
在Click事件旁边双击,自动生成
void button1_Click(object sender, RoutedEventArgs e)
然后加入如下代码
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.Threading; using System.Diagnostics; namespace testsl { public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); } void button1_Click(object sender, RoutedEventArgs e) { //Console.WriteLine("hello"); //see http://msdn.microsoft.com/en-us/library/system.diagnostics.debug(v=vs.95).aspx Debug.WriteLine("hello"); //tb2.Text = "hello"; } } }
然后打开DbgView,
见http://technet.microsoft.com/en-us/sysinternals/bb896647
运行后,点击浏览器里的按钮,可以看到DbgView中输出hello。
20110403补充:
使用Debug.Assert()方法则以对话框形式显示堆栈回溯(一种不优雅但可以考虑的方法)
五、反编译dll/xap
可以用.NET Reflector反编译刚才生成的dll文件(拖入dll文件,然后右键->Disassemble)
http://www.red-gate.com/products/dotnet-development/reflector/
如果反编译时弹出对话框Assembly File Selector,可以选择Silverlight安装目录下的相应名称的dll。
<Deployment xmlns="http://schemas.microsoft.com/client/2007/deployment" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" > <Deployment.Parts> </Deployment.Parts> <Deployment.OutOfBrowserSettings> <OutOfBrowserSettings ShortName="Out-of-Browser Application" EnableGPUAcceleration="True" ShowInstallMenuItem="True"> <OutOfBrowserSettings.Blurb> Demonstrates the out-of-browser feature. </OutOfBrowserSettings.Blurb> <OutOfBrowserSettings.WindowSettings> <WindowSettings Title="Out-of-Browser Example" Height="600" Width="600" Left="0" Top="0" WindowStartupLocation="Manual" WindowStyle="SingleBorderWindow"/> </OutOfBrowserSettings.WindowSettings> <OutOfBrowserSettings.SecuritySettings> <SecuritySettings ElevatedPermissions="Required" /> </OutOfBrowserSettings.SecuritySettings> </OutOfBrowserSettings> </Deployment.OutOfBrowserSettings> </Deployment>详细的配置见Silverlight的帮助文档。浏览器运行时右键菜单将多出一项“安装到计算机”或者“删除”。