mirror of
https://jihulab.com/DGP-Studio/Snap.Hutao.git
synced 2025-11-19 21:02:53 +08:00
InitializeDataContext
This commit is contained in:
@@ -28,4 +28,21 @@ internal static class FrameworkElementExtension
|
||||
frameworkElement.IsRightTapEnabled = false;
|
||||
frameworkElement.IsTabStop = false;
|
||||
}
|
||||
|
||||
public static void InitializeDataContext<TDataContext>(this FrameworkElement frameworkElement, IServiceProvider? serviceProvider = default)
|
||||
where TDataContext : class
|
||||
{
|
||||
IServiceProvider service = serviceProvider ?? Ioc.Default;
|
||||
try
|
||||
{
|
||||
frameworkElement.DataContext = service.GetRequiredService<TDataContext>();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
ILogger? logger = service.GetRequiredService(typeof(ILogger<>).MakeGenericType([frameworkElement.GetType()])) as ILogger;
|
||||
logger?.LogError(ex, "Failed to initialize DataContext");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -44,9 +44,17 @@ internal class ScopedPage : Page
|
||||
protected void InitializeWith<TViewModel>()
|
||||
where TViewModel : class, IViewModel
|
||||
{
|
||||
IViewModel viewModel = currentScope.ServiceProvider.GetRequiredService<TViewModel>();
|
||||
viewModel.CancellationToken = viewCancellationTokenSource.Token;
|
||||
DataContext = viewModel;
|
||||
try
|
||||
{
|
||||
IViewModel viewModel = currentScope.ServiceProvider.GetRequiredService<TViewModel>();
|
||||
viewModel.CancellationToken = viewCancellationTokenSource.Token;
|
||||
DataContext = viewModel;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
currentScope.ServiceProvider.GetRequiredService<ILogger<ScopedPage>>().LogError(ex, "Failed to initialize view model.");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// Licensed under the MIT license.
|
||||
|
||||
using Microsoft.UI.Xaml;
|
||||
using Snap.Hutao.Control.Extension;
|
||||
using Snap.Hutao.Core.Windowing;
|
||||
using Snap.Hutao.ViewModel.Game;
|
||||
using Snap.Hutao.Win32.UI.WindowsAndMessaging;
|
||||
@@ -35,7 +36,7 @@ internal sealed partial class LaunchGameWindow : Window, IDisposable, IWindowOpt
|
||||
scope = serviceProvider.CreateScope();
|
||||
windowOptions = new(this, DragableGrid, new(MaxWidth, MaxHeight));
|
||||
this.InitializeController(serviceProvider);
|
||||
RootGrid.DataContext = scope.ServiceProvider.GetRequiredService<LaunchGameViewModel>();
|
||||
RootGrid.InitializeDataContext<LaunchGameViewModel>(scope.ServiceProvider);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
|
||||
@@ -17,16 +17,15 @@ namespace Snap.Hutao.Service;
|
||||
/// <inheritdoc/>
|
||||
[HighQuality]
|
||||
[ConstructorGenerated]
|
||||
[Injection(InjectAs.Transient, typeof(IAnnouncementService))]
|
||||
[Injection(InjectAs.Scoped, typeof(IAnnouncementService))]
|
||||
internal sealed partial class AnnouncementService : IAnnouncementService
|
||||
{
|
||||
private static readonly string CacheKey = $"{nameof(AnnouncementService)}.Cache.{nameof(AnnouncementWrapper)}";
|
||||
|
||||
private readonly AnnouncementClient announcementClient;
|
||||
private readonly IServiceScopeFactory serviceScopeFactory;
|
||||
private readonly ITaskContext taskContext;
|
||||
private readonly IMemoryCache memoryCache;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async ValueTask<AnnouncementWrapper> GetAnnouncementWrapperAsync(string languageCode, Region region, CancellationToken cancellationToken = default)
|
||||
{
|
||||
// 缓存中存在记录,直接返回
|
||||
@@ -36,29 +35,37 @@ internal sealed partial class AnnouncementService : IAnnouncementService
|
||||
}
|
||||
|
||||
await taskContext.SwitchToBackgroundAsync();
|
||||
Response<AnnouncementWrapper> announcementWrapperResponse = await announcementClient
|
||||
.GetAnnouncementsAsync(languageCode, region, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
if (!announcementWrapperResponse.IsOk())
|
||||
List<AnnouncementContent>? contents;
|
||||
AnnouncementWrapper wrapper;
|
||||
using (IServiceScope scope = serviceScopeFactory.CreateScope())
|
||||
{
|
||||
return default!;
|
||||
AnnouncementClient announcementClient = scope.ServiceProvider.GetRequiredService<AnnouncementClient>();
|
||||
|
||||
Response<AnnouncementWrapper> announcementWrapperResponse = await announcementClient
|
||||
.GetAnnouncementsAsync(languageCode, region, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
if (!announcementWrapperResponse.IsOk())
|
||||
{
|
||||
return default!;
|
||||
}
|
||||
|
||||
wrapper = announcementWrapperResponse.Data;
|
||||
|
||||
Response<ListWrapper<AnnouncementContent>> announcementContentResponse = await announcementClient
|
||||
.GetAnnouncementContentsAsync(languageCode, region, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
if (!announcementContentResponse.IsOk())
|
||||
{
|
||||
return default!;
|
||||
}
|
||||
|
||||
contents = announcementContentResponse.Data.List;
|
||||
}
|
||||
|
||||
AnnouncementWrapper wrapper = announcementWrapperResponse.Data;
|
||||
Response<ListWrapper<AnnouncementContent>> announcementContentResponse = await announcementClient
|
||||
.GetAnnouncementContentsAsync(languageCode, region, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
if (!announcementContentResponse.IsOk())
|
||||
{
|
||||
return default!;
|
||||
}
|
||||
|
||||
List<AnnouncementContent> contents = announcementContentResponse.Data.List;
|
||||
|
||||
Dictionary<int, string> contentMap = contents
|
||||
.ToDictionary(id => id.AnnId, content => content.Content);
|
||||
Dictionary<int, string> contentMap = contents.ToDictionary(id => id.AnnId, content => content.Content);
|
||||
|
||||
// 将活动公告置于前方
|
||||
wrapper.List.Reverse();
|
||||
@@ -75,8 +82,7 @@ internal sealed partial class AnnouncementService : IAnnouncementService
|
||||
{
|
||||
foreach (ref readonly WebAnnouncement item in CollectionsMarshal.AsSpan(listWrapper.List))
|
||||
{
|
||||
contentMap.TryGetValue(item.AnnId, out string? rawContent);
|
||||
item.Content = rawContent ?? string.Empty;
|
||||
item.Content = contentMap.GetValueOrDefault(item.AnnId, string.Empty);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// Licensed under the MIT license.
|
||||
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using Snap.Hutao.Control.Extension;
|
||||
|
||||
namespace Snap.Hutao.View.Card;
|
||||
|
||||
@@ -15,7 +16,7 @@ internal sealed partial class AchievementCard : Button
|
||||
/// </summary>
|
||||
public AchievementCard()
|
||||
{
|
||||
DataContext = Ioc.Default.GetRequiredService<ViewModel.Achievement.AchievementViewModelSlim>();
|
||||
this.InitializeDataContext<ViewModel.Achievement.AchievementViewModelSlim>();
|
||||
InitializeComponent();
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
// Licensed under the MIT license.
|
||||
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using Snap.Hutao.Control.Extension;
|
||||
|
||||
namespace Snap.Hutao.View.Card;
|
||||
|
||||
@@ -15,7 +16,7 @@ internal sealed partial class DailyNoteCard : Button
|
||||
/// </summary>
|
||||
public DailyNoteCard()
|
||||
{
|
||||
DataContext = Ioc.Default.GetRequiredService<ViewModel.DailyNote.DailyNoteViewModelSlim>();
|
||||
this.InitializeDataContext<ViewModel.DailyNote.DailyNoteViewModelSlim>();
|
||||
InitializeComponent();
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
// Licensed under the MIT license.
|
||||
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using Snap.Hutao.Control.Extension;
|
||||
|
||||
namespace Snap.Hutao.View.Card;
|
||||
|
||||
@@ -15,7 +16,7 @@ internal sealed partial class GachaStatisticsCard : Button
|
||||
/// </summary>
|
||||
public GachaStatisticsCard()
|
||||
{
|
||||
DataContext = Ioc.Default.GetRequiredService<ViewModel.GachaLog.GachaLogViewModelSlim>();
|
||||
this.InitializeDataContext<ViewModel.GachaLog.GachaLogViewModelSlim>();
|
||||
InitializeComponent();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// Licensed under the MIT license.
|
||||
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using Snap.Hutao.Control.Extension;
|
||||
|
||||
namespace Snap.Hutao.View.Card;
|
||||
|
||||
@@ -15,7 +16,7 @@ internal sealed partial class LaunchGameCard : Button
|
||||
/// </summary>
|
||||
public LaunchGameCard()
|
||||
{
|
||||
DataContext = Ioc.Default.GetRequiredService<ViewModel.Game.LaunchGameViewModelSlim>();
|
||||
this.InitializeDataContext<ViewModel.Game.LaunchGameViewModelSlim>();
|
||||
InitializeComponent();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ internal sealed partial class GuideView : UserControl
|
||||
{
|
||||
public GuideView()
|
||||
{
|
||||
this.InitializeDataContext<GuideViewModel>();
|
||||
InitializeComponent();
|
||||
DataContext = this.ServiceProvider().GetRequiredService<GuideViewModel>();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// Licensed under the MIT license.
|
||||
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using Snap.Hutao.Control.Extension;
|
||||
using Snap.Hutao.Service.Navigation;
|
||||
using Snap.Hutao.View.Page;
|
||||
using Snap.Hutao.ViewModel;
|
||||
@@ -23,12 +24,11 @@ internal sealed partial class MainView : UserControl
|
||||
{
|
||||
IServiceProvider serviceProvider = Ioc.Default;
|
||||
|
||||
MainViewModel mainViewModel = serviceProvider.GetRequiredService<MainViewModel>();
|
||||
this.InitializeDataContext<MainViewModel>(serviceProvider);
|
||||
|
||||
DataContext = mainViewModel;
|
||||
InitializeComponent();
|
||||
|
||||
mainViewModel.Initialize(new BackgroundImagePresenterAccessor(BackgroundImagePresenter));
|
||||
(DataContext as MainViewModel)?.Initialize(new BackgroundImagePresenterAccessor(BackgroundImagePresenter));
|
||||
|
||||
navigationService = serviceProvider.GetRequiredService<INavigationService>();
|
||||
if (navigationService is INavigationInitialization navigationInitialization)
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using Snap.Hutao.Control.Extension;
|
||||
using Snap.Hutao.ViewModel;
|
||||
|
||||
namespace Snap.Hutao.View;
|
||||
@@ -15,7 +16,7 @@ internal sealed partial class TitleView : UserControl
|
||||
{
|
||||
public TitleView()
|
||||
{
|
||||
DataContext = Ioc.Default.GetRequiredService<TitleViewModel>();
|
||||
this.InitializeDataContext<TitleViewModel>();
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// Licensed under the MIT license.
|
||||
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using Snap.Hutao.Control.Extension;
|
||||
using Snap.Hutao.ViewModel.User;
|
||||
|
||||
namespace Snap.Hutao.View;
|
||||
@@ -17,7 +18,7 @@ internal sealed partial class UserView : UserControl
|
||||
/// </summary>
|
||||
public UserView()
|
||||
{
|
||||
this.InitializeDataContext<UserViewModel>();
|
||||
InitializeComponent();
|
||||
DataContext = Ioc.Default.GetRequiredService<UserViewModel>();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user