终于开始正式学习silverlight,虽然有点晚,但总算开始了,今天看了一下sdk,主要是想看下silverlight中如何动态调用数据,对于数据库的访问,sdk中的官方示例多用的是Ado.Net数据服务,方便到是方便,就是感觉Ado.Net数据服务返回的xml太啰嗦了,一点也不精简,数据通讯量太大(N多无用的标签导致客户端与服务端之间传输数据增加了不少),于是想到能否直接用wcf返回json格式,
何况sdk中提到silverlight中System.Json已经完成了对json的解析
经实验,用WebClient貌似就能解析一切,包括加载json文本,纯文本,以及wcf,不过要注意的是:xap与被调用的txt或wcf必须在同一个域下,否则将调用失败,具体看下面的代码
Page.Xaml完整内容如下:
1
<UserControl x:Class="WcfTest.Page"
2
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4
>
5
<Grid x:Name="LayoutRoot" Background="White" ShowGridLines="True">
6
<Grid.RowDefinitions>
7
<RowDefinition Height="*"></RowDefinition>
8
9
10
</Grid.RowDefinitions>
11
12
<TextBlock x:Name="txtJson" Grid.Row="0" Grid.Column="0"></TextBlock>
13
<TextBlock x:Name="txtBlank" Grid.Row="1" Grid.Column="0" TextWrapping="Wrap"></TextBlock>
14
<TextBlock x:Name="txtWcf" Grid.Row="2" Grid.Column="0" TextWrapping="Wrap"></TextBlock>
15
16
</Grid>
17
</UserControl>
Page.Xaml.cs完整内容如下:
Code
using System;
using System.Json;
using System.Net;
using System.Windows.Controls;
using System.IO;
namespace WcfTest
{
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
LoadJsonTxt();
LoadBlankTxt();
LoadWcfTxt();
}
/// <summary>
/// 加载Json格式的文本文件
/// </summary>
void LoadJsonTxt()
Uri serviceUri = new Uri("http://localhost:7055/data/json.txt");//很奇怪,只能用绝对路径
WebClient downloader = new WebClient();
downloader.OpenReadCompleted += new OpenReadCompletedEventHandler(LoadJsonTxtCompleted);
downloader.OpenReadAsync(serviceUri);
/// 异常回调函数
/// <param name="sender"></param>
/// <param name="e"></param>
void LoadJsonTxtCompleted(object sender, OpenReadCompletedEventArgs e)
if (e.Error == null)
{
JsonArray _data = (JsonArray)JsonArray.Load(e.Result);
string _Result = "";
for (int i = 0; i < _data.Count; i++)
{
_Result += (",name=" + _data[i]["name"].ToString());
}
if (_Result.StartsWith(","))
_Result = _Result.Substring(1);
this.txtJson.Text = _Result;
}
else
txtJson.Text = "出错:" + e.Error.Message.ToString();
/// 加载普通格式的文本文件
void LoadBlankTxt()
Uri serviceUri = new Uri("http://localhost:7055/data/txt.txt");
downloader.OpenReadCompleted += new OpenReadCompletedEventHandler(LoadBlankTxtCompleted);
void LoadBlankTxtCompleted(object sender, OpenReadCompletedEventArgs e)
StreamReader _Reader = new StreamReader(e.Result);
this.txtBlank.Text = _Reader.ReadToEnd();
txtBlank.Text = "出错:" + e.Error.Message.ToString();
/// 加载wcf返回的文本
void LoadWcfTxt()
Uri serviceUri = new Uri("http://localhost:7055/Demo.svc/GetData?callBack=123");
downloader.OpenReadCompleted += new OpenReadCompletedEventHandler(LoadWcfCompleted);
void LoadWcfCompleted(object sender, OpenReadCompletedEventArgs e)
this.txtWcf.Text = _Reader.ReadToEnd();
txtWcf.Text = "出错:" + e.Error.Message.ToString();
}
}
wcf的代码也在这里贴出来,就是一"启用了ajax的wcf服务",没啥特别的,下面的代码仅参考(从NorthWind数据库中,读取了表Categories的部分信息)
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using JIMMY.TOOLS;
namespace WcfTest.Web
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Demo
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "GetData?callback={callback}")]
public Stream GetData(string callback)
DataTable _Table = new DataTable();
string _Result = "";
using (SqlConnection conn = Database.GetConn(ConfigurationManager.ConnectionStrings["ConnStr"].ToString()))
try
string sql = "Select CategoryId,CategoryName,Description from Categories";
SqlDataReader sdr = Database.ExecuteReader(conn, CommandType.Text, sql);
_Table = Database.ConvertDataReaderToDataTable(sdr);
catch { }
finally { conn.Close(); }
if (_Table.Rows.Count <= 0)
_Result = "var _" + callback + "={Head:[]}";
_Result = "var _" + callback + "=" + Utils.CreateJsonParameters(_Table);
return GetStream(_Result);
private Stream GetStream(string str)
MemoryStream ms = new MemoryStream();
StreamWriter sw = new StreamWriter(ms);
sw.AutoFlush = true;
sw.Write(str);
ms.Position = 0;
WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";
return ms;
}
最后附上:txt.txt以及json.txt二个测试文本的内容
json.txt内容:
[{'name':"jimmy.yang"},{'name':'yezhouyun'},{'name':123456}]
txt.txt内容:
要做到这两点只需要更改一下所示的高两部分即可。第一个高两部分表示我们将NorthwindEntities作为我们的数据源。NorthwindEntities是我们刚才创建的EDMX的类名,Employees等表都已经作为其属性被Mapping成对象。第二个高亮部分是控制EntitySet(其实就是对应的表一级)的访问权限。例如你仍然可以通过下边的代码仅仅暴露Employees对象的只读权限:config.SetEntityAccessRule(“Employees”,EntitySetRights.AllRead.这样,服务只会暴露Employees集合并且只接受读取,而不能有更新操作。
运行效果图如下: