Building an Imgur Client for Windows Phone - Part 6 - Uploading UI

DZone 's Guide to

Building an Imgur Client for Windows Phone - Part 6 - Uploading UI

· Mobile Zone ·
Free Resource

If you missed the previous four parts, you can easily find them here:

In the previous article I talked about building the upload mechanism. Now, I am going to talk about building the UI workflow that is needed to create a user-friendly upload routine.

I create a new upload page (UploadPage.xaml) that allows the user to select the image from the media library, as well as take a new one if necessary. Not only that, but I am also giving the user the possibility to enter an image title and description, even if those are not mandatory.

The complete XAML looks like this:

    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>

        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel Grid.Row="0" Margin="12,17,0,28">
            <TextBlock Text="UPLOAD IMAGE" Style="{StaticResource PhoneTextNormalStyle}"/>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
                <RowDefinition Height="230"></RowDefinition>
                <RowDefinition Height="*"></RowDefinition>
            <Image Stretch="UniformToFill" x:Name="imgPreview" Margin="12,12,12,12"></Image>
            <Button x:Name="btnPick" Click="btnPick_Click_1">
                <Image Source="/Images/photocapture.png"></Image>
            <StackPanel Grid.Row="1">
                <TextBlock Style="{StaticResource PhoneTextTitle3Style}">Name:</TextBlock>
                <TextBox x:Name="txtName"></TextBox>
                <TextBlock Style="{StaticResource PhoneTextTitle3Style}">Description:</TextBlock>
                <TextBox x:Name="txtDescription" TextWrapping="Wrap" AcceptsReturn="True" 
                         TextOptions.TextHintingMode="Animated" Height="210"></TextBox>
        <Grid Visibility="Collapsed" Grid.RowSpan="2" x:Name="grdUploading">
                <SolidColorBrush Color="Black"></SolidColorBrush>
            <TextBlock Text="Uploading..." HorizontalAlignment="Center"

            <toolkit:PerformanceProgressBar IsIndeterminate="True" Margin="0,80,0,0"></toolkit:PerformanceProgressBar>

            <shell:ApplicationBarIconButton x:Name="btnUpload" Click="btnUpload_Click_1" Text="ok" IconUri="/Images/appbar.check.png"></shell:ApplicationBarIconButton>
            <shell:ApplicationBarIconButton x:Name="btnCancel" Click="btnCancel_Click_1" Text="cancel" IconUri="/Images/appbar.cancel.png"></shell:ApplicationBarIconButton>

To do a quick overview, here is what it looks like as a result:

The main working grid is split in two parts - the image preview component that has a Button control overlaid on top of it, and the meta area that is dedicated to specifying the name and the description of the image to be uploaded. 

The Image control behind the button will display the selected content. At the same time, the main grid background (for LayoutRoot) will also showcase the selected image, although in a much larger format. This is all done with the help of PhotoChooserTask:

PhotoChooserTask task = new PhotoChooserTask() { ShowCamera = true };
task.Completed += (s, d) =>
        if (d.TaskResult == TaskResult.OK)
            byte[] buffer = new byte[16 * 1024];

            using (MemoryStream stream = new MemoryStream())
                int read = 0;

                while ((read = d.ChosenPhoto.Read(buffer, 0, buffer.Length)) > 0)
                    stream.Write(buffer, 0, read);

                BitmapImage image = new BitmapImage();

                imgPreview.Source = image;

                ImageBrush brush = new ImageBrush();
                brush.ImageSource = image;
                LayoutRoot.Background = brush;
                brush.Opacity = .2;
                brush.Stretch = Stretch.UniformToFill;

                ImageContent = stream.ToArray();

ImageContent is a common byte buffer that will be used if the user wants to upload the selected image. Once the selection is done, the UI will look like this:

Here is the code snippet that activates the upload:

grdUploading.Visibility = System.Windows.Visibility.Visible;
this.ApplicationBar.IsVisible = false;

App.ServiceClient.UploadImage(ImageContent, txtName.Text, txtDescription.Text, (n, image) =>
        if (n)
            Dispatcher.BeginInvoke(() =>

                MessageBox.Show("Your image was uploaded. The link is placed in the clipboard.",
                    "Image Upload", MessageBoxButton.OK);

                if (NavigationService.CanGoBack)
            Dispatcher.BeginInvoke(() =>
                    MessageBox.Show("There was an error uploading the image. Please try again later.",
                        "Image Upload", MessageBoxButton.OK);

        Dispatcher.BeginInvoke(() =>
                    grdUploading.Visibility = System.Windows.Visibility.Collapsed;
                    this.ApplicationBar.IsVisible = true;

When the users accepts the image, an overlay blocks off the entire UI to show that the upload is in progress:

The custom action that is passed to UploadImage will be triggered with two arguments - the boolean flag that shows whether the upload succeeded or not, and the deserialized instance of ImgurAtomicImageData. If the first argument is equal to true, then I am setting the image URL to be in the clipboard, so that the user can share it if necessary. If the upload failed, the user is being informed about this. 

You can download the current source code for the project here. Don't forget to specify your API key and secret.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}