dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.28k stars 1.76k forks source link

IAnimationManager not yet set on first page that is navigated to #18433

Open sjorsmiltenburg opened 1 year ago

sjorsmiltenburg commented 1 year ago

Description

Because I found memory leaks in lottie animations and wanted a loading indicator that looks similar on all platforms I have created a ContentView with an animation that is initialized in the constructor of the ContentView. This ContentView renders succesfully when used on a page that is loaded later in the app lifecycle, like the Bug10 view in the example. But when I use on the first page that is navigated to, i get: System.ArgumentException: 'Unable to find IAnimationManager for 'zz_MauiBugs.ViewModels.MyAnimatedContentView'. (Parameter 'animatable')'

I have tried to override different "OnXXXX" methods of the ContentView to initialize theanimation, but as ContentView misses an OnLoaded and OnAppearing, these all seem to come to soon and the AnimationManager does not seem to have been set yet.

If I call the animation.Commit method on the view from the Mainview.OnAppearing it works.

probably related to https://github.com/dotnet/maui/issues/3353

Steps to Reproduce

open repo click bug10 button to see it "work" uncomment line 29 to see the error

Link to public reproduction project repository

https://github.com/sjorsmiltenburg/maui_bugs

Version with bug

8.0.0-rc.2.9373

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Windows

Affected platform versions

No response

Did you find any workaround?

If I call the animation.Commit method on the view from the Mainview.OnAppearing it works.

Relevant log output

No response

XamlTest commented 11 months ago

Verified this on Visual Studio Enterprise 17.9.0 Preview 2(8.0.3). Repro project: maui_bugs.zip

After uncomment line 29 in MainView.xaml file, application run failed with exception on Windows 11, Android 14.0-API34, iOS 17.0 and MacCatalyst platform.

Keflon commented 9 months ago

I have a Controls library where I've run into this problem, and I'm thinking about a work-around by doing some variation on this idea:

if (currentViewAsView.IsLoaded)
{
    // Control is loaded, we can animate!
    currentViewAsView.AbortAnimation("CurrentAnimation");

    var animation = new Animation(v => currentViewAsView.TranslationX = v, this.Width, 0);
    animation.Commit(this, "CurrentAnimation", 16, this.Duration, Easing.SinOut, (v, c) => currentViewAsView.TranslationX = 0, () => false);
}
else
{
    // Control is not loaded, so snap to the end-value.
    currentViewAsView.TranslationX = 0;
}

Basically if I can't start an animation then I simply set any 'animated' property to its final value.
Would it be sensible for MAUI to do this for us, rather than throw an exception?
Any suggestions about how this may be generalised are welcome, as one of my as one of my controls has rather a lot of animated properties!