Async coding in C++ means never having to say you are sorry

Maybe.  That is a paraphrase quote from an old movie called "Love Story", that is seriously boring, unless you are with someone you either love or hope to, then it is still boring if you actually watched it.  Like Dr. Zhivago or whatever, I didn't know it was in Russia till a few years ago.

Oh well…In this Async document, I just show you how to generate a filepicker for documents and pictures.  I stole the idea from Sridhar Poduri who has written a very good book, go to his blog at https://sridharpoduri.com/2012/04/08/async-programming-using-c-cx/ but I added the picture selection code.  You get to add the image display control.  Why, I am too lazy.  Actually, I have been busy today.  Sridhar's blog is much better than mine so make sure to spend some time on it.  I also added the ViewManagement namespace, it will be used in future articles about using View management to write difficult to explain code that is easily maintained.

The nice thing about Async coding is that you can do stuff while your program does other stuff.  It is generally fairly easy to understand in C#, but C++, as usual has a couple of ways to do something.  One way is the hard way and the other way is the harder way, such is life.  I like it.

CPP File, then the XAML file follows, just copy and paste if you want to give it a try.  Just FYI, it may not work first time, it will.  Also, the namespace ChangeThis will need to match your needs.  Will discuss the code in the next article on Friday.

//
// MainPage.xaml.cpp
// Implementation of the MainPage class.
//

#include "pch.h"
#include "MainPage.xaml.h"
#include <collection.h>

using namespace ChangeThis;

using namespace Platform;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Controls::Primitives;
using namespace Windows::UI::Xaml::Data;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Xaml::Media;
using namespace Windows::UI::Xaml::Navigation;
//
using namespace Platform;
using namespace Platform::Collections;
//using namespace Windows::Foundation;
//using namespace Windows::Foundation::Collections;
using namespace Windows::Storage;
using namespace Windows::Storage::Streams;
using namespace Windows::Storage::Pickers;
using namespace concurrency;
//
using namespace Windows::UI::ViewManagement;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238

MainPage::MainPage()
{
    InitializeComponent();
}

/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached.  The Parameter
/// property is typically used to configure the page.</param>
void MainPage::OnNavigatedTo(NavigationEventArgs^ e)
{
    (void) e;    // Unused parameter
}

void ChangeThis::MainPage::Hello(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
    label->Text="hello world";
    auto picker = ref new FileOpenPicker();
    picker->SuggestedStartLocation = Windows::Storage::Pickers::PickerLocationId::DocumentsLibrary;
    picker->FileTypeFilter->Append(".docx");
    picker->FileTypeFilter->Append(".pdf");

    task<StorageFile^>(picker->PickSingleFileAsync()).then([this](StorageFile^ file)
    {
        if (file)
        {
            this->label->Text = file->DisplayName;
        }
    });
}

void ChangeThis::MainPage::TappedOn(Platform::Object^ sender, Windows::UI::Xaml::Input::TappedRoutedEventArgs^ e)
{
        FileOpenPicker^ openPicker = ref new FileOpenPicker();
        openPicker->ViewMode = PickerViewMode::Thumbnail;
        openPicker->SuggestedStartLocation = PickerLocationId::PicturesLibrary;
        openPicker->FileTypeFilter->Append(".jpg");
        openPicker->FileTypeFilter->Append(".jpeg");
        openPicker->FileTypeFilter->Append(".png");
        create_task(openPicker->PickSingleFileAsync()).then([this](StorageFile^ file)
        {
            if (file)
            {
                OutputTextBlock->Text = "Picked photo: " + file->Name;
            }
            else
            {
                OutputTextBlock->Text = "Operation cancelled.";
            }
        });
}

/*********************************************************************/

 

XAML CODE:

<Page
    x:Class="ChangeThis.MainPage"
    xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:ChangeThis"
    xmlns:d="https://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="https://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <Button Content="Button"
                HorizontalAlignment="Left" Margin="194,84,0,0" VerticalAlignment="Top"
                Click="Hello"/>
        < TextBlock x:Name="label"
                   HorizontalAlignment="Left" Margin="343,84,0,0" VerticalAlignment="Top"
                   TextWrapping="Wrap"
                   Text="TextBlock"/>
        <Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top"
                Height="43" Margin="194,145,0,0"
                Width="80" Tapped="TappedOn"/>
        < TextBlock x:Name="OutputTextBlock" HorizontalAlignment="Left"  VerticalAlignment="Top"
                   Margin="343,145,0,0"
                   Height="43" Width="381"
                   TextWrapping="Wrap" Text="TextBlock"
                  />

    </Grid>
< /Page>