Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MyImage 控件 #4868

Merged
merged 13 commits into from
Oct 13, 2024
73 changes: 73 additions & 0 deletions Plain Craft Launcher 2/Controls/MyImage.vb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
Imports System.Threading.Tasks

Public Class MyImage
Inherits Image

'事件

Private _SourceData As String = ""
Private _UseCache As Boolean = False
Private _DownloadTask As Task

Private FileCacheExpiredTime As TimeSpan = New TimeSpan(7, 0, 0, 0) ' 一个星期的缓存有效期

Public Property UseCache As Boolean
Get
Return _UseCache
End Get
Set(value As Boolean)
_UseCache = value
End Set
End Property

''' <summary>
''' 重写Image的Source属性
''' </summary>
Public Shadows Property Source As String
Get
Return _SourceData
End Get
Set(value As String)
If String.IsNullOrEmpty(value) Then Exit Property
_SourceData = value
Try
If Not value.StartsWithF("http") Then ' 本地资源直接使用
MyBase.Source = New MyBitmap(_SourceData)
Exit Property
End If
If _DownloadTask IsNot Nothing AndAlso Not _DownloadTask.IsCompleted Then ' 之前下载任务还在,直接砍了
_DownloadTask.Dispose()
End If
Dim NeedDownload As Boolean = True '是否需要下载/本地是否有有效缓存
Dim TempFilePath As String = PathTemp & "Cache\MyImage\" & GetHash(_SourceData) & ".png"
If _UseCache And File.Exists(TempFilePath) And (DateTime.Now - File.GetCreationTime(TempFilePath)) < FileCacheExpiredTime Then NeedDownload = False ' 缓存文件存在且未过期,不需要重下
tangge233 marked this conversation as resolved.
Show resolved Hide resolved
If Not NeedDownload Then
MyBase.Source = New MyBitmap(TempFilePath)
Exit Property
End If
' 异步下载图片
Dim TaskID = 0
_DownloadTask = New Task(Sub()
NetDownload(_SourceData, TempFilePath, True)
End Sub)
TaskID = _DownloadTask.Id
_DownloadTask.Start()
_DownloadTask.ContinueWith(Sub(t)
If t.IsCompleted AndAlso t.Id = TaskID Then ' 任务没有被干掉
MyBase.Source = New MyBitmap(TempFilePath)
End If
End Sub, TaskScheduler.FromCurrentSynchronizationContext())
tangge233 marked this conversation as resolved.
Show resolved Hide resolved
Catch ex As Exception
Log(ex, "加载图片失败")
End Try
End Set
End Property
Public Shared Shadows ReadOnly SourceProperty As DependencyProperty = DependencyProperty.Register("Source", GetType(String), GetType(MyImage), New PropertyMetadata(New PropertyChangedCallback(
Sub(sender As DependencyObject, e As DependencyPropertyChangedEventArgs)
If Not IsNothing(sender) Then
If String.IsNullOrEmpty(e.NewValue.ToString()) Then Exit Sub
CType(sender, MyImage).Source = e.NewValue.ToString()
End If
End Sub)))

End Class
2 changes: 1 addition & 1 deletion Plain Craft Launcher 2/Modules/Base/MyBitmap.vb
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,4 @@ Public Class MyBitmap
End Using
End Sub

End Class
End Class
6 changes: 1 addition & 5 deletions Plain Craft Launcher 2/Plain Craft Launcher 2.vbproj
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,7 @@
<Compile Include="Controls\MyIconTextButton.xaml.vb">
<DependentUpon>MyIconTextButton.xaml</DependentUpon>
</Compile>



<Compile Include="Controls\MyImage.vb" />
<Compile Include="Modules\Minecraft\ModComp.vb" />
<Compile Include="Modules\Minecraft\ModJava.vb" />
<Compile Include="Modules\Minecraft\ModMod.vb" />
Expand Down Expand Up @@ -478,7 +476,6 @@
<DependentUpon>FormMain.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>

<Page Include="Modules\Minecraft\MyLocalModItem.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
Expand Down Expand Up @@ -913,7 +910,6 @@
<Resource Include="Images\Heads\MCBBS.png" />
</ItemGroup>
<ItemGroup>

</ItemGroup>
<ItemGroup>
<Resource Include="Images\Blocks\NeoForge.png" />
Expand Down