1创建silverlight应用程序
2 在解决方案右击属性添加新项 添加 linq to sql
3 把数据库中的表复制到linq to sql类中(注意:以下截图中红色方框需要自己修改)
4 为项目添加 wcf服务
5为wcf服务类文件中添加显示数据的函数其代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.ServiceModel.Activation; using StuEntity; using StuContent; using System.Collections.Generic; using System.Text; namespace SilverlightApplicationTest.Web { [ServiceContract(Namespace = "")] [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class ServiceStuInfo { [OperationContract] public void DoWork() { // 在此处添加操作实现 return; } [OperationContract] public List<StuMsg> GetAllMsg() { StuTestDataContext stuTest = new StuTestDataContext(); var result = from info in stuTest.StuMsg select info; return result.ToList<StuMsg>(); } // 在此处添加更多操作并使用 [OperationContract] 标记它们 } }
6 为mainpage.xaml布局datagrid控件 button按钮
7 项目添加服务引用和添加System.Data.Services.Client
8 mainpage.xaml后台代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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.Data.Services.Client; using SilverlightApplicationTest.StuServiceReference; namespace SilverlightApplicationTest { public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); this.btnGet.Click += new RoutedEventHandler(btnGet_Click); } private void btnGet_Click(object sender, RoutedEventArgs e) { ServiceStuInfoClient stu = new ServiceStuInfoClient(); stu.GetAllMsgAsync(); stu.GetAllMsgCompleted += new EventHandler<GetAllMsgCompletedEventArgs>(stu_GetAllMsgCompleted); } private void stu_GetAllMsgCompleted(object sender, GetAllMsgCompletedEventArgs e) { dgrid.ItemsSource = e.Result; } } }
9 按F5调试运行