using System; using System.Collections.Generic; using System.IO; using System.Net; using System.Windows; using System.Windows.Media.Imaging; using System.Windows.Navigation; using Microsoft.Phone.Controls; using Microsoft.Phone.Tasks; using Microsoft.Xna.Framework.Media; using RestSharp; using RestSharp.Authenticators; namespace Juick { public partial class NewPostView : PhoneApplicationPage { private readonly BitmapImage _attachedPhoto = new BitmapImage(); private PhotoChooserTask _chooser; public NewPostView() { InitializeComponent(); } // When page is navigated to set data context to selected item in list protected override void OnNavigatedTo(NavigationEventArgs e) { // Get a dictionary of query string keys and values. IDictionary queryStrings = NavigationContext.QueryString; // Ensure that there is at least one key in the query string, and check // whether the "FileId" key is present. if (queryStrings.ContainsKey("FileId")) { App.AppContext.Client.Authenticator = new HttpBasicAuthenticator(App.AppContext.Account.UserName, App.AppContext.Account.Password); // Retrieve the picture from the media library using the FileID // passed to the application. MediaLibrary library = new MediaLibrary(); Picture picture = library.GetPictureFromToken(queryStrings["FileId"]); _attachedPhoto.SetSource(picture.GetImage()); image1.Source = new WriteableBitmap(_attachedPhoto); } else { string _rid, _mid; if (NavigationContext.QueryString.TryGetValue("mid", out _mid)) { PageTitle.Text = "reply"; textBox1.Text = "#" + _mid; } if (NavigationContext.QueryString.TryGetValue("rid", out _rid)) { textBox1.Text += "/" + _rid; } } } private void Publish(object sender, EventArgs e) { var request = new RestRequest("/post", Method.POST); request.AddParameter("body", textBox1.Text); if (_attachedPhoto.PixelHeight > 0) { using (var ms = new MemoryStream()) { var wb = new WriteableBitmap(_attachedPhoto); wb.SaveJpeg(ms, _attachedPhoto.PixelWidth, _attachedPhoto.PixelHeight, 0, 100); request.AddFile("attach", ms.ToArray(), "file.jpg"); } } App.AppContext.Client.ExecuteAsync(request, response => { if (response.StatusCode != HttpStatusCode.OK) { MessageBox.Show(response.StatusCode.ToString()); } }); NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative)); Dispatcher.BeginInvoke(() => NavigationService.RemoveBackEntry()); } private void AttachFile(object sender, EventArgs e) { _chooser = new PhotoChooserTask { ShowCamera = true }; _chooser.Completed += (o, result) => { if (result.TaskResult == TaskResult.OK) { _attachedPhoto.SetSource(result.ChosenPhoto); image1.Source = new BitmapImage(new Uri(result.OriginalFileName)); } }; _chooser.Show(); } } }