0

几天来,我一直试图弄清楚如何从我的 Silverlight 应用程序访问数据。

我想使用已经编写好的数据类和业务类。

根据另一篇文章的一些建议,我创建了一个 Silverlight 业务应用程序。访问数据的代码位于我的 Web 应用程序的域服务类中。这是从 Silveright 应用程序调用的。

我想我很接近,但我的语法不太正确。

这是我的域服务类中的代码

Public Function GetGridData() As IEnumerable(Of Submissions)

Dim dtResults As DataTable

Dim _ConnectionString As String
= _

"Password=xxxx;Persist Security Info=True;User ID=xxxx;Initial Catalog=APCD;Data Source=xxxx"

mdsResults = s.GetSubmissions(3, 0, _ConnectionString,"2011", "0", False)

dtResults = mdsResults.Tables(0)

Dim MySubmissions = New List(Of Submissions
)()

For Each row As DataRow In
dtResults.Rows

Dim MySubmission = New Submissions() With
{ _
.SubmissionControlId = Convert.ToString(row("SubmissionControlId"
)), _
.OrgId = Convert.ToString(row("Org Id"
)), _
.DateProcessed = Convert.ToString(row("DateProcessed")) _

}


MySubmissions.Add(MySubmission)

Next

Return MySubmissions

End Function

The code in the silverlght page is



 Dim x As New Web.CustomerDomainContext

    grdSubmissions.DataContext = x.GetGridData()

它全部编译并运行,但网格为空。我通过逐步了解存储过程确实包含数据。

4

1 回答 1

0

There are several points I'd like to comment on.

Firstly, it is better not to return from a function like above. This is because you lose the benefit of being able to edit and update data. You should return IQueryable instead. Generate some code using EF to have a feel for it.

Secondly, instead of setting DataContext, the ItemsSource on the DataGrid instead.

Dim gridData = x.GetGridData()
grdSubmissions.ItemsSource= gridData

Thirdly, you should also step through Silverlight code and make sure gridData contains values. From first observation, it doesn't look correct because Silverlight is asynchronous. The code to fetch data from grid should look like:

Dim domainContext = new MyDomainContext()
AddHandler domainContext.Completed, 
  Sub (op)
    grdSubmissions.ItemsSource = domainContext.Submissions
  End Sub 
domainContext.Load(domainContext.GetSubmissionQuery())

You are best working through this example: http://blogs.msdn.com/b/kylemc/archive/2011/04/29/mvvm-pattern-for-ria-services.aspx as the original Ria Services code is the bare minimum, and needs more convenience methods.

Update 1 - DomainService

namespace SampleDataClass.Web.Services
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;

    public class Customer
    {
        [Key]
        public int CustomerId { get; set; }

        public string Name { get; set; }

        public string Address { get; set; }
    }

    // TODO: Create methods containing your application logic.
    [EnableClientAccess()]
    public class DomainService1 : DomainService
    {

        [Query]
        public IQueryable<Customer> GetCustomers()
        {
            return (new Customer[]
            {
                new Customer() { CustomerId=1, Name="Luigina Davide", Address="Via Giulio Petroni, 82 24050-Palosco BG" },
            }).AsQueryable<Customer>();
        }
    }

}
于 2012-08-28T00:25:01.360 回答