示例#1
0
 //把函数转换出来的Json串交给后台数据库服务
 public void Invoke(IAsyncObject obj, Func<JsonValue> method)
 {
     //传递到后台执行
     Uri uri = new Uri(Uri);
     WebClient client = new WebClient();
     client.UploadStringCompleted += (o, e) =>
     {
         obj.IsBusy = false;
         //通知数据提交过程完成
         if (e.Error != null)
         {
             obj.State = State.Error;
             obj.Error = e.Error.GetMessage();
         }
         else
         {
             //返回数据重新赋值给对象
             JsonObject resultJson =  (JsonObject)JsonValue.Parse(e.Result);
             if (resultJson.ContainsKey(obj.Name))
             {
                 (obj as IFromJson).FromJson((JsonObject)resultJson[obj.Name]);
             }
             //判定是否保存成功
             if (resultJson.ContainsKey("success")) 
             {
                 string success = resultJson["success"];
                 MessageBox.Show(success);
             }
             if (resultJson.ContainsKey("error"))
             {
                 obj.Error = resultJson["error"];
                 obj.State = State.Error;
                 obj.OnCompleted(e);
                 return;
             }
             else 
             {
                 obj.State = State.End;
             }
         }
         obj.OnCompleted(e);
     };
     JsonArray array = new JsonArray();
     JsonValue json = method();
     if (json is JsonObject)
     {
         //把执行批处理对象的名字添加进去
         json["name"] = obj.Name;
         array.Add(json);
     }
     else
     {
         array = (JsonArray) json;
     }
     obj.IsBusy = true;
     obj.State = State.Start;
     client.UploadStringAsync(uri, array.ToString());
 }
示例#2
0
        //把函数转换出来的Json串交给后台数据库服务
        public void Invoke(IAsyncObject obj, Func <JsonValue> method)
        {
            //传递到后台执行
            Uri       uri    = new Uri(Uri);
            WebClient client = new WebClient();

            client.UploadStringCompleted += (o, e) =>
            {
                obj.IsBusy = false;
                //通知数据提交过程完成
                if (e.Error != null)
                {
                    obj.State = State.Error;
                    obj.Error = e.Error.GetMessage();
                }
                else
                {
                    //返回数据重新赋值给对象
                    JsonObject resultJson = (JsonObject)JsonValue.Parse(e.Result);
                    //提示后台返回信息
                    if (resultJson.Keys.Contains("error"))
                    {
                        MessageBox.Show(resultJson["error"]);
                        return;
                    }
                    if (resultJson.ContainsKey(obj.Name))
                    {
                        (obj as IFromJson).FromJson((JsonObject)resultJson[obj.Name]);
                    }
                    obj.State = State.End;
                }
                obj.OnCompleted(e);
            };
            JsonArray array = new JsonArray();
            JsonValue json  = method();

            if (json is JsonObject)
            {
                //把执行批处理对象的名字添加进去
                json["name"] = obj.Name;
                array.Add(json);
            }
            else
            {
                array = (JsonArray)json;
            }
            obj.IsBusy = true;
            obj.State  = State.Start;
            client.UploadStringAsync(uri, array.ToString());
        }