locked
IAsyncOperation question in metro style application

    Question

  • Hi,

    in windows8 develop preview version:

     1 void SimpleSample::MainPage::ImageButton_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
     2 {
     3 
     4     FileOpenPicker^ open = ref new FileOpenPicker();
     5     // Select display mode
     6     open->ViewMode = PickerViewMode::Thumbnail;
     7     // Start location
     8     open->SuggestedStartLocation = PickerLocationId::Desktop;
     9 
    10     open->FileTypeFilter->Append(".jpg");
    11     open->FileTypeFilter->Append(".jpeg");
    12     open->FileTypeFilter->Append(".png");
    13     open->FileTypeFilter->Append(".bmp");
    14 
    15     // Instantiate a operation and Start.
    16     PickSingleFileOperation^ pickOperation = open->PickSingleFileAsync();
    17     pickOperation->Completed = ref new AsyncOperationCompletedHandler<StorageFile^>([this] (IAsyncOperation<StorageFile^>^ operation)
    18     {
    19         StorageFile^ file = operation->GetResults();
    20 
    21         if(file)
    22         {
    23             StreamRetrievalOperation^ StreamOperation = file->OpenAsync(FileAccessMode::Read);
    24             StreamOperation->Completed = ref new AsyncOperationCompletedHandler<IRandomAccessStream^>([this](IAsyncOperation<IRandomAccessStream
    25                 ^>^ operation)
    26             {
    27                 IRandomAccessStream^ bitmapStream = operation->GetResults();
    28                 BitmapImage^ bitmapImage = ref new BitmapImage();
    29                 bitmapImage->SetSource(bitmapStream);
    30                 ShowImage->Source = bitmapImage;
    31             });
    32             StreamOperation->Start();
    33         }
    34     });
    35     pickOperation->Start();
    36 
    37 
    38 
    39 }

    "StreamOperation->Start();" and "pickOperation->Start();" could not used in windows8 consumer preview.

    So, how I should do in windows8 consumer preview?

    Thursday, March 8, 2012 10:34 AM

Answers

  • Hello,

    You can follow the updated sample code of "Access and save files using a sample"

    http://code.msdn.microsoft.com/windowsapps/File-picker-sample-9f294cba/sourcecode?fileId=43704&pathId=1462329729

    void MainPage::Scenario1Button_Click(Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e) 
    { 
        // Clear previous returned file name, if it exists, between iterations of this scenario 
        Scenario1Reset(); 
     
        if (EnsureUnsnapped()) 
        { 
            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"); 
     
            task<StorageFile^>(openPicker->PickSingleFileAsync()).then([this](StorageFile^ file) 
            { 
                if (file) 
                { 
                    this->Scenario1FileName->Text = file->Name; 
                } 
                else 
                { 
                    this->Scenario1FileName->Text = "File was not returned"; 
                } 
            }); 
        } 
    } 

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by yangdebin Saturday, March 10, 2012 7:29 AM
    Friday, March 9, 2012 8:49 AM

All replies

  • All you need to do is remove the Op->Start(); calls, and then add "AsyncStatus" as your last parameter in the completed handlers.  So for pickOperation->Completed for example it would be " pickOperation->Completed = ref new AsyncOperationCompletedHandler<StorageFile^>([this] (IAsyncOperation<StorageFile^>^ operation, AsyncStatus) "
    • Proposed as answer by Cristianohh Thursday, March 8, 2012 3:45 PM
    Thursday, March 8, 2012 3:39 PM
  • Hello,

    You can follow the updated sample code of "Access and save files using a sample"

    http://code.msdn.microsoft.com/windowsapps/File-picker-sample-9f294cba/sourcecode?fileId=43704&pathId=1462329729

    void MainPage::Scenario1Button_Click(Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e) 
    { 
        // Clear previous returned file name, if it exists, between iterations of this scenario 
        Scenario1Reset(); 
     
        if (EnsureUnsnapped()) 
        { 
            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"); 
     
            task<StorageFile^>(openPicker->PickSingleFileAsync()).then([this](StorageFile^ file) 
            { 
                if (file) 
                { 
                    this->Scenario1FileName->Text = file->Name; 
                } 
                else 
                { 
                    this->Scenario1FileName->Text = "File was not returned"; 
                } 
            }); 
        } 
    } 

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by yangdebin Saturday, March 10, 2012 7:29 AM
    Friday, March 9, 2012 8:49 AM
  • 23             StreamRetrievalOperation^ StreamOperation = file->OpenAsync(FileAccessMode::Read);
    24             StreamOperation->Completed = ref new AsyncOperationCompletedHandler<IRandomAccessStream^>([this](IAsyncOperation<IRandomAccessStream
    25                 ^>^ operation)
    26             {
    27                 IRandomAccessStream^ bitmapStream = operation->GetResults();
    28                 BitmapImage^ bitmapImage = ref new BitmapImage();
    29                 bitmapImage->SetSource(bitmapStream);
    30                 ShowImage->Source = bitmapImage;
    31             });

    in windows 8 consumer preview:

    "BitmapImage^ bitmapImage = ref new BitmapImage();", run into this row, program error.

    Saturday, March 10, 2012 7:37 AM
  • in windows8 develop preview version:
        m_ras = ref new InMemoryRandomAccessStream();
        auto enc1 = BitmapEncoder::CreateAsync(BitmapEncoder::BmpEncoderId, m_ras);

        try{
            enc1->Completed = ref new AsyncOperationCompletedHandler<BitmapEncoder^>
       ([this](IAsyncOperation<BitmapEncoder^>^ operation, AsyncStatus aaa)
            {
                if(aaa == AsyncStatus::Completed)
       {
                    BitmapEncoder^ enc = operation->GetResults();
                    Platform::Array<unsigned char>^ p = ref new Platform::Array<unsigned char>(m_pinpix, m_nPixelSize);
                    enc->SetPixelData(BitmapPixelFormat::Rgba8, BitmapAlphaMode::Straight,
                    m_width, m_height, 96, 96, p);
        auto flushOp = enc->FlushAsync();
        flushOp->Completed = ref new AsyncActionCompletedHandler([this](IAsyncAction ^action, AsyncStatus aaa)
        {
         if(aaa == AsyncStatus::Completed)
         {
          try{
           BitmapImage^ bImg = ref new BitmapImage();
           bImg->SetSource(m_ras);
           this->myImage->Source = bImg;
          }catch(...){
          }
         }
        });
                }

            });
        }catch(...){
        }

    how I should do in windows 8 consumer preview?

    Saturday, March 10, 2012 9:39 AM