MyLoading.xaml.vb 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. Ex = Ex.RootException()
  69. LabText.Text = If(Ex.IsBadNetwork(), "网络环境不佳,请稍后再试,或使用 VPN 改善网络环境", Ex.Message)
  70. End If
  71. Else
  72. LabText.Text = TextError
  73. End If
  74. Else
  75. If ShowProgress AndAlso State.IsLoader Then
  76. LabText.Text = Text & " - " & Math.Floor(CType(State, Object).Progress * 100) & "%"
  77. Else
  78. LabText.Text = Text
  79. End If
  80. End If
  81. End Sub)
  82. End Sub
  83. #End Region
  84. #Region "状态改变"
  85. '状态枚举
  86. Public Enum MyLoadingState
  87. Unloaded = -1
  88. Run = 0
  89. [Stop] = 1
  90. [Error] = 2
  91. End Enum
  92. '用于外部改变的公开状态
  93. Private WithEvents _State As ILoadingTrigger
  94. Public Property State As ILoadingTrigger
  95. Get
  96. InitState()
  97. Return _State
  98. End Get
  99. Set(value As ILoadingTrigger)
  100. _State = value
  101. RefreshState()
  102. End Set
  103. End Property
  104. Private Sub InitState() Handles Me.Loaded
  105. If _State Is Nothing Then
  106. _State = New MyLoadingStateSimulator
  107. If AutoRun Then _State.LoadingState = MyLoadingState.Run
  108. End If
  109. End Sub
  110. Private Sub RefreshState() Handles _State.LoadingStateChanged, Me.Loaded, Me.Unloaded
  111. If _State.LoadingState = MyLoadingState.Run AndAlso Not IsLoaded Then InnerState = MyLoadingState.Stop
  112. InnerState = _State.LoadingState
  113. OuterState = _State.LoadingState
  114. AniLoop()
  115. End Sub
  116. '用于引发外部事件的状态
  117. Private Property _OuterState As MyLoadingState = MyLoadingState.Unloaded
  118. Private Property OuterState As MyLoadingState
  119. Get
  120. Return _OuterState
  121. End Get
  122. Set(value As MyLoadingState)
  123. If _OuterState = value Then Return
  124. Dim OldValue = _OuterState
  125. _OuterState = value
  126. '引发事件
  127. RaiseEvent StateChanged(Me, value, OldValue)
  128. If (OldValue = MyLoadingState.Error) <> (value = MyLoadingState.Error) Then RaiseEvent IsErrorChanged(Me, value = MyLoadingState.Error)
  129. End Set
  130. End Property
  131. '用于引发内部动画事件的状态
  132. Private Property _InnerState As MyLoadingState = MyLoadingState.Unloaded
  133. Private Property InnerState As MyLoadingState
  134. Get
  135. Return _InnerState
  136. End Get
  137. Set(value As MyLoadingState)
  138. If _InnerState = value Then Return
  139. Dim OldValue = _InnerState
  140. _InnerState = value
  141. '引发事件
  142. AniLoop()
  143. If (OldValue = MyLoadingState.Error) <> (value = MyLoadingState.Error) Then ErrorAnimation(Me, value = MyLoadingState.Error)
  144. End Set
  145. End Property
  146. #End Region
  147. #Region "动画"
  148. ''' <summary>
  149. ''' 是否需要动画。
  150. ''' </summary>
  151. Public Property HasAnimation As Boolean = True
  152. ''' <summary>
  153. ''' 主动画循环是否正在运行中。
  154. ''' </summary>
  155. Private IsLooping As Boolean = False
  156. Private Sub AniLoop()
  157. '这坨循环代码也是老屎坑了,救救.jpg
  158. If Not HasAnimation OrElse IsLooping OrElse Not InnerState = MyLoadingState.Run OrElse AniSpeed > 10 OrElse Not IsLoaded Then Return
  159. IsLooping = True
  160. ErrorAnimationWaiting = True
  161. AniStart({
  162. AaRotateTransform(PathPickaxe, -20 - CType(PathPickaxe.RenderTransform, RotateTransform).Angle, 350, 250, New AniEaseInBack(AniEasePower.Weak)),
  163. AaRotateTransform(PathPickaxe, 50, 900,, New AniEaseOutFluent, True),
  164. AaRotateTransform(PathPickaxe, 25, 900,, New AniEaseOutElastic(AniEasePower.Weak)),
  165. AaCode(
  166. Sub()
  167. PathLeft.Opacity = 1
  168. PathLeft.Margin = New Thickness(7, 41, 0, 0)
  169. PathRight.Opacity = 1
  170. PathRight.Margin = New Thickness(14, 41, 0, 0)
  171. ErrorAnimationWaiting = False
  172. End Sub),
  173. AaOpacity(PathLeft, -1, 100, 50),
  174. AaX(PathLeft, -5, 180,, New AniEaseOutFluent),
  175. AaY(PathLeft, -6, 180,, New AniEaseOutFluent),
  176. AaOpacity(PathRight, -1, 100, 50),
  177. AaX(PathRight, 5, 180,, New AniEaseOutFluent),
  178. AaY(PathRight, -6, 180,, New AniEaseOutFluent),
  179. AaCode(
  180. Sub()
  181. IsLooping = False
  182. AniLoop()
  183. End Sub,, True)
  184. }, "MyLoader Loop " & Uuid & "/" & GetUuid())
  185. End Sub
  186. ''' <summary>
  187. ''' 镐子是否还没挥下去,要求错误动画等待。
  188. ''' </summary>
  189. Private ErrorAnimationWaiting As Boolean = False
  190. Private Sub ErrorAnimation(sender As Object, isError As Boolean)
  191. If isError Then
  192. '非错误变为错误
  193. Dim Wait As Integer = If(ErrorAnimationWaiting, 400, 0)
  194. AniStart({
  195. AaColor(PanBack, ForegroundProperty, "ColorBrushRedLight", 300),
  196. AaOpacity(PathError, 1 - PathError.Opacity, 100, 300 + Wait),
  197. AaScaleTransform(PathError, 1 - CType(PathError.RenderTransform, ScaleTransform).ScaleX, 400, 300 + Wait, New AniEaseOutBack)
  198. }, "MyLoader Error " & Uuid)
  199. Else
  200. '错误变为非错误
  201. AniStart({
  202. AaOpacity(PathError, -PathError.Opacity, 100),
  203. AaScaleTransform(PathError, 0.5 - CType(PathError.RenderTransform, ScaleTransform).ScaleX, 200),
  204. AaColor(PanBack, ForegroundProperty, "ColorBrush3", 300)
  205. }, "MyLoader Error " & Uuid)
  206. End If
  207. End Sub
  208. #End Region
  209. #Region "点击事件"
  210. Private Sub Button_MouseUp(sender As Object, e As MouseButtonEventArgs) Handles Me.MouseLeftButtonUp
  211. RaiseEvent Click(sender, e)
  212. End Sub
  213. Private IsMouseDown As Boolean = False
  214. Private Sub Button_MouseDown(sender As Object, e As MouseButtonEventArgs) Handles Me.MouseLeftButtonDown
  215. '鼠标点击判定(务必放在点击事件之后,以使得 Button_MouseUp 先于 Button_MouseLeave 执行)
  216. IsMouseDown = True
  217. End Sub
  218. Private Sub Button_MouseLeave(sender As Object, e As Object) Handles Me.MouseLeave, Me.MouseLeftButtonUp
  219. IsMouseDown = False
  220. End Sub
  221. #End Region
  222. End Class
  223. Public Interface ILoadingTrigger
  224. ReadOnly Property IsLoader As Boolean
  225. Property LoadingState As MyLoadingState
  226. Event LoadingStateChanged(NewState As MyLoadingState, OldState As MyLoadingState)
  227. Event ProgressChanged(NewProgress As Double, OldProgress As Double)
  228. End Interface
  229. Public Class MyLoadingStateSimulator
  230. Implements ILoadingTrigger
  231. Private Property _LoadingState As MyLoadingState = MyLoadingState.Unloaded
  232. Public Property LoadingState As MyLoadingState Implements ILoadingTrigger.LoadingState
  233. Get
  234. Return _LoadingState
  235. End Get
  236. Set(value As MyLoadingState)
  237. If _LoadingState = value Then Return
  238. Dim OldState = _LoadingState
  239. _LoadingState = value
  240. RaiseEvent LoadingStateChanged(value, OldState)
  241. End Set
  242. End Property
  243. Public ReadOnly Property IsLoader As Boolean = False Implements ILoadingTrigger.IsLoader
  244. Public Event LoadingStateChanged(NewState As MyLoadingState, OldState As MyLoadingState) Implements ILoadingTrigger.LoadingStateChanged
  245. Public Event ProgressChanged(NewProgress As Double, OldProgress As Double) Implements ILoadingTrigger.ProgressChanged
  246. End Class