silverlight体验之二:HelloWorld
HelloWorld似乎已经成了技术尝鲜的代名词了,关于Silverlight的学习是按照TerryLee的路线进行的。
来进行我的第一个SilverLight小例子吧。
1.创建项目
还需要建立一个Web Application Project项目,托管Silverlight项目。自我感觉Silverlight就像个封装好的控件。
2.编译后ClientBin文件夹中多了个SilverlightApplication3.xap文件,感觉类似于Silverlight项目的dll文件等的压缩包。
里面压缩了这些东西
在MainPage.XAML中创建按钮
<UserControl x:Class="SilverlightApplication3.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
<Grid x:Name="LayoutRoot">
<Button x:Name="myButton" Content="Click Me"
Width="240" Height="100"
FontSize="24" Foreground="#FF7800"
Click="myButton_Click">
</Button>
</Grid>
</UserControl>
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
<Grid x:Name="LayoutRoot">
<Button x:Name="myButton" Content="Click Me"
Width="240" Height="100"
FontSize="24" Foreground="#FF7800"
Click="myButton_Click">
</Button>
</Grid>
</UserControl>
MainPage.XAML.cs文件中加入按钮变化,很熟悉的C#代码
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;
namespace SilverlightApplication3
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
private void myButton_Click(object sender, RoutedEventArgs e)
{
this.myButton.Content = "clicked";
this.myButton.Background = new SolidColorBrush(Colors.Red);
}
}
}
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;
namespace SilverlightApplication3
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
private void myButton_Click(object sender, RoutedEventArgs e)
{
this.myButton.Content = "clicked";
this.myButton.Background = new SolidColorBrush(Colors.Red);
}
}
}
之后在web项目中使用即可
<asp:Silverlight ID="Items" runat="server" Source="~/ClientBin/SilverlightApplication3.xap" Width="100%" Height="100%" />
执行效果如下:
PS:
这个小例子中也遇到了些问题,找不到System.web.Silverlight。最后没办法在一个项目包中拿来一个用。不知是不是因为TerryLee的演示时Silverlight2。而我使用的是3,期间有了什么变化。至今仍困惑。
转载于:https://www.cnblogs.com/BuildLife/archive/2010/02/03/1662739.html