首先需要下载:
如果你不到Microsoft.Windows.Data.DomainServices.dll,请关闭VS2010下载并安装以上两个安装包,特别是RiaService Toolkit(April 2011)
主要代码如下:
EmployeeList.xaml
<navigation:Page xmlns:riaControls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.DomainServices" x:Class="BusinessApplication1.Views.EmployeeList"
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"
xmlns:dataForm="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.DataForm.Toolkit"
xmlns:ds="clr-namespace:BusinessApplication1.Web"
mc:Ignorable="d"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
d:DesignWidth="640" d:DesignHeight="480"
Title="EmployeeList Page" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
<Grid x:Name="LayoutRoot">
<ScrollViewer x:Name="PageScrollViewer" Style="{StaticResource
PageScrollViewerStyle}" >
<StackPanel x:Name="ContentStackPanel" Style="{StaticResource ContentStackPanelStyle}">
<TextBlock Text="Employee List" Style="{StaticResource HeaderTextStyle}"/>
<StackPanel Orientation="Horizontal"
HorizontalAlignment="Left">
<TextBlock VerticalAlignment="Center"
Text="Min Vacation Hours Filter" />
<TextBox x:Name="vacationHoursText" Width="75"
FontSize="11" Margin="4" Text="0"/>
</StackPanel>
<sdk:DataGrid AutoGenerateColumns="True" IsReadOnly="True" Name="dataGrid1" MinHeight="100" Height="Auto" ItemsSource="{Binding Data, ElementName=employeeDataSource}" />
<riaControls:DomainDataSource Name="employeeDataSource" LoadSize="20" QueryName="GetCustomerQuery" AutoLoad="True" SubmittedChanges="employeeDataSource_SubmittedChanges">
<riaControls:DomainDataSource.DomainContext>
<ds:OrganizationContext/>
</riaControls:DomainDataSource.DomainContext>
<riaControls:DomainDataSource.SortDescriptors>
<riaControls:SortDescriptor PropertyPath="Email" Direction="Ascending" />
</riaControls:DomainDataSource.SortDescriptors>
<riaControls:DomainDataSource.FilterDescriptors>
<riaControls:FilterDescriptor
PropertyPath="Phone"
Operator="IsGreaterThanOrEqualTo"
IgnoredValue=""
Value="{Binding ElementName=vacationHoursText, Path=Text}" >
</riaControls:FilterDescriptor>
</riaControls:DomainDataSource.FilterDescriptors>
</riaControls:DomainDataSource>
<sdk:DataPager PageSize="5" Source="{Binding Data, ElementName=employeeDataSource}" HorizontalAlignment="Left" />
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Margin="0,12,0,0">
<Button x:Name="addNewEmployee" Width="90" Height="23" Content="Add Employee" Margin="4,0,0,0" Click="addNewEmployee_Click"/>
</StackPanel>
<dataForm:DataForm x:Name="dataForm1" Header="Employee Information"
AutoGenerateFields="False" HorizontalAlignment="Left"
AutoEdit="False" AutoCommit="False" Width="400"
CurrentItem="{Binding SelectedItem, ElementName=dataGrid1}" Margin="0,12,0,0">
<dataForm:DataForm.EditTemplate>
<DataTemplate>
<StackPanel>
<dataForm:DataField Label="Employee GUID">
<TextBox IsReadOnly="True"
Text="{Binding CustomerGUID, Mode=OneWay}" />
</dataForm:DataField>
<dataForm:DataField Label=" ID">
<TextBox Text="{Binding ID, Mode=TwoWay}" />
</dataForm:DataField>
<dataForm:DataField Label="Modified Date">
<TextBox Text="{Binding ModifiedDate, Mode=TwoWay}" />
</dataForm:DataField>
<dataForm:DataField Label="Name">
<TextBox Text="{Binding Name, Mode=TwoWay}" />
</dataForm:DataField>
</StackPanel>
</DataTemplate>
</dataForm:DataForm.EditTemplate>
</dataForm:DataForm>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Margin="0,12,0,0">
<Button x:Name="submitButton" Width="75" Height="23"
Content="Submit" Margin="4,0,0,0" Click="submitButton_Click"/>
</StackPanel>
</StackPanel>
</ScrollViewer>
</Grid>
</navigation:Page>
EmployeeList.xaml.cs
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 BusinessApplication1.Web;
using System.ServiceModel.DomainServices.Client;
namespace BusinessApplication1.Views
{
public partial class EmployeeList : Page
{
//OrganizationContext _OrganizationContext = new OrganizationContext();
public EmployeeList()
{
InitializeComponent();
//this.dataGrid1.ItemsSource = _OrganizationContext.Customers;
//_OrganizationContext.Load(_OrganizationContext.GetCustomerQuery());
}
// Executes when the user navigates to this page.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
private void submitButton_Click(object sender, RoutedEventArgs e)
{
submitButton.IsEnabled = false;
employeeDataSource.SubmitChanges();
}
private void employeeDataSource_SubmittedChanges(object sender, SubmittedChangesEventArgs e)
{
if (e.HasError)
{
MessageBox.Show(string.Format("Changes were not saved: {0}", e.Error.Message));
e.MarkErrorAsHandled();
}
submitButton.IsEnabled = true;
}
private void addNewEmployee_Click(object sender, RoutedEventArgs e)
{
EmployeeRegistrationWindow addEmp = new EmployeeRegistrationWindow();
addEmp.Closed += new EventHandler(addEmp_Closed);
addEmp.Show();
}
void addEmp_Closed(object sender, EventArgs e)
{
EmployeeRegistrationWindow emp = (EmployeeRegistrationWindow)sender;
if (emp.NewCustomer != null)
{
OrganizationContext _OrganizationContext = (OrganizationContext)(employeeDataSource.DomainContext);
_OrganizationContext.Customers.Add(emp.NewCustomer);
employeeDataSource.SubmitChanges();
}
}
}
}
PurchaseOrders.xaml
<navigation:Page x:Class="BusinessApplication1.Views.PurchaseOrders"
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"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
d:DesignWidth="640" d:DesignHeight="480"
Title="PurchaseOrders Page" xmlns:riaControls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.DomainServices" xmlns:my="clr-namespace:BusinessApplication1.Web" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
<sdk:Page.Resources>
<CollectionViewSource x:Key="customerCustomerCardViewSource" Source="{Binding Path=Data.CustomerCard, ElementName=customerDomainDataSource}" />
</sdk:Page.Resources>
<Grid x:Name="LayoutRoot">
<ScrollViewer x:Name="PageScrollViewer" Style="{StaticResource
PageScrollViewerStyle}" >
<StackPanel x:Name="ContentStackPanel" Style="{StaticResource ContentStackPanelStyle}">
<TextBlock Text="Purchase Orders" Style="{StaticResource HeaderTextStyle}"/>
<riaControls:DomainDataSource AutoLoad="True" d:DesignData="{d:DesignInstance my:Customer, CreateList=true}" Height="0" LoadedData="customerDomainDataSource_LoadedData" Name="customerDomainDataSource" QueryName="GetPurchaseOrderHeaders" Width="0">
<riaControls:DomainDataSource.DomainContext>
<my:OrganizationContext />
</riaControls:DomainDataSource.DomainContext>
<!--<riaControls:DomainDataSource.FilterDescriptors>
<riaControls:FilterDescriptor PropertyPath="ID" Operator="IsLessThan" Value="100"></riaControls:FilterDescriptor>
</riaControls:DomainDataSource.FilterDescriptors>-->
</riaControls:DomainDataSource>
<TextBlock Text="Order Headers"></TextBlock>
<sdk:DataGrid AutoGenerateColumns="False" Height="200" ItemsSource="{Binding ElementName=customerDomainDataSource, Path=Data}" Name="customerDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" >
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn x:Name="address1Column" Binding="{Binding Path=Address1}" Header="Address 1" Width="SizeToHeader" />
<sdk:DataGridTextColumn x:Name="address2Column" Binding="{Binding Path=Address2}" Header="Address 2" Width="SizeToHeader" />
<sdk:DataGridTextColumn x:Name="customerGUIDColumn" Binding="{Binding Path=CustomerGUID, Mode=OneWay}" Header="Customer GUID" IsReadOnly="True" Width="SizeToHeader" />
<sdk:DataGridTextColumn x:Name="emailColumn" Binding="{Binding Path=Email}" Header="Email" Width="SizeToHeader" />
<sdk:DataGridTextColumn x:Name="iDColumn" Binding="{Binding Path=ID}" Header="ID" Width="SizeToHeader" />
<sdk:DataGridTextColumn x:Name="modifiedByColumn" Binding="{Binding Path=ModifiedBy}" Header="Modified By" Width="SizeToHeader" />
<sdk:DataGridTemplateColumn x:Name="modifiedDateColumn" Header="Modified Date" Width="SizeToHeader">
<sdk:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<sdk:DatePicker SelectedDate="{Binding Path=ModifiedDate, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellEditingTemplate>
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=ModifiedDate, StringFormat=\{0:d\}}" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
<sdk:DataGridTextColumn x:Name="nameColumn" Binding="{Binding Path=Name}" Header="Name" Width="SizeToHeader" />
<sdk:DataGridTextColumn x:Name="phoneColumn" Binding="{Binding Path=Phone}" Header="Phone" Width="SizeToHeader" />
<sdk:DataGridTextColumn x:Name="postCodeGUIDColumn" Binding="{Binding Path=PostCodeGUID}" Header="Post Code GUID" Width="SizeToHeader" />
<sdk:DataGridTextColumn x:Name="priceCategoryGUIDColumn" Binding="{Binding Path=PriceCategoryGUID}" Header="Price Category GUID" Width="SizeToHeader" />
<sdk:DataGridTextColumn x:Name="salaryIDColumn" Binding="{Binding Path=SalaryID}" Header="Salary ID" Width="SizeToHeader" />
<sdk:DataGridTextColumn x:Name="shortNameColumn" Binding="{Binding Path=ShortName}" Header="Short Name" Width="SizeToHeader" />
</sdk:DataGrid.Columns>
</sdk:DataGrid>
<TextBlock Text="Order Details"></TextBlock>
<sdk:DataGrid AutoGenerateColumns="False" Height="200" ItemsSource="{Binding Source={StaticResource customerCustomerCardViewSource}}" Name="customerCardDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" >
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn x:Name="balanceColumn" Binding="{Binding Path=Balance}" Header="Balance" Width="SizeToHeader" />
<sdk:DataGridCheckBoxColumn x:Name="checkLimitColumn" Binding="{Binding Path=CheckLimit}" Header="Check Limit" Width="SizeToHeader" />
<sdk:DataGridTextColumn x:Name="commentColumn" Binding="{Binding Path=Comment}" Header="Comment" Width="SizeToHeader" />
<sdk:DataGridTextColumn x:Name="creditLimitColumn" Binding="{Binding Path=CreditLimit}" Header="Credit Limit" Width="SizeToHeader" />
<sdk:DataGridTextColumn x:Name="customerCardGUIDColumn" Binding="{Binding Path=CustomerCardGUID, Mode=OneWay}" Header="Customer Card GUID" IsReadOnly="True" Width="SizeToHeader" />
<sdk:DataGridTextColumn x:Name="customerCardTemplateGUIDColumn" Binding="{Binding Path=CustomerCardTemplateGUID}" Header="Customer Card Template GUID" Width="SizeToHeader" />
<sdk:DataGridTextColumn x:Name="customerCompanyGUIDColumn" Binding="{Binding Path=CustomerCompanyGUID}" Header="Customer Company GUID" Width="SizeToHeader" />
<sdk:DataGridTextColumn x:Name="customerGUIDColumn1" Binding="{Binding Path=CustomerGUID}" Header="Customer GUID" Width="SizeToHeader" />
<sdk:DataGridTextColumn x:Name="discountRuleGUIDColumn" Binding="{Binding Path=DiscountRuleGUID}" Header="Discount Rule GUID" Width="SizeToHeader" />
<sdk:DataGridTemplateColumn x:Name="expiresDateColumn" Header="Expires Date" Width="SizeToHeader">
<sdk:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<sdk:DatePicker SelectedDate="{Binding Path=ExpiresDate, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true, TargetNullValue=''}" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellEditingTemplate>
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=ExpiresDate, StringFormat=\{0:d\}}" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
<sdk:DataGridTextColumn x:Name="iDColumn1" Binding="{Binding Path=ID}" Header="ID" Width="SizeToHeader" />
<sdk:DataGridCheckBoxColumn x:Name="isCardValidColumn" Binding="{Binding Path=IsCardValid}" Header="Is Card Valid" Width="SizeToHeader" />
<sdk:DataGridTextColumn x:Name="modifiedByColumn1" Binding="{Binding Path=ModifiedBy}" Header="Modified By" Width="SizeToHeader" />
<sdk:DataGridTemplateColumn x:Name="modifiedDateColumn1" Header="Modified Date" Width="SizeToHeader">
<sdk:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<sdk:DatePicker SelectedDate="{Binding Path=ModifiedDate, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellEditingTemplate>
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=ModifiedDate, StringFormat=\{0:d\}}" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
<sdk:DataGridTextColumn x:Name="priceCategoryGUIDColumn1" Binding="{Binding Path=PriceCategoryGUID}" Header="Price Category GUID" Width="SizeToHeader" />
<sdk:DataGridTextColumn x:Name="revisionColumn" Binding="{Binding Path=Revision}" Header="Revision" Width="SizeToHeader" />
<sdk:DataGridTextColumn x:Name="typeOfCardColumn" Binding="{Binding Path=TypeOfCard}" Header="Type Of Card" Width="SizeToHeader" />
<sdk:DataGridTextColumn x:Name="warningLimitColumn" Binding="{Binding Path=WarningLimit}" Header="Warning Limit" Width="SizeToHeader" />
</sdk:DataGrid.Columns>
</sdk:DataGrid>
</StackPanel>
</ScrollViewer>
</Grid>
</navigation:Page>
OrganizationService.cs
namespace BusinessApplication1.Web
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Linq;
using System.ServiceModel.DomainServices.EntityFramework;
using System.ServiceModel.DomainServices.Hosting;
using System.ServiceModel.DomainServices.Server;
// Implements application logic using the KDRGoldEntities context.
// TODO: Add your application logic to these methods or in additional methods.
// TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
// Also consider adding roles to restrict access as appropriate.
// [RequiresAuthentication]
[EnableClientAccess()]
public class OrganizationService : LinqToEntitiesDomainService<KDRGoldEntities>
{
// TODO:
// Consider constraining the results of your query method. If you need additional input you can
// add parameters to this method or create additional query methods with different names.
// To support paging you will need to add ordering to the 'Customer' query.
public IQueryable<Customer> GetCustomer()
{
return this.ObjectContext.Customer.OrderBy(e => e.ID);
}
public void InsertCustomer(Customer customer)
{
customer.Phone = "888888";
customer.ModifiedBy = "ss";
customer.ModifiedDate = DateTime.Now;
customer.CustomerGUID = Guid.NewGuid();
customer.PostCodeGUID = Guid.Parse("567AB4B6-4057-4097-B58D-030CF6DB8E1C");
customer.PriceCategoryGUID = Guid.Parse("E3CB6B6C-7573-4625-8A6B-8689454CABF4");
if ((customer.EntityState != EntityState.Detached))
{
this.ObjectContext.ObjectStateManager.ChangeObjectState(customer, EntityState.Added);
}
else
{
this.ObjectContext.Customer.AddObject(customer);
}
}
public IQueryable<Customer> GetPurchaseOrderHeaders()
{
return this.ObjectContext.Customer.Include("CustomerCard").OrderBy(p => p.ID);
}
public void UpdateCustomer(Customer currentCustomer)
{
this.ObjectContext.Customer.AttachAsModified(currentCustomer, this.ChangeSet.GetOriginal(currentCustomer));
}
public void DeleteCustomer(Customer customer)
{
if ((customer.EntityState != EntityState.Detached))
{
this.ObjectContext.ObjectStateManager.ChangeObjectState(customer, EntityState.Deleted);
}
else
{
this.ObjectContext.Customer.Attach(customer);
this.ObjectContext.Customer.DeleteObject(customer);
}
}
// TODO:
// Consider constraining the results of your query method. If you need additional input you can
// add parameters to this method or create additional query methods with different names.
// To support paging you will need to add ordering to the 'CustomerCard' query.
public IQueryable<CustomerCard> GetCustomerCard()
{
return this.ObjectContext.CustomerCard;
}
public void InsertCustomerCard(CustomerCard customerCard)
{
if ((customerCard.EntityState != EntityState.Detached))
{
this.ObjectContext.ObjectStateManager.ChangeObjectState(customerCard, EntityState.Added);
}
else
{
this.ObjectContext.CustomerCard.AddObject(customerCard);
}
}
public void UpdateCustomerCard(CustomerCard currentCustomerCard)
{
this.ObjectContext.CustomerCard.AttachAsModified(currentCustomerCard, this.ChangeSet.GetOriginal(currentCustomerCard));
}
public void DeleteCustomerCard(CustomerCard customerCard)
{
if ((customerCard.EntityState != EntityState.Detached))
{
this.ObjectContext.ObjectStateManager.ChangeObjectState(customerCard, EntityState.Deleted);
}
else
{
this.ObjectContext.CustomerCard.Attach(customerCard);
this.ObjectContext.CustomerCard.DeleteObject(customerCard);
}
}
}
}
OrganizationService.metadata.cs
namespace BusinessApplication1.Web
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data.Objects.DataClasses;
using System.Linq;
using System.ServiceModel.DomainServices.Hosting;
using System.ServiceModel.DomainServices.Server;
// The MetadataTypeAttribute identifies CustomerMetadata as the class
// that carries additional metadata for the Customer class.
[MetadataTypeAttribute(typeof(Customer.CustomerMetadata))]
public partial class Customer
{
// This class allows you to attach custom attributes to properties
// of the Customer class.
//
// For example, the following marks the Xyz property as a
// required property and specifies the format for valid values:
// [Required]
// [RegularExpression("[A-Z][A-Za-z0-9]*")]
// [StringLength(32)]
// public string Xyz { get; set; }
internal sealed class CustomerMetadata
{
// Metadata classes are not meant to be instantiated.
private CustomerMetadata()
{
}
public string Address1 { get; set; }
public string Address2 { get; set; }
[Include]
[Composition]
public EntityCollection<CustomerCard> CustomerCard { get; set; }
public Guid CustomerGUID { get; set; }
public string Email { get; set; }
public string ID { get; set; }
public string ModifiedBy { get; set; }
public DateTime ModifiedDate { get; set; }
[Required]
public string Name { get; set; }
public string Phone { get; set; }
public Nullable<Guid> PostCodeGUID { get; set; }
public Nullable<Guid> PriceCategoryGUID { get; set; }
public string SalaryID { get; set; }
public string ShortName { get; set; }
}
}
// The MetadataTypeAttribute identifies CustomerCardMetadata as the class
// that carries additional metadata for the CustomerCard class.
[MetadataTypeAttribute(typeof(CustomerCard.CustomerCardMetadata))]
public partial class CustomerCard
{
// This class allows you to attach custom attributes to properties
// of the CustomerCard class.
//
// For example, the following marks the Xyz property as a
// required property and specifies the format for valid values:
// [Required]
// [RegularExpression("[A-Z][A-Za-z0-9]*")]
// [StringLength(32)]
// public string Xyz { get; set; }
internal sealed class CustomerCardMetadata
{
// Metadata classes are not meant to be instantiated.
private CustomerCardMetadata()
{
}
public decimal Balance { get; set; }
public bool CheckLimit { get; set; }
public string Comment { get; set; }
public Nullable<decimal> CreditLimit { get; set; }
public Customer Customer { get; set; }
public Guid CustomerCardGUID { get; set; }
public Nullable<Guid> CustomerCardTemplateGUID { get; set; }
public Nullable<Guid> CustomerCompanyGUID { get; set; }
public Guid CustomerGUID { get; set; }
public Nullable<Guid> DiscountRuleGUID { get; set; }
public Nullable<DateTime> ExpiresDate { get; set; }
public string ID { get; set; }
public bool IsCardValid { get; set; }
public string ModifiedBy { get; set; }
public DateTime ModifiedDate { get; set; }
public Nullable<Guid> PriceCategoryGUID { get; set; }
public Nullable<int> Revision { get; set; }
public int TypeOfCard { get; set; }
public decimal WarningLimit { get; set; }
}
}
}
以下是截图: