日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

vs2010 学习Silverlight学习笔记(15):数据与通信之JSON

發布時間:2024/4/17 javascript 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 vs2010 学习Silverlight学习笔记(15):数据与通信之JSON 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

概述:

?????? 有段時間沒學習了,該繼續了。不一定寫完就發,有可能最后一起發。這個JSON我以前不太了解,只知道是web傳輸中的一種格式。今天初步了解一下這篇關于JSON是干什么的,寫完后再看看W3C中JSON的資料。

內容:

?????? 創建兩個實體,一個Post類,一個Blog類。與以往不同的是在sl中也有著相同兩個類。

Post.cs

public class Post { ??? public int Id { get; set; } ? ??? public string Title { get; set; } ? ??? public string Author { get; set; } }

Blog.cs

public class Blog { ??? public List<Post> Posts { get; set; } }

新建HttpHandler用于向sl傳輸JSON數據。

public class BlogHandler : IHttpHandler { ??????? public void ProcessRequest(HttpContext context) ??????? { ??????????? context.Response.ContentType = "text/plain"; ??????????? //context.Response.Write("Hello World"); ??????????? List<Post> posts = new List<Post>() ????????????{ ??????????????? new Post{Id=1,Title="1a",Author="li"}, ??????????????? new Post{Id=2,Title="2a",Author="li"}, ??????????????? new Post{Id=3,Title="3a",Author="li"}, ??????????????? new Post{Id=4,Title="4a",Author="li"}, ??????????????? new Post{Id=5,Title="5a",Author="li"} ??????????? }; ??????????? Blog blog = new Blog(); ??????????? blog.Posts = posts; ??????????? context.Response.Write(JavaScriptConvert.SerializeObject(blog)); ??????? }??? ??????? public bool IsReusable ??? { ??????? get ??????? { ??????????? return false; ??????? } ??? } } 傳出數據的這一句:context.Response.Write(JavaScriptConvert.SerializeObject(blog));

所用的的JavaScriptConvert.SerializeObject來自JSON,在sl中添加引用中會有System.Json;但這WEB中并沒有,這里用的是李老師源碼中的Dll文件引用。有興趣的可以對Json.net做了解。

?????? 在此時可以查看http://localhost:8081/BlogHandler.ashx,但此時瀏覽器顯示的是錯誤:文件頂層無效。在下面的留言中也發現有個這錯誤的,但并沒有原因和方法。先不管他,做剩下的部分。

布局界面:

<UserControlx:Class="SLDemo16JOSN.MainPage"

???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"

???d:DesignHeight="300" d:DesignWidth="400"Loaded="UserControl_Loaded">

?

???<Grid Background="#46461F">

???????<Grid.RowDefinitions>

??????????? <RowDefinitionHeight="40"></RowDefinition>

???????????<RowDefinition Height="*"></RowDefinition>

???????</Grid.RowDefinitions>

???????<Grid.ColumnDefinitions>

???????????<ColumnDefinition></ColumnDefinition>

???????</Grid.ColumnDefinitions>

???????<Border Grid.Row="0" Grid.Column="0"CornerRadius="15"

???????????Width="240" Height="36"Background="Orange"

???????????Margin="20 0 0 0" HorizontalAlignment="Left">

???????????<TextBlock Text="最新隨筆" Foreground="White"

?????????????????? HorizontalAlignment="Left"VerticalAlignment="Center"

?????????????????? Margin="20 0 00"></TextBlock>

???????</Border>

???????<ListBox x:Name="Posts" Grid.Row="1"Margin="40 10 10 10">

???????????<ListBox.ItemTemplate>

??????????????? <DataTemplate>

???? ???????????????<StackPanelOrientation="Horizontal">

??????????????????????? <TextBlockText="{Binding Id}" Height="40"Foreground="Red"></TextBlock>

??????????????????????? <TextBlockText="{Binding Title}" Height="40"></TextBlock>

?????????????????????? ?<TextBlock Text="{BindingAuthor}" Height="40"Foreground="Orange"></TextBlock>

??????????????????? </StackPanel>

??????????????? </DataTemplate>

???????????</ListBox.ItemTemplate>

???????</ListBox>

???</Grid>

</UserControl>

?

MainPage.cs界面:需要添加引用System.Net;,System.ServiceModel.Web;

using System;

using System.Collections.Generic;

using System.Linq;

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.Net;

using System.IO;

using System.Runtime.Serialization.Json;

using System.Threading;

?

?

namespace SLDemo16JOSN

{

???public partial class MainPage : UserControl

??? {

???????public MainPage()

???????{

???????????InitializeComponent();

???????}

?

???????private void UserControl_Loaded(object sender, RoutedEventArgs e)

???????{

???????????Uri endpoint = newUri("http://localhost:8081/BlogHandler.ashx");

?

???????????WebRequest request = WebRequest.Create(endpoint);

???????????request.Method = "POST";

???????????request.ContentType = "application/x-www-form-urlencoded";

???????????request.BeginGetResponse(new AsyncCallback(ResponseReady), request);

???????}

?

???????void ResponseReady(IAsyncResult asyncResult)

???????{

???????????WebRequest request = asyncResult.AsyncState as WebRequest;

???????????WebResponse response = request.EndGetResponse(asyncResult);

?

???????????using (Stream responseStream = response.GetResponseStream())

???????????{

??????????????? DataContractJsonSerializerjsonSerializer =

??????????????????? newDataContractJsonSerializer(typeof(Blog));

?

??????????????? Blog blog =jsonSerializer.ReadObject(responseStream) as Blog;

?

??????????????? //Posts.ItemsSource = blog.Posts;

??????????????? //new Thread(() => {Posts.Dispatcher.BeginInvoke(() => Posts.ItemsSource = blog.Posts);}).Start();

??????????????? Posts.Dispatcher.BeginInvoke(()=> Posts.ItemsSource = blog.Posts);

???????????}

???????}

??? }

}

可能是因為Handler在瀏覽器中出錯的原因吧,在sl頁面數據賦值的時候用原來的:

//Posts.ItemsSource = blog.Posts;就會出現數據為空的情況。

具體原因我不了解,不過有人給出了如下兩種解決方法:

new Thread(() => {Posts.Dispatcher.BeginInvoke(() => Posts.ItemsSource = blog.Posts);}).Start();

?????????????? 或 Posts.Dispatcher.BeginInvoke(() => Posts.ItemsSource =blog.Posts);

?

總結:

?????? 這個JSON以前沒接觸過,以后也不一定接觸多少。我認為學習這個主要不是記住這篇中每一句代碼應該怎么寫,而是以后遇到時我只知道用JSON需要怎么序列化和怎么反序列化格式,具體的代碼可以網上查一查。已經學了幾篇的數據通信部分了,其實好多東西差不多,服務器端不是Handler,就是Service,數據先要封裝序列化,然后再反序列化顯示。但我覺得就憑我這并不聰明的腦袋,記住所有的使用方法是有困難的,但記住他們的區別和關鍵特征還是有可能的。

轉載于:https://www.cnblogs.com/yaoge/archive/2010/09/09/1822604.html

總結

以上是生活随笔為你收集整理的vs2010 学习Silverlight学习笔记(15):数据与通信之JSON的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。