deferload experiment

This commit is contained in:
DismissedLight
2024-05-27 23:24:03 +08:00
parent db4b0d3dcb
commit 0cc4897354
5 changed files with 92 additions and 3 deletions

View File

@@ -0,0 +1,44 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.
using CommunityToolkit.WinUI.Behaviors;
using Microsoft.UI.Xaml;
using System.Runtime.InteropServices;
namespace Snap.Hutao.Control.Behavior;
[DependencyProperty("ElementNames", typeof(DeferLoadCollection))]
internal sealed partial class DeferLoadBehavior : BehaviorBase<FrameworkElement>
{
protected override bool Initialize()
{
if (ElementNames.IsNullOrEmpty())
{
return true;
}
ThreadPool.UnsafeQueueUserWorkItem(LoadElements, this);
return true;
}
private static void LoadElements(object? state)
{
if (state is not DeferLoadBehavior behavior)
{
return;
}
List<string>? elementNames = null;
behavior.AssociatedObject.DispatcherQueue.Invoke(() => elementNames = [.. behavior.ElementNames]);
foreach (string name in CollectionsMarshal.AsSpan(elementNames))
{
Thread.Sleep(1000);
behavior.AssociatedObject.DispatcherQueue.TryEnqueue(() =>
{
behavior.AssociatedObject.FindName(name);
});
}
}
}

View File

@@ -0,0 +1,25 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.
using Microsoft.Extensions.Primitives;
using Windows.Foundation.Metadata;
namespace Snap.Hutao.Control.Behavior;
[CreateFromString(MethodName = "Snap.Hutao.Control.Behavior.DeferLoadCollection.Parse")]
internal sealed class DeferLoadCollection : List<string>
{
public static DeferLoadCollection Parse(string text)
{
DeferLoadCollection collection = [];
foreach (StringSegment segment in new StringTokenizer(text, [',']))
{
if (segment.HasValue)
{
collection.Add(segment.Value);
}
}
return collection;
}
}

View File

@@ -0,0 +1,15 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.
using CommunityToolkit.Mvvm.ComponentModel;
using Microsoft.Extensions.Primitives;
using Windows.Foundation.Metadata;
namespace Snap.Hutao.View.Helper;
internal sealed class LoadDeferral : ObservableObject
{
private bool canLoad;
public bool CanLoad { get => canLoad; set => SetProperty(ref canLoad, value); }
}

View File

@@ -31,7 +31,7 @@
<shvcs:BackdropTypeToOpacityConverter x:Key="BackdropTypeToOpacityConverter"/>
</UserControl.Resources>
<Grid Transitions="{ThemeResource EntranceThemeTransitions}">
<Grid>
<Border Background="{ThemeResource SolidBackgroundFillColorBaseBrush}" Opacity="{Binding AppOptions.BackdropType, Converter={StaticResource BackdropTypeToOpacityConverter}, Mode=OneWay}">
<Border.OpacityTransition>
<ScalarTransition Duration="0:0:1"/>

View File

@@ -23,6 +23,7 @@
mc:Ignorable="d">
<mxi:Interaction.Behaviors>
<shcb:InvokeCommandOnLoadedBehavior Command="{Binding OpenUICommand}"/>
<shcb:DeferLoadBehavior ElementNames="TopPanel,GameAnnouncementPivot"/>
</mxi:Interaction.Behaviors>
<shc:ScopedPage.Resources>
<shc:BindingProxy x:Key="BindingProxy" DataContext="{Binding}"/>
@@ -207,7 +208,7 @@
<Grid>
<ScrollViewer Padding="0" Style="{StaticResource DefaultScrollViewerStyle}">
<StackPanel>
<StackPanel>
<StackPanel Name="TopPanel">
<TextBlock
Margin="16,16,16,0"
Style="{StaticResource TitleTextBlockStyle}"
@@ -240,7 +241,11 @@
</ScrollViewer>
</StackPanel>
<Pivot Style="{StaticResource DefaultPivotStyle}">
<Pivot
x:Name="GameAnnouncementPivot"
x:Load="False"
Style="{StaticResource DefaultPivotStyle}"
Transitions="{ThemeResource EntranceThemeTransitions}">
<PivotItem
Content="{Binding Announcement.List[0]}"
ContentTemplate="{StaticResource AnnouncementPivotItemContentTemplate}"