技术开发 频道

Microsoft .NET RIA Services快速上手

  点击Show All Files,你会发现系统已经为你生成了客户端的类,放在了Generated_Code目录下。我们将它include进来。

无标题10 无标题11

  这个RiaServices_1.Web.g.cs,是服务端RdChat_DomainService.cs的对应客户端版本。它包含了所有服务端版本类中定义的方法、实体等,而可在客户端直接调用。它打代码如下:

//------------------------------------------------------------------------------
//
//     This code was generated by a tool.
//     Runtime Version:2.0.50727.3053
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
//
//------------------------------------------------------------------------------

namespace RiaServices_1.Web
{
    
using System;
    
using System.Collections.Generic;
    
using System.ComponentModel;
    
using System.ComponentModel.DataAnnotations;
    
using System.Linq;
    
using System.Runtime.Serialization;
    
using System.Web.Ria.Data;
    
using System.Windows.Ria.Data;
    
    
    [DataContract(Namespace
="http://schemas.datacontract.org/2004/07/RiaServices_1.Web")]
    
public sealed partial class Messages : Entity
    {
        
        
private string _body;
        
        
private string _name;
        
        
private string _partitionKey;
        
        
private string _rowKey;
        
        
private Nullable<DateTime> _timestamp;
        
        [DataMember()]
        
public string Body
        {
            
get
            {
                
return this._body;
            }
            
set
            {
                
if ((this._body != value))
                {
                    
this.ValidateProperty("Body", value);
                    
this.RaiseDataMemberChanging("Body");
                    
this._body = value;
                    
this.RaiseDataMemberChanged("Body");
                }
            }
        }
        
        [DataMember()]
        
public string Name
        {
            
get
            {
                
return this._name;
            }
            
set
            {
                
if ((this._name != value))
                {
                    
this.ValidateProperty("Name", value);
                    
this.RaiseDataMemberChanging("Name");
                    
this._name = value;
                    
this.RaiseDataMemberChanged("Name");
                }
            }
        }
        
        [DataMember()]
        [Key()]
        
public string PartitionKey
        {
            
get
            {
                
return this._partitionKey;
            }
            
set
            {
                
if ((this._partitionKey != value))
                {
                    
this.ValidateProperty("PartitionKey", value);
                    
this.RaiseDataMemberChanging("PartitionKey");
                    
this._partitionKey = value;
                    
this.RaiseDataMemberChanged("PartitionKey");
                }
            }
        }
        
        [DataMember()]
        [Key()]
        
public string RowKey
        {
            
get
            {
                
return this._rowKey;
            }
            
set
            {
                
if ((this._rowKey != value))
                {
                    
this.ValidateProperty("RowKey", value);
                    
this.RaiseDataMemberChanging("RowKey");
                    
this._rowKey = value;
                    
this.RaiseDataMemberChanged("RowKey");
                }
            }
        }
        
        [DataMember()]
        
public Nullable<DateTime> Timestamp
        {
            
get
            {
                
return this._timestamp;
            }
            
set
            {
                
if ((this._timestamp != value))
                {
                    
this.ValidateProperty("Timestamp", value);
                    
this.RaiseDataMemberChanging("Timestamp");
                    
this._timestamp = value;
                    
this.RaiseDataMemberChanged("Timestamp");
                }
            }
        }
        
        
public override object GetIdentity()
        {
            
return EntityKey.Create(this._partitionKey, this._rowKey);
        }
    }
    
    
public sealed partial class RdChat_DomainContext : DomainContext
    {
        
        
#region Query root fields
        
private static IQueryable<Messages> _MessagesQuery = new Messages[0].AsQueryable();
        
#endregion
        
        
///
        
/// Default constructor.
        
///
        public RdChat_DomainContext() :
                
base(new HttpDomainClient(new Uri("DataService.axd/RiaServices_1-Web-RdChat_DomainService/", System.UriKind.Relative)))
        {
        }
        
        
///
        
/// Constructor used to specify a data service URI.
        
///
        
///
        
/// The RdChat_DomainService data service URI.
        
///
        public RdChat_DomainContext(Uri serviceUri) :
                
base(new HttpDomainClient(serviceUri))
        {
        }
        
        
///
        
/// Constructor used to specify a DomainClient instance.
        
///
        
///
        
/// The DomainClient instance the DomainContext should use.
        
///
        public RdChat_DomainContext(DomainClient domainClient) :
                
base(domainClient)
        {
        }
        
        
public EntityList<Messages> Messages
        {
            
get
            {
                
return this.Entities.GetEntityList<Messages>();
            }
        }
        
        
#region Query root properties
        
public static IQueryable<Messages> MessagesQuery
        {
            
get
            {
                
return _MessagesQuery;
            }
        }
        
#endregion
        
        
#region LoadMessages method overloads
        
///
        
/// Invokes the server-side method 'GetMessages' and loads the result into .
        
///
        [LoadMethod(typeof(Messages))]
        
public void LoadMessages(IQueryable<Messages> query, MergeOption mergeOption, object userState)
        {
            
base.Load("GetMessages", null, query, mergeOption, userState);
        }
        
        
///
        
/// Invokes the server-side method 'GetMessages' and loads the result into .
        
///
        [LoadMethod(typeof(Messages))]
        
public void LoadMessages()
        {
            
this.LoadMessages(null, MergeOption.KeepCurrentValues, null);
        }
        
        
///
        
/// Invokes the server-side method 'GetMessages' and loads the result into .
        
///
        [LoadMethod(typeof(Messages))]
        
public void LoadMessages(IQueryable<Messages> query, object userState)
        {
            
this.LoadMessages(query, MergeOption.KeepCurrentValues, userState);
        }
        
#endregion
        
        
protected override EntityContainer CreateEntityContainer()
        {
            
return new RdChat_DomainContextEntityContainer();
        }
        
        
internal sealed class RdChat_DomainContextEntityContainer : EntityContainer
        {
            
            
public RdChat_DomainContextEntityContainer()
            {
                
this.CreateEntityList<Messages>(EntityListOperations.All);
            }
        }
    }
}

  打开Silverlight项目的 MainPage.xaml ,我们放入一些控件,做简单的界面。

  包括一个DataGrid ,TextBox和Button。在按钮中添加Click方法。 

<UserControl xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"  x:Class="RiaServices_1.MainPage"
    xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
    Width
="640" Height="450">
    
<Grid x:Name="LayoutRoot" Background="White">
        
<StackPanel Orientation="Vertical"  Height="420">
            
<data:DataGrid x:Name="dataGrid" >data:DataGrid>
            
<StackPanel Orientation="Horizontal" Height="25">
                
<TextBox x:Name="txtMsg" Text=" " Width="580"  >TextBox>
                
<Button x:Name="btnSend" Content="发送消息"  Click="btnSend_Click"  >Button>
            StackPanel>
        StackPanel>
    Grid>
UserControl>

  在MainPage.xaml后台对应的cs文件 MainPage.xaml.cs 中,写入初始方法和处理按钮点击的方法。

  初始时,将所有已有的数据绑定到DataGrid中。

  点击Click方法后,将添加一条新数据到数据库,并刷新外观使新数据显示出来。

 

0
相关文章