MyLoading.xaml.vb 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. Imports PCL.MyLoading
  2. Public Class MyLoading
  3. Public Event IsErrorChanged(sender As Object, isError As Boolean)
  4. Public Event StateChanged(sender As Object, newState As MyLoadingState, oldState As MyLoadingState)
  5. Public Event Click(sender As Object, e As MouseButtonEventArgs)
  6. Public Property AutoRun As Boolean = True
  7. Private Uuid As Integer = GetUuid()
  8. #Region "颜色"
  9. Public Property Foreground As SolidColorBrush
  10. Get
  11. Return GetValue(ForegroundProperty)
  12. End Get
  13. Set(value As SolidColorBrush)
  14. SetValue(ForegroundProperty, value)
  15. End Set
  16. End Property
  17. Public Shared ReadOnly ForegroundProperty As DependencyProperty = DependencyProperty.Register("Foreground", GetType(SolidColorBrush), GetType(MyLoading))
  18. Public Sub New()
  19. InitializeComponent()
  20. SetResourceReference(ForegroundProperty, "ColorBrush3")
  21. End Sub
  22. #End Region
  23. #Region "文本"
  24. Private Property _ShowProgress As Boolean = False
  25. Public Property ShowProgress As Boolean
  26. Get
  27. Return _ShowProgress
  28. End Get
  29. Set(value As Boolean)
  30. If _ShowProgress = value Then Return
  31. _ShowProgress = value
  32. RefreshText()
  33. End Set
  34. End Property
  35. Private _Text As String = "加载中"
  36. Public Property Text As String
  37. Get
  38. Return _Text
  39. End Get
  40. Set(value As String)
  41. _Text = value
  42. RefreshText()
  43. End Set
  44. End Property
  45. Private _TextError As String = "加载失败"
  46. Public Property TextError As String
  47. Get
  48. Return _TextError
  49. End Get
  50. Set(value As String)
  51. _TextError = value
  52. RefreshText()
  53. End Set
  54. End Property
  55. ''' <summary>
  56. ''' 是否在使用 Loader 时使用 Loader 的错误输出来替换默认的错误文本显示。
  57. ''' </summary>
  58. Public Property TextErrorInherit As Boolean = True
  59. Private Sub RefreshText() Handles Me.IsErrorChanged, Me.Loaded, _State.ProgressChanged
  60. RunInUi(
  61. Sub()
  62. If InnerState = MyLoadingState.Error Then
  63. If TextErrorInherit AndAlso State.IsLoader Then
  64. Dim Ex As Exception = CType(State, Object).Error
  65. If Ex Is Nothing Then
  66. LabText.Text = "未知错误"
  67. Else
  68. LabText.Text = Ex.GetDisplay(False)
  69. End If
  70. Else
  71. LabText.Text = TextError
  72. End If
  73. Else
  74. If ShowProgress AndAlso State.IsLoader Then
  75. LabText.Text = Text & " - " & Math.Floor(CType(State, Object).Progress * 100) & "%"
  76. Else
  77. LabText.Text = Text
  78. End If
  79. End If
  80. End Sub)
  81. End Sub
  82. #End Region
  83. #Region "状态改变"
  84. '状态枚举
  85. Public Enum MyLoadingState
  86. Unloaded = -1
  87. Run = 0
  88. [Stop] = 1
  89. [Error] = 2
  90. End Enum
  91. '用于外部改变的公开状态
  92. Private WithEvents _State As ILoadingTrigger
  93. Public Property State As ILoadingTrigger
  94. Get
  95. InitState()
  96. Return _State
  97. End Get
  98. Set(value As ILoadingTrigger)
  99. _State = value
  100. RefreshState()
  101. End Set
  102. End Property
  103. Private Sub InitState() Handles Me.Loaded
  104. If _State Is Nothing Then
  105. _State = New MyLoadingStateSimulator
  106. If AutoRun Then _State.LoadingState = MyLoadingState.Run
  107. End If
  108. End Sub
  109. Private Sub RefreshState() Handles _State.LoadingStateChanged, Me.Loaded, Me.Unloaded
  110. If _State.LoadingState = MyLoadingState.Run AndAlso Not IsLoaded Then InnerState = MyLoadingState.Stop
  111. InnerState = _State.LoadingState
  112. OuterState = _State.LoadingState
  113. AniLoop()
  114. End Sub
  115. '用于引发外部事件的状态
  116. Private Property _OuterState As MyLoadingState = MyLoadingState.Unloaded
  117. Private Property OuterState As MyLoadingState
  118. Get
  119. Return _OuterState
  120. End Get
  121. Set(value As MyLoadingState)
  122. If _OuterState = value Then Return
  123. Dim OldValue = _OuterState
  124. _OuterState = value
  125. '引发事件
  126. RaiseEvent StateChanged(Me, value, OldValue)
  127. If (OldValue = MyLoadingState.Error) <> (value = MyLoadingState.Error) Then RaiseEvent IsErrorChanged(Me, value = MyLoadingState.Error)
  128. End Set
  129. End Property
  130. '用于引发内部动画事件的状态
  131. Private Property _InnerState As MyLoadingState = MyLoadingState.Unloaded
  132. Private Property InnerState As MyLoadingState
  133. Get
  134. Return _InnerState
  135. End Get
  136. Set(value As MyLoadingState)
  137. If _InnerState = value Then Return
  138. Dim OldValue = _InnerState
  139. _InnerState = value
  140. '引发事件
  141. AniLoop()
  142. If (OldValue = MyLoadingState.Error) <> (value = MyLoadingState.Error) Then ErrorAnimation(Me, value = MyLoadingState.Error)
  143. End Set
  144. End Property
  145. #End Region
  146. #Region "动画"
  147. ''' <summary>
  148. ''' 是否需要动画。
  149. ''' </summary>
  150. Public Property HasAnimation As Boolean = True
  151. ''' <summary>
  152. ''' 主动画循环是否正在运行中。
  153. ''' </summary>
  154. Private IsLooping As Boolean = False
  155. Private Sub AniLoop()
  156. '这坨循环代码也是老屎坑了,救救.jpg
  157. If Not HasAnimation OrElse IsLooping OrElse Not InnerState = MyLoadingState.Run OrElse AniSpeed > 10 OrElse Not IsLoaded Then Return
  158. IsLooping = True
  159. ErrorAnimationWaiting = True
  160. AniStart({
  161. AaRotateTransform(PathPickaxe, -20 - CType(PathPickaxe.RenderTransform, RotateTransform).Angle, 350, 250, New AniEaseInBack(AniEasePower.Weak)),
  162. AaRotateTransform(PathPickaxe, 50, 900,, New AniEaseOutFluent, True),
  163. AaRotateTransform(PathPickaxe, 25, 900,, New AniEaseOutElastic(AniEasePower.Weak)),
  164. AaCode(
  165. Sub()
  166. PathLeft.Opacity = 1
  167. PathLeft.Margin = New Thickness(7, 41, 0, 0)
  168. PathRight.Opacity = 1
  169. PathRight.Margin = New Thickness(14, 41, 0, 0)
  170. ErrorAnimationWaiting = False
  171. End Sub),
  172. AaOpacity(PathLeft, -1, 100, 50),
  173. AaX(PathLeft, -5, 180,, New AniEaseOutFluent),
  174. AaY(PathLeft, -6, 180,, New AniEaseOutFluent),
  175. AaOpacity(PathRight, -1, 100, 50),
  176. AaX(PathRight, 5, 180,, New AniEaseOutFluent),
  177. AaY(PathRight, -6, 180,, New AniEaseOutFluent),
  178. AaCode(
  179. Sub()
  180. IsLooping = False
  181. AniLoop()
  182. End Sub,, True)
  183. }, "MyLoader Loop " & Uuid & "/" & GetUuid())
  184. End Sub
  185. ''' <summary>
  186. ''' 镐子是否还没挥下去,要求错误动画等待。
  187. ''' </summary>
  188. Private ErrorAnimationWaiting As Boolean = False
  189. Private Sub ErrorAnimation(sender As Object, isError As Boolean)
  190. If isError Then
  191. '非错误变为错误
  192. Dim Wait As Integer = If(ErrorAnimationWaiting, 400, 0)
  193. AniStart({
  194. AaColor(PanBack, ForegroundProperty, "ColorBrushRedLight", 300),
  195. AaOpacity(PathError, 1 - PathError.Opacity, 100, 300 + Wait),
  196. AaScaleTransform(PathError, 1 - CType(PathError.RenderTransform, ScaleTransform).ScaleX, 400, 300 + Wait, New AniEaseOutBack)
  197. }, "MyLoader Error " & Uuid)
  198. Else
  199. '错误变为非错误
  200. AniStart({
  201. AaOpacity(PathError, -PathError.Opacity, 100),
  202. AaScaleTransform(PathError, 0.5 - CType(PathError.RenderTransform, ScaleTransform).ScaleX, 200),
  203. AaColor(PanBack, ForegroundProperty, "ColorBrush3", 300)
  204. }, "MyLoader Error " & Uuid)
  205. End If
  206. End Sub
  207. #End Region
  208. #Region "点击事件"
  209. Private Sub Button_MouseUp(sender As Object, e As MouseButtonEventArgs) Handles Me.MouseLeftButtonUp
  210. RaiseEvent Click(sender, e)
  211. End Sub
  212. Private IsMouseDown As Boolean = False
  213. Private Sub Button_MouseDown(sender As Object, e As MouseButtonEventArgs) Handles Me.MouseLeftButtonDown
  214. '鼠标点击判定(务必放在点击事件之后,以使得 Button_MouseUp 先于 Button_MouseLeave 执行)
  215. IsMouseDown = True
  216. End Sub
  217. Private Sub Button_MouseLeave(sender As Object, e As Object) Handles Me.MouseLeave, Me.MouseLeftButtonUp
  218. IsMouseDown = False
  219. End Sub
  220. #End Region
  221. End Class
  222. Public Interface ILoadingTrigger
  223. ReadOnly Property IsLoader As Boolean
  224. Property LoadingState As MyLoadingState
  225. Event LoadingStateChanged(NewState As MyLoadingState, OldState As MyLoadingState)
  226. Event ProgressChanged(NewProgress As Double, OldProgress As Double)
  227. End Interface
  228. Public Class MyLoadingStateSimulator
  229. Implements ILoadingTrigger
  230. Private Property _LoadingState As MyLoadingState = MyLoadingState.Unloaded
  231. Public Property LoadingState As MyLoadingState Implements ILoadingTrigger.LoadingState
  232. Get
  233. Return _LoadingState
  234. End Get
  235. Set(value As MyLoadingState)
  236. If _LoadingState = value Then Return
  237. Dim OldState = _LoadingState
  238. _LoadingState = value
  239. RaiseEvent LoadingStateChanged(value, OldState)
  240. End Set
  241. End Property
  242. Public ReadOnly Property IsLoader As Boolean = False Implements ILoadingTrigger.IsLoader
  243. Public Event LoadingStateChanged(NewState As MyLoadingState, OldState As MyLoadingState) Implements ILoadingTrigger.LoadingStateChanged
  244. Public Event ProgressChanged(NewProgress As Double, OldProgress As Double) Implements ILoadingTrigger.ProgressChanged
  245. End Class