1
0

MyLoading.xaml.vb 11 KB

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