WEB开发网
开发学院WEB开发ASP.NET 稳扎稳打Silverlight(56) - 4.0通信之与 WCF Data... 阅读

稳扎稳打Silverlight(56) - 4.0通信之与 WCF Data Services 进行通信

 2010-10-12 12:29:56 来源:WEB开发网   
核心提示: Demo.xaml.cs代码/** 演示 Silverlight 4.0 与 WCF Data Services 之间的通信** DataServiceCollection<T> - 继承自 ObservableCollection<T>,用于将 WCF Data Ser

Demo.xaml.cs

代码

/*
 * 演示 Silverlight 4.0 与 WCF Data Services 之间的通信
 *
 * DataServiceCollection<T> - 继承自 ObservableCollection<T>。用于将 WCF Data Services 提供的数据绑定到 Silverlight 控件上,当数据发生变化时可提供通知
 *     LoadAsync(IQueryable<T>) - 从数据服务上异步加载 IQueryable<T> 数据到 DataServiceCollection<T> 中(在 LoadCompleted 触发之前不能再调用此方法)
 *     LoadAsync() - 当 DataServiceCollection<T> 表示为某实体的导航属性时,从数据服务上异步加载该数据(在 LoadCompleted 触发之前不能再调用此方法)
 *     LoadCompleted - 异步加载完成后所触发的事件
 *         LoadCompletedEventArgs - LoadCompleted 事件的事件参数
 *         LoadCompletedEventArgs.QueryOperationResponse - 获取数据服务对改查询操作的响应
 *             QueryOperationResponse.Error - 引发的异常
 *             QueryOperationResponse.Headers -  HTTP 的响应头
 *             QueryOperationResponse.Query - 对应的 DataServiceQuery(DataServiceQuery.RequestUri - 请求数据服务的 URI) 对象
 *             QueryOperationResponse.StatusCode - HTTP 的响应代码
 *     Continuation - 返回一个 DataServiceQueryContinuation<T> 对象,该对象封装了当前查询结果的下一页的 URI
 *         DataServiceQueryContinuation<T>.NextLinkUri - 当前查询结果的下一页的 URI
 *     LoadNextPartialSetAsync() - 加载下一页数据到 DataServiceCollection<T> 中
 *
 * Silverlight 通过 ObservableCollection<T> 与 ADO.NET Data Services 1.0 进行通信详见 http://www.cnblogs.com/webabcd/archive/2009/03/12/1409281.html
 * WCF Data Services(ADO.NET Data Services 1.5)新特性详见 http://www.cnblogs.com/webabcd/archive/2010/06/11/1756071.html
 */

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.Windows.Navigation;

using Silverlight40.MyWCFDataServiceReference;
using System.Data.Services.Client;

namespace Silverlight40.WCFDataServices
{
    public partial class Demo : Page
    {
        private NorthwindEntities _context;
        private DataServiceCollection<Category> _categories;

        public Demo()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // 指定数据服务地址
            _context = new NorthwindEntities(new Uri("Service/MyWCFDataService.svc", UriKind.Relative));

            _categories = new DataServiceCollection<Category>();
            _categories.LoadCompleted += new EventHandler<LoadCompletedEventArgs>(_categories_LoadCompleted);

            // 演示如何通过 WCF Data Services 做服务端分页
            ServerPagingDemo();
        }

        // 开始异步加载 Categories 数据
        private void btnGetData_Click(object sender, RoutedEventArgs e)
        {
            gridCategory.DataContext = null;
            gridProduct.DataContext = null;

            // 加载 Categories 表中的全部数据
            var query = from c in _context.Categories select c;
            _categories.LoadAsync(query);

            btnGetData.IsEnabled = false;
        }

        // 异步加载 Categories 数据完成
        void _categories_LoadCompleted(object sender, LoadCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                if (_categories.Continuation != null)
                {
                    // 在数据服务中 Categories 被服务端分页了,所以需要不断调用 LoadNextPartialSetAsync() ,以便加载全部的 Categories 数据
                    _categories.LoadNextPartialSetAsync();
                }
                else
                {
                    gridCategory.DataContext = _categories;
                    gridCategory.SelectedIndex = 0;

                    btnGetData.IsEnabled = true;
                }
            }
            else
            {
                MessageBox.Show(e.Error.ToString());
                btnGetData.IsEnabled = true;
            }
        }

        // 开始异步加载指定 Category 下的 Products 数据
        private void gridCategory_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Category category = gridCategory.SelectedItem as Category;

            if (category != null)
            {
                if (category.Products.Count == 0)
                {
                    category.Products.LoadCompleted += new EventHandler<LoadCompletedEventArgs>(Products_LoadCompleted);

                    try
                    {
                        // 加载实体的导航属性数据
                        category.Products.LoadAsync();
                    }
                    catch (InvalidOperationException ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }
                else
                {
                    gridProduct.DataContext = category.Products;
                }
            }
        }

        // 异步加载 Products 数据完成
        void Products_LoadCompleted(object sender, LoadCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                DataServiceCollection<Product> products = sender as DataServiceCollection<Product>;

                if (products.Continuation != null)
                {
                    try
                    {
                        products.LoadNextPartialSetAsync();
                    }
                    catch (InvalidOperationException ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }
                else
                {
                    gridProduct.DataContext = products;
                }
            }
            else
            {
                MessageBox.Show(e.Error.ToString());
            }
        }

        // 异步保存 Categories 中的被更新的数据(包括增删改),由于 Products 是 Category 的导航属性,所以相关的 Products 的数据的更新也会被保存
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                /*
                 * NorthwindEntities - 继承自 DataServiceContext,在 VS 中引用服务后会自动生成这个对象
                 *     DataServiceContext.BeginSaveChanges() - 将有变化的数据提交至数据服务端进行更新
                 *     DataServiceContext.EndSaveChanges() - 获取 BeginSaveChanges() 操作的结果,返回一个 DataServiceResponse 类型的对象
                 */

                _context.BeginSaveChanges(SaveChangesOptions.Batch, OnChangesSaved, null);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        // 数据被异步保存后
        private void OnChangesSaved(IAsyncResult result)
        {
            bool error = false;

            this.Dispatcher.BeginInvoke(() =>
            {
                try
                {
                    DataServiceResponse response = _context.EndSaveChanges(result);

                    foreach (ChangeOperationResponse changeResponse in response)
                    {
                        if (changeResponse.Error != null)
                            error = true;
                    }

                    if (!error)
                    {
                        MessageBox.Show("数据更新成功");
                    }
                    else
                    {
                        MessageBox.Show("数据更新失败");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            });
        }

 

        private int _pageIndex = 0;
        private int _pageSize = 3;

        // 演示如何通过 WCF Data Services 做服务端分页
        private void ServerPagingDemo()
        {
            BindOrder();

            btnNext.Click += (s, args) =>
            {
                _pageIndex++;
                BindOrder();
            };

            btnPrev.Click += (s, args) =>
            {
                _pageIndex--;
                BindOrder();
            };
        }

        private void BindOrder()
        {
            txtCurrentPageIndex.Text = "当前页索引:" + _pageIndex.ToString();

            var query = from o in _context.Orders select o;
            query = query.Skip(_pageIndex * _pageSize).Take(_pageSize);
            DataServiceCollection<Order> orders = new DataServiceCollection<Order>();
            orders.LoadAsync(query);

            orders.LoadCompleted += (s, args) =>
            {
                gridOrder.DataContext = orders;
            };
        }
    }
}

OK

源码下载:http://files.cnblogs.com/webabcd/Silverlight.rar

上一页  1 2 3 

Tags:稳扎稳打 Silverlight 通信

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接