real time refresh background

This commit is contained in:
qhy040404
2024-02-22 22:34:12 +08:00
parent 25cf9f5356
commit 04c187aa16
6 changed files with 45 additions and 5 deletions

View File

@@ -0,0 +1,8 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.
namespace Snap.Hutao.Message;
internal sealed class BackgroundImageTypeChangedMessage
{
}

View File

@@ -18,7 +18,7 @@
<mxi:Interaction.Behaviors>
<shcb:PeriodicInvokeCommandOrOnActualThemeChangedBehavior
Command="{Binding UpdateBackgroundCommand}"
CommandParameter="{x:Bind BackdroundImagePresenter}"
CommandParameter="{x:Bind BackgroundImagePresenter}"
Period="0:5:0"/>
</mxi:Interaction.Behaviors>
@@ -31,7 +31,7 @@
<!-- Background="{ThemeResource SolidBackgroundFillColorBaseBrush}" -->
<Grid Transitions="{ThemeResource EntranceThemeTransitions}">
<Image
x:Name="BackdroundImagePresenter"
x:Name="BackgroundImagePresenter"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Opacity="0"

View File

@@ -27,10 +27,14 @@ internal sealed partial class MainView : UserControl
/// </summary>
public MainView()
{
DataContext = Ioc.Default.GetRequiredService<MainViewModel>();
IServiceProvider serviceProvider = Ioc.Default;
MainViewModel mainViewModel = serviceProvider.GetRequiredService<MainViewModel>();
DataContext = mainViewModel;
InitializeComponent();
IServiceProvider serviceProvider = Ioc.Default;
mainViewModel.Initialize(BackgroundImagePresenter);
navigationService = serviceProvider.GetRequiredService<INavigationService>();
if (navigationService is INavigationInitialization navigationInitialization)

View File

@@ -0,0 +1,11 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.
using Microsoft.UI.Xaml.Controls;
namespace Snap.Hutao.ViewModel.Main;
internal interface IMainViewModelInitialization
{
void Initialize(Image backgroundImagePresenter);
}

View File

@@ -1,23 +1,37 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.
using CommunityToolkit.Mvvm.Messaging;
using CommunityToolkit.WinUI.Animations;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media.Animation;
using Snap.Hutao.Control.Animation;
using Snap.Hutao.Control.Theme;
using Snap.Hutao.Message;
using Snap.Hutao.Service.BackgroundImage;
using Snap.Hutao.ViewModel.Main;
namespace Snap.Hutao.ViewModel;
[ConstructorGenerated]
[Injection(InjectAs.Singleton)]
internal sealed partial class MainViewModel : Abstraction.ViewModel
internal sealed partial class MainViewModel : Abstraction.ViewModel, IMainViewModelInitialization, IRecipient<BackgroundImageTypeChangedMessage>
{
private readonly IBackgroundImageService backgroundImageService;
private readonly ITaskContext taskContext;
private BackgroundImage? previousBackgroundImage;
private Image backgroundImagePresenter;
public void Initialize(Image backgroundImagePresenter)
{
this.backgroundImagePresenter = backgroundImagePresenter;
}
public void Receive(BackgroundImageTypeChangedMessage message)
{
UpdateBackgroundAsync(backgroundImagePresenter).SafeForget();
}
[Command("UpdateBackgroundCommand")]
private async Task UpdateBackgroundAsync(Image presenter)

View File

@@ -1,6 +1,7 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.
using CommunityToolkit.Mvvm.Messaging;
using Microsoft.UI.Xaml.Controls;
using Microsoft.Windows.AppLifecycle;
using Snap.Hutao.Core;
@@ -54,6 +55,7 @@ internal sealed partial class SettingViewModel : Abstraction.ViewModel
private readonly IUserService userService;
private readonly ITaskContext taskContext;
private readonly AppOptions appOptions;
private readonly IMessenger messenger;
private NameValue<BackdropType>? selectedBackdropType;
private NameValue<BackgroundImageType>? selectedBackgroundImageType;
@@ -100,6 +102,7 @@ internal sealed partial class SettingViewModel : Abstraction.ViewModel
if (SetProperty(ref selectedBackgroundImageType, value) && value is not null)
{
AppOptions.BackgroundImageType = value.Value;
messenger.Send(new Message.BackgroundImageTypeChangedMessage());
}
}
}