OData服务与Silverlight应用的集成

与上一个教程一样,将不使用数据库,而是创建一个简单的集合,将以编程方式创建这个集合。这将允许只看到OData部分。

首先,打开项目,在Visual Studio 2010(或更高版本)中,从下面链接的zip文件中打开:

选择“添加”,然后“新建项目...”创建一个新的Silverlight应用程序。添加一个服务引用。点击“发现”。创建一个名为wsSampleCustomerData的引用。接下来,在Silverlight项目中添加以下程序集引用:

  • System.CoreEx
  • System.Observable
  • System.Reactive

在Silverlight项目中,删除MainPage.xaml。打开项目在Expression Blend 4(或更高版本)中。在Expression Blend中,选择“文件”然后“新建项目...”选择“带有ViewModel的用户控件”模板,并创建一个名为MainPage.xaml的文件。它将创建ViewModel页面(MainPage.xaml和MainPage.xaml.cs与一个已经连接的MainPageModel.cs视图模型页面)。

创建一个名为Model的文件夹和一个名为Model.cs的类。用以下代码替换所有代码:

C# using System; using System.Linq; using System.Collections.Generic; using SilverlightODataSample.wsSampleCustomerData; using System.Data.Services.Client; namespace SilverlightODataSample { public class Model { #region GetCustomers public static IObservable> GetCustomers(int intPage) { // Create a URI that points to theODataService Uri objUri = new Uri(GetBaseAddress(), UriKind.RelativeOrAbsolute); // Set up oData service call SampleDataSource SDS = new SampleDataSource(objUri); // Construct a Query var query = (from SampleCustomerData in SDS.SampleCustomerData where SampleCustomerData.CustomerNotes.Contains("3") select SampleCustomerData).Skip(intPage).Take(10); // Set up a DataServiceCollection to hold the results DataServiceCollection CustomerRecords = new DataServiceCollection(); // Set up a Rx Observable (in a variable called observable) // that will contain the results of // the "LoadCompleted" event that CustomerRecords will fire // When LoadAsync(query) is fired in the following statement IObservable> observable = Observable.FromEvent(CustomerRecords, "LoadCompleted"); // Execute the LoadAsync on CustomerRecords passing // the query that was constructed earlier CustomerRecords.LoadAsync(query); // Return observable return observable; } #endregion #region GetBaseAddress private static string GetBaseAddress() { // This gets the address of the webservice by // getting the AbsoluteUri and then stripping out the // name of the .xap file string strXapFile = @"/ClientBin/SilverlightODataSample.xap"; string strBaseWebAddress = App.Current.Host.Source.AbsoluteUri.Replace(strXapFile, ""); return string.Format("{0}/{1}", strBaseWebAddress, "Service.svc"); } #endregion } }

打开MainPageModel.cs并用以下代码替换所有代码:

C# using System; using System.ComponentModel; using System.Collections.ObjectModel; using SilverlightODataSample.wsSampleCustomerData; using System.Data.Services.Client; namespace SilverlightODataSample { public class MainPageModel : INotifyPropertyChanged { public MainPageModel() { // When the Control loads // Get the Customers GetCustomers(); } #region GetCustomers private void GetCustomers() { // Call the Model to get the Customers // Passing in 0 to get the first page // Paging could easily be done here // You could also pass in other criteria Model.GetCustomers(0).Subscribe(p => { // Check for an error in the Service if ((p.EventArgs.Error == null)) { // loop thru each item in the // DataServiceCollection // Collection foreach (CustomerRecord Customer in (DataServiceCollection)p.Sender) { // Add to the Customer to the colCustomerRecord // Collection so the View can bind to it colCustomerRecord.Add(Customer); } } }); } #endregion #region CustomerRecord // The View will bind to this collection and automatically be notified if // The collection changes. The Designer can bind any UI element that // can hold a collection private ObservableCollection _colCustomerRecord = new ObservableCollection(); public ObservableCollection colCustomerRecord { get { return _colCustomerRecord; } private set { if (colCustomerRecord == value) { return; } _colCustomerRecord = value; this.NotifyPropertyChanged("colCustomerRecord"); } } #endregion #region INotifyPropertyChanged // This is a supporting method to raise a notification for any // Element that is subscribed to a Property that implements // NotifyPropertyChanged public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(String info) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(info)); } } #endregion } }

抓取一个DataGrid并将其拖放到设计表面上。将其加宽以填充页面。点击“数据”选项卡,以便看到数据上下文。将colCustomerRecord集合拖放到DataGrid上。构建并运行项目。

沪ICP备2024098111号-1
上海秋旦网络科技中心:上海市奉贤区金大公路8218号1幢 联系电话:17898875485