IAsyncAction DetailPage::OnNavigatedTo()

in PhotoEditor/DetailPage.cpp [295:346]


    IAsyncAction DetailPage::OnNavigatedTo(NavigationEventArgs e)
    {
        Item(e.Parameter().as<PhotoEditor::Photo>());

        if (auto item = Item())
        {
            Photo* impleType = get_self<Photo>(item);
            m_imageSource = co_await impleType->GetImageSourceAsync();

            // Because DetailPage can be destroyed during the life of the event handler, 
            // it is good practice to create a weak_ref to *this, capture it in the lambda, and resolve it before use.
            m_propertyChangedToken = item.PropertyChanged(auto_revoke, [weak{ get_weak() }](auto&&, auto&& args)
            {
                if (auto strong = weak.get())
                {
                    strong->UpdateEffectBrush(args.PropertyName());
                }
            });

            targetImage().Source(m_imageSource);

            ConnectedAnimation imageAnimation = ConnectedAnimationService::GetForCurrentView().GetAnimation(L"itemAnimation");
            if (imageAnimation)
            {
                imageAnimation.Completed([weak{ get_weak() }](auto&&, auto&&)
                {
                    if (auto strong = weak.get())
                    {
                        strong->MainImage().Source(strong->m_imageSource);
                        strong->MainImage().Visibility(Visibility::Visible);
                        strong->targetImage().Source(nullptr);

                        strong->InitializeEffects();
                        strong->UpdateMainImageBrush();
                        strong->InitializeEffectPreviews();
                        strong->UpdateButtonImageBrush();
                    }
                });

                imageAnimation.TryStart(targetImage());
            }
            if (m_imageSource.PixelHeight() == 0 && m_imageSource.PixelWidth() == 0)
            {
                // There is no editable image loaded. Disable zoom and edit
                // to prevent other errors.
                EditButton().IsEnabled(false);
                ZoomButton().IsEnabled(false);
            }
        }

        BackButton().IsEnabled(Frame().CanGoBack());
    }