From 0cc48973542a5d1927d69ddcdc13e2489625a453 Mon Sep 17 00:00:00 2001 From: DismissedLight <1686188646@qq.com> Date: Mon, 27 May 2024 23:24:03 +0800 Subject: [PATCH] deferload experiment --- .../Control/Behavior/DeferLoadBehavior.cs | 44 +++++++++++++++++++ .../Control/Behavior/DeferLoadCollection.cs | 25 +++++++++++ .../Snap.Hutao/View/Helper/LoadDeferral.cs | 15 +++++++ src/Snap.Hutao/Snap.Hutao/View/MainView.xaml | 2 +- .../View/Page/AnnouncementPage.xaml | 9 +++- 5 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 src/Snap.Hutao/Snap.Hutao/Control/Behavior/DeferLoadBehavior.cs create mode 100644 src/Snap.Hutao/Snap.Hutao/Control/Behavior/DeferLoadCollection.cs create mode 100644 src/Snap.Hutao/Snap.Hutao/View/Helper/LoadDeferral.cs diff --git a/src/Snap.Hutao/Snap.Hutao/Control/Behavior/DeferLoadBehavior.cs b/src/Snap.Hutao/Snap.Hutao/Control/Behavior/DeferLoadBehavior.cs new file mode 100644 index 00000000..27b52fbc --- /dev/null +++ b/src/Snap.Hutao/Snap.Hutao/Control/Behavior/DeferLoadBehavior.cs @@ -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 +{ + 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? 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); + }); + } + } +} \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/Control/Behavior/DeferLoadCollection.cs b/src/Snap.Hutao/Snap.Hutao/Control/Behavior/DeferLoadCollection.cs new file mode 100644 index 00000000..c7ae5eb8 --- /dev/null +++ b/src/Snap.Hutao/Snap.Hutao/Control/Behavior/DeferLoadCollection.cs @@ -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 +{ + public static DeferLoadCollection Parse(string text) + { + DeferLoadCollection collection = []; + foreach (StringSegment segment in new StringTokenizer(text, [','])) + { + if (segment.HasValue) + { + collection.Add(segment.Value); + } + } + + return collection; + } +} \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/View/Helper/LoadDeferral.cs b/src/Snap.Hutao/Snap.Hutao/View/Helper/LoadDeferral.cs new file mode 100644 index 00000000..e4934471 --- /dev/null +++ b/src/Snap.Hutao/Snap.Hutao/View/Helper/LoadDeferral.cs @@ -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); } +} \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/View/MainView.xaml b/src/Snap.Hutao/Snap.Hutao/View/MainView.xaml index 04885ca6..0edacf43 100644 --- a/src/Snap.Hutao/Snap.Hutao/View/MainView.xaml +++ b/src/Snap.Hutao/Snap.Hutao/View/MainView.xaml @@ -31,7 +31,7 @@ - + diff --git a/src/Snap.Hutao/Snap.Hutao/View/Page/AnnouncementPage.xaml b/src/Snap.Hutao/Snap.Hutao/View/Page/AnnouncementPage.xaml index 4c4abb6b..4304f903 100644 --- a/src/Snap.Hutao/Snap.Hutao/View/Page/AnnouncementPage.xaml +++ b/src/Snap.Hutao/Snap.Hutao/View/Page/AnnouncementPage.xaml @@ -23,6 +23,7 @@ mc:Ignorable="d"> + @@ -207,7 +208,7 @@ - + - +