private async void BtnShareText_Click(object sender, RoutedEventArgs e) { try { WeiboClient client = await WeiboClient.CreateAsync(); var shareResult = await client.ShareTextAsync(txtShareText.Text); if (shareResult.IsSuccess) { await new MessageDialog("分享成功").ShowAsync(); } else { await new MessageDialog("分享失败,错误码" + shareResult.ErrorCode).ShowAsync(); } } catch (ArgumentException ex) { // 空字符串或长度超过 140。 await new MessageDialog(ex.Message).ShowAsync(); } catch (AuthorizeException ex) { if (ex.Result.ResponseStatus == WebAuthenticationStatus.UserCancel) { await new MessageDialog("你取消了授权").ShowAsync(); } else if (ex.Result.ResponseStatus == WebAuthenticationStatus.ErrorHttp) { await new MessageDialog("网络故障").ShowAsync(); } } catch (HttpException ex) { await new MessageDialog("网络故障:" + ex.ErrorStatus.ToString()).ShowAsync(); } }
private static async Task <bool> WeiboShare(string url, string text) { CacheProvider cache = new CacheProvider(StorageType.IsolatedStorage); if (GlobalValue.CurrentWeiboUserInfo == null) { GlobalValue.CurrentWeiboUserInfo = cache.GetItem <UserInfo>(CacheKey.WeiboUser) ?? new UserInfo(); } byte[] image = await new HttpClient().GetByteArrayAsync(url); bool shareResult = false; try { WeiboClient client = new WeiboClient(GlobalValue.CurrentWeiboUserInfo); await client.LoginAsync(); WeiboResult result; if (!string.IsNullOrEmpty(url)) { result = await client.ShareImageAsync(image, text); } else { result = await client.ShareTextAsync(text); } GlobalValue.CurrentWeiboUserInfo = client.UserInfo; cache.UpdateItem(CacheKey.WeiboUser, GlobalValue.CurrentWeiboUserInfo); shareResult = true; } catch { shareResult = false; } return(shareResult); }