Skip to content

Commit

Permalink
优化UI加载效率
Browse files Browse the repository at this point in the history
  • Loading branch information
aiqinxuancai committed Mar 1, 2024
1 parent b93c423 commit 7effdad
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 37 deletions.
29 changes: 10 additions & 19 deletions Aria2Fast/Service/Model/MikanModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ public class MikanAnimeRss : BaseNotificationModel

private List<MikanAnimeRssItem> _items { get; set; }

private bool _isUpdateToday;
private int _episode = 0;
private string _updateTime;

public List<MikanAnimeRssItem> Items
{
Expand All @@ -131,10 +134,15 @@ public List<MikanAnimeRssItem> Items
{
_episode = e;
}

_isUpdateToday = TimeHelper.IsUpdateToday(item.Updated, +8); ;
_updateTime = TimeHelper.FormatTimeAgo(item.Updated, +8);
}
}
}



public string ShowEpisode
{
get
Expand All @@ -147,13 +155,9 @@ public string ShowEpisode
}

return text;
// text += $" 总{Items.Count}";
}
}


private int _episode = 0;

//TODO 当前集数
public int Episode
{
Expand All @@ -169,28 +173,15 @@ public string UpdateTime
{
get
{
var item = Items.FirstOrDefault();
if (item != null)
{

string result = TimeHelper.FormatTimeAgo(item.Updated, +8);

return result;
}
return string.Empty;
return _updateTime;
}
}

public bool IsUpdateToday
{
get
{
var item = Items.FirstOrDefault();
if (item != null)
{
return TimeHelper.IsUpdateToday(item.Updated, +8); ;
}
return false;
return _isUpdateToday;
}
}

Expand Down
18 changes: 8 additions & 10 deletions Aria2Fast/Utils/ImageCacheUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,8 @@ await Task.Run(() => {
bitmap.UriSource = new Uri(file);
bitmap.EndInit();

// 这一步很重要,它标记了图片的缓存行为,当图片加载完毕后,它会被保留在内存中
bitmap.Freeze();

//var img = new BitmapImage(new Uri(file));
//img.CacheOption = BitmapCacheOption.OnLoad;
cache.Set(cacheKey, bitmap, new CacheItemPolicy());
Debug.WriteLine($"[缓存]{cacheKey}");

Expand All @@ -57,13 +54,14 @@ await Task.Run(() => {
/// <returns></returns>
public static BitmapImage GetImageWithLocalCache(Uri uri)
{
//MemoryCache cache = MemoryCache.Default;
MemoryCache cache = MemoryCache.Default;
var fileName = Path.GetFileName(uri.LocalPath);
//if (cache.Contains(fileName))
//{
// var bmp = (BitmapImage)cache.Get(fileName);
// return bmp;
//}
if (cache.Contains(fileName))
{
var bmp = (BitmapImage)cache.Get(fileName);
//bmp.Freeze();
return bmp;
}

var dirPath = Path.Combine(Directory.GetCurrentDirectory(), "ImageCached");
if (!Directory.Exists(dirPath))
Expand Down Expand Up @@ -94,7 +92,7 @@ public static BitmapImage GetImageWithLocalCache(Uri uri)
}
img.CacheOption = BitmapCacheOption.OnLoad;
img.EndInit();

img.Freeze();
return img;
}

Expand Down
14 changes: 10 additions & 4 deletions Aria2Fast/View/AnimeListView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,15 @@
Margin="0,45,0,0"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch"
ItemsSource="{Binding Master.AnimeDays}" />



ItemsSource="{Binding Master.AnimeDays, IsAsync=True}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel IsVirtualizing="True" VirtualizationMode="Recycling" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>



</Grid>
</Page>
8 changes: 4 additions & 4 deletions Aria2Fast/View/MikanAnimeDayControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
Height="200"
Opacity="0"
RenderOptions.BitmapScalingMode="HighQuality"
Source="{Binding ImageCache}"
Source="{Binding ImageCache, IsAsync=True}"
Stretch="UniformToFill">
<Image.Clip>
<RectangleGeometry
Expand All @@ -69,11 +69,11 @@
</Image.Clip>
<Image.Style>
<Style TargetType="Image">
<!-- Image initially transparent -->

<Setter Property="Opacity" Value="0" />
<Style.Triggers>
<EventTrigger RoutedEvent="Image.Loaded">
<!-- Begin fade in when image is loaded -->

<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Expand Down Expand Up @@ -128,7 +128,7 @@
HorizontalAlignment="Center"
VerticalAlignment="Center"
Foreground="White"
Text="{Binding NewEpisode}" />
Text="{Binding NewEpisode, IsAsync=True}" />
<TextBlock
Margin="0,0,5,0"
HorizontalAlignment="Center"
Expand Down

0 comments on commit 7effdad

Please sign in to comment.