locked
Update an Image just in time

    Question

  • Hello, i have this code:

    Concurrency::task<void> Login::LoadCaptchaImage( String^ imageUrl )
    {
         namespace Xaml = Windows::UI::Xaml;
         using namespace Concurrency;
    
         task_completion_event<void> tce;
         auto source = ref new Xaml::Media::Imaging::BitmapImage( ref new Windows::Foundation::Uri( imageUrl ) );
         
         //event does not get fired with the following line commented out
         //CaptchaImage->Source = source;
    
         source->ImageOpened += ref new Xaml::RoutedEventHandler( [tce]( Object^ sender, Xaml::RoutedEventArgs^ e )
         {
              tce.set( );
         } );
    
         return task<void>( tce ).then( 
              [this, source]( )
         {
              // the CaptchaImage should now be updated since the source is loaded
              CaptchaImage->Source = source; // Crash here regardless of the task continuation context
         }, task_continuation_context::use_current( ) );
    }

    1.) Why does this crash on the last task?

    EDIT: Ok this was my mistake, it didnt crash there but the .then afterwards which didnt use the correct task_continuation_context. however the last problem is still existing.

    2.) Why does this not even come to the last task when i have 

    //CaptchaImage->Source = source;

    not commented out? Why does BitmapImage not fire its Loaded event if it is not attached to an Image?

    Update:

    I cannot post links right now as my account has not been verified (my surface runs on it but nvm xD). see the official doc regarding BitmapImage.ImageOpened:

    "The BitmapImage.ImageOpened fires at a time that is potentially before you've assigned your BitmapImage to be the source of an Image or ImageBrush."

    Which is obviously not true as my example above shows...
    • Edited by kr4uzi Saturday, January 25, 2014 4:43 PM update
    Saturday, January 25, 2014 4:07 PM

Answers