0

在Xamarin Forms中,當涉及連續和/或同時動畫,自定義或其他動畫的數量時,是否應該注意某種限制?Xamarin表單動畫重載錯誤?

這裏是我處理的一個簡單的例子:

protected void ShowAlertMessage(string message) 
    { 
     lblAlertMessage.Text = message; 

     this.AbortAnimation("AlertShow"); // crash happens with and without this... 

     new Animation { 
      { 0, 0.1, new Animation(v => AlertView.Opacity = v, 0.0, 1.0) }, 
      { 0.1, 0.6, new Animation(v => AlertView.Opacity = v, 1.0, 1.0) }, 
      { 0.6, 1.0, new Animation(v => AlertView.Opacity = v, 1.0, 0) }, 
     }.Commit(this, "AlertShow", length: 3000); 
    } 

動畫本身工作正常。 AlertView視圖淡入,持續,然後淡出。我在動畫完成後再次調用該函數,並且它工作正常。當AlertView處於正在運行的動畫中間時,我再次調用該函數,並且它仍然正常 - 它會重新啓動動畫。但是,如果我搗碎調用ShowAlertMessage()超級快速的按鈕而不會失敗,那麼iPhone模擬器會崩潰,並顯示以下堆棧跟蹤(我包括整個事情......它看起來像是一個新線程開始下蓋...):

2017-06-24 12:20:37.595 RootTuner.iOS[19386:887418] critical: Stacktrace: 

2017-06-24 12:20:37.596 RootTuner.iOS[19386:887418] critical: at <unknown> <0xffffffff> 
2017-06-24 12:20:37.596 RootTuner.iOS[19386:887418] critical: at (wrapper managed-to-native) ObjCRuntime.Messaging.void_objc_msgSend_float (intptr,intptr,single) [0x0000b] in <58a9bd8a3cbb4d0092bc0a767ff66c0f>:0 
2017-06-24 12:20:37.597 RootTuner.iOS[19386:887418] critical: at CoreAnimation.CALayer.set_Opacity (single) [0x0000b] in /Users/builder/data/lanes/4691/d2270eec/source/xamarin-macios/src/build/ios/native/CoreAnimation/CALayer.g.cs:1714 
2017-06-24 12:20:37.597 RootTuner.iOS[19386:887418] critical: at Xamarin.Forms.Platform.iOS.VisualElementTracker/<>c__DisplayClass23_0.<OnUpdateNativeControl>b__0() [0x0015a] in C:\BuildAgent\work\ca3766cfc22354a1\Xamarin.Forms.Platform.iOS\VisualElementTracker.cs:178 
2017-06-24 12:20:37.597 RootTuner.iOS[19386:887418] critical: at Xamarin.Forms.Platform.iOS.CADisplayLinkTicker.StartThread() [0x00025] in C:\BuildAgent\work\ca3766cfc22354a1\Xamarin.Forms.Platform.iOS\CADisplayLinkTicker.cs:60 
2017-06-24 12:20:37.598 RootTuner.iOS[19386:887418] critical: at System.Threading.ThreadHelper.ThreadStart_Context (object) [0x00014] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.10.0.36/src/mono/mcs/class/referencesource/mscorlib/system/threading/thread.cs:68 
2017-06-24 12:20:37.598 RootTuner.iOS[19386:887418] critical: at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext,System.Threading.ContextCallback,object,bool) [0x00071] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.10.0.36/src/mono/mcs/class/referencesource/mscorlib/system/threading/executioncontext.cs:957 
2017-06-24 12:20:37.598 RootTuner.iOS[19386:887418] critical: at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext,System.Threading.ContextCallback,object,bool) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.10.0.36/src/mono/mcs/class/referencesource/mscorlib/system/threading/executioncontext.cs:904 
2017-06-24 12:20:37.598 RootTuner.iOS[19386:887418] critical: at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext,System.Threading.ContextCallback,object) [0x0002b] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.10.0.36/src/mono/mcs/class/referencesource/mscorlib/system/threading/executioncontext.cs:893 
2017-06-24 12:20:37.599 RootTuner.iOS[19386:887418] critical: at System.Threading.ThreadHelper.ThreadStart() [0x00008] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.10.0.36/src/mono/mcs/class/referencesource/mscorlib/system/threading/thread.cs:105 
2017-06-24 12:20:37.599 RootTuner.iOS[19386:887418] critical: at (wrapper runtime-invoke) object.runtime_invoke_void__this__ (object,intptr,intptr,intptr) [0x0004f] in <2f4074c3120b4d80802e10af84b67d41>:0 
2017-06-24 12:20:37.599 RootTuner.iOS[19386:887418] critical: 
Native stacktrace: 

2017-06-24 12:20:37.606 RootTuner.iOS[19386:887418] critical: 0 RootTuner.iOS      0x000000010fdfc501 mono_handle_native_crash + 257 
2017-06-24 12:20:37.607 RootTuner.iOS[19386:887418] critical: 1 RootTuner.iOS      0x000000010fe095d0 mono_sigsegv_signal_handler + 288 
2017-06-24 12:20:37.607 RootTuner.iOS[19386:887418] critical: 2 libsystem_platform.dylib   0x000000011bbfbbba _sigtramp + 26 
2017-06-24 12:20:37.607 RootTuner.iOS[19386:887418] critical: 3 ???         0x0000000000000000 0x0 + 0 
2017-06-24 12:20:37.607 RootTuner.iOS[19386:887418] critical: 4 QuartzCore       0x000000011dc0a9ab _ZL18CAAnimation_setterP11CAAnimationj12_CAValueTypePKv + 169 
2017-06-24 12:20:37.608 RootTuner.iOS[19386:887418] critical: 5 QuartzCore       0x000000011dc03d5a -[CAAnimation setDelegate:] + 30 
2017-06-24 12:20:37.608 RootTuner.iOS[19386:887418] critical: 6 UIKit        0x0000000114057c39 -[UIViewAnimationState setAnimationAttributes:correctZeroDuration:skipDelegateAssignment:customCurve:] + 1094 
2017-06-24 12:20:37.609 RootTuner.iOS[19386:887418] critical: 7 UIKit        0x0000000114054475 -[UIViewAnimationState animationForLayer:forKey:forView:] + 1195 
2017-06-24 12:20:37.609 RootTuner.iOS[19386:887418] critical: 8 UIKit        0x0000000114055178 -[UIViewAnimationState actionForLayer:forKey:forView:] + 99 
2017-06-24 12:20:37.609 RootTuner.iOS[19386:887418] critical: 9 UIKit        0x000000011408052e +[UIView(Animation) _defaultUIViewActionForLayer:forKey:] + 111 
2017-06-24 12:20:37.609 RootTuner.iOS[19386:887418] critical: 10 UIKit        0x000000011485e8ec -[UIView(UIKitManual) actionForLayer:forKey:] + 119 
2017-06-24 12:20:37.610 RootTuner.iOS[19386:887418] critical: 11 QuartzCore       0x000000011dbda4bc -[CALayer actionForKey:] + 437 
2017-06-24 12:20:37.610 RootTuner.iOS[19386:887418] critical: 12 QuartzCore       0x000000011dbd5ade _ZL12actionForKeyP7CALayerPN2CA11TransactionEP8NSString + 76 
2017-06-24 12:20:37.610 RootTuner.iOS[19386:887418] critical: 13 QuartzCore       0x000000011dbd5a6c _ZN2CA5Layer12begin_changeEPNS_11TransactionEjRP11objc_object + 140 
2017-06-24 12:20:37.611 RootTuner.iOS[19386:887418] critical: 14 QuartzCore       0x000000011dbd5e04 _ZN2CA5Layer6setterEj12_CAValueTypePKv + 164 
2017-06-24 12:20:37.611 RootTuner.iOS[19386:887418] critical: 15 QuartzCore       0x000000011dbe1351 -[CALayer setOpacity:] + 43 
2017-06-24 12:20:37.611 RootTuner.iOS[19386:887418] critical: 16 RootTuner.iOS      0x000000010ffbe429 xamarin_dyn_objc_msgSend + 217 
2017-06-24 12:20:37.611 RootTuner.iOS[19386:887418] critical: 17 ???         0x00000001351767f4 0x0 + 5185693684 
2017-06-24 12:20:37.611 RootTuner.iOS[19386:887418] critical: 18 ???         0x00000001357292f4 0x0 + 5191668468 
2017-06-24 12:20:37.611 RootTuner.iOS[19386:887418] critical: 19 ???         0x000000013572747c 0x0 + 5191660668 
2017-06-24 12:20:37.612 RootTuner.iOS[19386:887418] critical: 20 RootTuner.iOS      0x000000010fe0c585 mono_jit_runtime_invoke + 1301 
2017-06-24 12:20:37.612 RootTuner.iOS[19386:887418] critical: 21 RootTuner.iOS      0x000000010fec6b88 do_runtime_invoke + 88 
2017-06-24 12:20:37.612 RootTuner.iOS[19386:887418] critical: 22 RootTuner.iOS      0x000000010ff0d41a start_wrapper + 538 
2017-06-24 12:20:37.612 RootTuner.iOS[19386:887418] critical: 23 RootTuner.iOS      0x000000010ff91fed inner_start_thread + 189 
2017-06-24 12:20:37.612 RootTuner.iOS[19386:887418] critical: 24 libsystem_pthread.dylib    0x000000011bc0daab _pthread_body + 180 
2017-06-24 12:20:37.612 RootTuner.iOS[19386:887418] critical: 25 libsystem_pthread.dylib    0x000000011bc0d9f7 _pthread_body + 0 
2017-06-24 12:20:37.612 RootTuner.iOS[19386:887418] critical: 26 libsystem_pthread.dylib    0x000000011bc0d1fd thread_start + 13 
2017-06-24 12:20:37.613 RootTuner.iOS[19386:887418] critical: 
================================================================= 
Got a SIGSEGV while executing native code. This usually indicates 
a fatal error in the mono runtime or one of the native libraries 
used by your application. 
================================================================= 

我在做什麼錯?它在使用簡單的.FadeTo()和.ScaleTo()方法的其他情況下執行相同的操作。動畫工作,但太多導致系統崩潰。

我使用Visual Studio for Mac(Community)7.0.1編譯爲iOS。讓我知道是否需要更多規格。我只是希望有一個動畫「最佳實踐」,我錯過了!

回答

0

我管理這與兩個保障措施。

第一個是我的動畫,並根據您的要求,當一個運行時關閉所有動畫。例如:

private bool isAnimating = false; 


     private bool isAnimating = false; 
     private object myObject = new object(); 
     private void RunAnimation() 
     { 
      if (isAnimating) return; 
      lock (myObject) 
      { 
       isAnimating = true; 
       try 
       { 
        //my animation 
       } 
       finally 
       { 
        isAnimating = false; 
       } 
      } 

     } 

第二個保障,當我用動畫是捕獲所有異常,並提供一個備用狀態(產品可見或隱藏例如)

private bool isAnimating = false; 
private object myObject = new object(); 
private void RunAnimation() 
{ 
    if (isAnimating) return; 
    lock (myObject) 
    { 
     isAnimating = true; 
     try 
     { 
      //my animation 
     } 
     catch (Exception) 
     { 
      //set opacity = 1, visible = true.... 
     } 
     finally 
     { 
      isAnimating = false; 
     } 
    } 
} 

你去那裏

+0

嗨丹尼爾 - 感謝您的迴應!我嘗試了你的建議,鎖定了所有動畫並捕捉到任何可能的例外,但不幸的是它仍然崩潰。它在處理動畫的內部工作時在iOS下崩潰,所以try/catch/finally塊不會捕獲系統錯誤。 - 關於你的建議的問題 - 如果設置了(isAnimating)條件以防止更多的動畫發生,直到當前動畫完成,那麼你如何同時處理多個動畫? –

+0

我剛剛在另一個視圖上嘗試過。同樣的處理 - 令人沮喪的是,有時它在重新開始動畫(有或沒有中止)後幾次崩潰。我最後一次嘗試,在它墜毀之前,我將動畫打了100多次。這是非常不可預測的。每次我在執行本機代碼時都得到了「SIGSEGV」,這通常表示單聲道運行時或您的應用程序使用的某個本地庫中的致命錯誤。我正在編輯我的原始帖子以包含完整的堆棧跟蹤。 –

+0

您可以嘗試使用FadeTo方法爲不透明度設置動畫效果。也許它會解決你的問題。 – Daniel