Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Web服务使用Silverlight 5使用JSON数据_C#_Json_Web Services_Silverlight - Fatal编程技术网

C# Web服务使用Silverlight 5使用JSON数据

C# Web服务使用Silverlight 5使用JSON数据,c#,json,web-services,silverlight,C#,Json,Web Services,Silverlight,首先,我使用的是视图模型视图模型,我是Silverlight的新手 我想从Internet上的其他应用程序获取JSON数据,并将其显示到网格中 在ViewModel页面上,我有以下内容: public void SetBasicAuthHeader(WebRequest req, String userName, String userPassword) { string authInfo = userName + ":" + userPassword;

首先,我使用的是视图模型视图模型,我是Silverlight的新手

我想从Internet上的其他应用程序获取JSON数据,并将其显示到网格中

在ViewModel页面上,我有以下内容:

    public void SetBasicAuthHeader(WebRequest req, String userName, String userPassword)
    {
        string authInfo = userName + ":" + userPassword;
        authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
        req.Headers["Authorization"] = "Basic " + authInfo;
    }

    public void Request()
    {
        // create the http request
        HttpWebRequest httpWebRequest = WebRequest.CreateHttp("Myurl");
        httpWebRequest.Method = "GET";
        httpWebRequest.Accept = "application/json";
        SetBasicAuthHeader(httpWebRequest, "mylogin", "mypassword");


        // get the response asynchronously
        httpWebRequest.BeginGetResponse(OnGetResponseCompleted, httpWebRequest);
    }

    public void OnGetResponseCompleted(IAsyncResult ar)
    {
        var httpWebRequest = (HttpWebRequest)ar.AsyncState;

        // get the response
        var response = httpWebRequest.EndGetResponse(ar);

        // deserialize json
        var jsonSerializer = new DataContractJsonSerializer(typeof(Users));
        var responseObject = (ListUtilisateursRest)jsonSerializer.ReadObject(response.GetResponseStream());

        // display on the view
        Deployment.Current.Dispatcher.BeginInvoke();
    }
我还创建了一个便携库,其中包括:

[DataContract]
public class Users
{
    [DataMember(Name = "Users")]
    private List<String> UsersList = new List<String>();
}
我知道我必须在BeginInvoke中加入一些东西来将数据绑定到视图,但我不知道如何做到这一点,你能帮我吗

谢谢,举个例子

您已获得从其他服务器接收到的用户列表。好啊 在XAML中创建listbox,绑定到数据,下面是列表数据 您可以编辑listbox的生成项模板,以定义每个用户在listbox上的显示方式。 别忘了InotifyProperty已更改。