技术开发 频道

我要“闪亮”


添加托管代码
    上面都是使用的设计工具,现在,我们再用VS2008打开工程,在Page.xaml.cs中添加C#代码,为程序增加用户交互体验。
    为“TextBlock”元素的鼠标移入/移出事件添加如下代码:

public void Page_Loaded(object o, EventArgs e)
{
// Required to initialize variables
InitializeComponent();
//声明事件
txtHello.MouseEnter +=new MouseEventHandler(txtHello_MouseEnter);
txtHello.MouseLeave +=new EventHandler(txtHello_MouseLeave);
}

Brush oldBrush;
//鼠标移入
private void txtHello_MouseEnter(object sender, MouseEventArgs e)
{
Timeline1.Pause();//暂停动画
oldBrush = txtHello.Foreground;
//将文字颜色改为红色
txtHello.Foreground = new System.Windows.Media.SolidColorBrush(Colors.Red);
}
//鼠标移出
private void txtHello_MouseLeave(object sender, EventArgs e)
{
//恢复状态
Timeline1.Resume();
txtHello.Foreground = oldBrush;
}
    可以看出,这已经和编写普通的C#的程序没什么区别啦。最后编译运行,效果如图5所示。


   图5 鼠标移入效果

3. 小结

    通过上面的演示可以看到,微软为Silverlight开发提供了强大的工具,有助于设计者和开发人员协同工作。并且对于开发人员来说,如果熟悉微软.NET和Web开发技术,就已经具备了开发Silverlight程序的能力。如果你要从事RIA开发,Silverlight会是一个不错的选择。

0
相关文章