Beispiel #1
0
        private void HandlePostBtnClicked(object sender, RoutedEventArgs e)
        {
            try
            {
                var availableLen = GetAvailableStatusText();
                if (availableLen < 0)
                {
                    ShowMessage("Exceeds the status length limit. Try the post-as-pic option.", false);

                    IsPostAsPic = true;
                    return;
                }

                ClearMessage();

                if (!Validate())
                    return;

                if (null != Posting)
                {
                    var args = new PostingEventArgs();
                    args.PostingText = Text;

                    Posting(this, args);

                    if (!args.IsContinue)
                        return;
                }

                ShowMessage("Posting...");

                if (!this.IsPicAttached)
                {
                    var updateStatusInfo = new UpdateStatusInfo() { Status = Text, Latitude = 31.22f, Longitude = 121.48f };

                    AMicroblog.PostStatusAsync(
                        delegate(AsyncCallResult<StatusInfo> result)
                        {
                            if (result.Success)
                            {
                                var statusInfo = result.Data;
                                AddStatusToView(statusInfo);

                                ShowMessage("Message posted successfully at {0}", statusInfo.CreatedAt);
                            }
                            else
                                ShowMessage("Failed to post message due to: {0}", false, result.Exception.Message);

                            if (null != Posted)
                                Posted(this, null);

                        }, updateStatusInfo);
                }
                else
                {
                    try
                    {
                        var updStatusData = new UpdateStatusWithPicInfo();
                        updStatusData.Status = Text;
                        updStatusData.Pic = this.AttachedPicLocation;

                        var postCallback = new AsyncCallback<StatusInfo>(delegate(AsyncCallResult<StatusInfo> result)
                        {
                            if (result.Success)
                            {
                                var statusInfo = result.Data;

                                AddStatusToView(statusInfo);

                                this.AttachedPicLocation = string.Empty;

                                ShowMessage("Message posted successfully at {0}", statusInfo.CreatedAt);
                            }
                            else
                            {
                                this.IsPicAttached = true;
                                ShowMessage("Failed to post message due to: {0}", false, result.Exception.Message);
                            }
                        });

                        AMicroblog.PostStatusWithPicAsync(postCallback, updStatusData);
                    }
                    catch (Exception ex)
                    {
                        ShowMessage(ex.Message, false);
                    }
                }

                this.IsPicAttached = false;
                txbStatus.Clear();
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message, false);
            }
        }
 /// <summary>
 /// The async implementation of <see cref="PostStatusWithPic"/>
 /// </summary>
 public static void PostStatusWithPicAsync(AsyncCallback<StatusInfo> callback, UpdateStatusWithPicInfo updateStatusWithPicInfo)
 {
     // Validates arguments
     ValidateArgument(updateStatusWithPicInfo, "updateStatusWithPicInfo");
     ValidateArgument(updateStatusWithPicInfo.Status, "updateStatusWithPicInfo.Status");
     ValidateArgument(updateStatusWithPicInfo.Pic, "updateStatusWithPicInfo.Pic");
     FileInfo picInfo = new FileInfo(updateStatusWithPicInfo.Pic);
     if (picInfo.Length > 5 * 1021 * 1024) // 5MB limit
     {
         throw new AMicroblogException(LocalErrorCode.ArgumentInvalid, "Pic file too large to post.");
     }
     var uri = APIUri.UpdateStatusWithPic;
     var requester = new OAuthMultiPartHttpPost(uri);
     requester.PartFields.Add(new MultiPartField() { Name = "status", Value = RFC3986Encoder.UrlEncode(updateStatusWithPicInfo.Status) });
     requester.PartFields.Add(new MultiPartField() { Name = "pic", FilePath = updateStatusWithPicInfo.Pic });
     if (updateStatusWithPicInfo.Latitude.HasValue && updateStatusWithPicInfo.Longitude.HasValue)
     {
         requester.PartFields.Add(new MultiPartField() { Name = "lat", FilePath = updateStatusWithPicInfo.Latitude.Value.ToString(InvariantCulture) });
         requester.PartFields.Add(new MultiPartField() { Name = "long", FilePath = updateStatusWithPicInfo.Longitude.Value.ToString(InvariantCulture) });
     }
     requester.RequestAsync(delegate(AsyncCallResult<string> result)
     {
         ProcessAsyncCallbackResponse(result, callback);
     });
 }