Stop using Ioc type as service locator

このコミットが含まれているのは:
Sergio Pedri committed 2023-01-17 00:36:40 +01:00
1 親 d5d42d960c
コミット f3d4820de0
2ファイル変更
+19 -11

検索条件に一致するファイルはありません

+13 -5
ファイルの表示
@@ -1,4 +1,4 @@
using CommunityToolkit.Mvvm.DependencyInjection;
using System;
using ComputeSharp.SwapChain.Core.Services;
using ComputeSharp.SwapChain.Core.ViewModels;
using ComputeSharp.SwapChain.Uwp.Services;
@@ -28,12 +28,19 @@ sealed partial class App : Application
this.InitializeComponent();
}
/// <inheritdoc cref="Application.Current"/>
public static new App Current => (App)Application.Current;
/// <summary>
/// Gets the current <see cref="IServiceProvider"/> instance.
/// </summary>
public IServiceProvider Services { get; } = ConfigureServices();
/// <inheritdoc/>
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
if (Window.Current.Content is not MainView)
{
ConfigureServices();
StyleTitleBar();
ExpandViewIntoTitleBar();
@@ -49,14 +56,15 @@ sealed partial class App : Application
/// <summary>
/// Configures the services for the application.
/// </summary>
private static void ConfigureServices()
/// <returns>The <see cref="IServiceProvider"/> instance to use.</returns>
private static IServiceProvider ConfigureServices()
{
ServiceCollection services = new();
_ = services.AddAnalyticsService();
_ = services.AddTransient<MainViewModel>(static services => new MainViewModel(services.GetRequiredService<IAnalyticsService>()));
_ = services.AddTransient(static services => new MainViewModel(services.GetRequiredService<IAnalyticsService>()));
Ioc.Default.ConfigureServices(services.BuildServiceProvider());
return services.BuildServiceProvider();
}
/// <summary>
+6 -6
ファイルの表示
@@ -1,9 +1,9 @@
using System;
using CommunityToolkit.Mvvm.DependencyInjection;
using ComputeSharp.SwapChain.Core.Constants;
using ComputeSharp.SwapChain.Core.Services;
using ComputeSharp.SwapChain.Core.ViewModels;
using ComputeSharp.Uwp;
using Microsoft.Extensions.DependencyInjection;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
@@ -18,7 +18,7 @@ public sealed partial class MainView : UserControl
public MainView()
{
this.InitializeComponent();
this.DataContext = Ioc.Default.GetRequiredService<MainViewModel>();
this.DataContext = App.Current.Services.GetRequiredService<MainViewModel>();
}
/// <summary>
@@ -29,7 +29,7 @@ public sealed partial class MainView : UserControl
// Opens the shader selection panel
private void OpenShaderSelectionPanelButton_Click(object sender, RoutedEventArgs e)
{
Ioc.Default.GetRequiredService<IAnalyticsService>().Log(Event.OpenShaderSelectionPanel);
App.Current.Services.GetRequiredService<IAnalyticsService>().Log(Event.OpenShaderSelectionPanel);
_ = this.Root.Resources.Remove("ShaderSelectionPanel");
this.Root.Children.Add(this.ShaderSelectionPanel);
@@ -38,7 +38,7 @@ public sealed partial class MainView : UserControl
// Hides the shader selection panel
private void ShaderSelectionPanel_Tapped(object sender, TappedRoutedEventArgs e)
{
Ioc.Default.GetRequiredService<IAnalyticsService>().Log(Event.CloseShaderSelectionPanel);
App.Current.Services.GetRequiredService<IAnalyticsService>().Log(Event.CloseShaderSelectionPanel);
_ = this.Root.Children.Remove(this.ShaderSelectionPanel);
}
@@ -52,7 +52,7 @@ public sealed partial class MainView : UserControl
// Logs rendering failed in the main panel
private void MainShaderPanel_RenderingFailed(AnimatedComputeShaderPanel sender, RenderingFailedEventArgs args)
{
Ioc.Default.GetRequiredService<IAnalyticsService>().Log(args.Exception, (nameof(Error), Error.RenderingFailedOnMainPanel));
App.Current.Services.GetRequiredService<IAnalyticsService>().Log(args.Exception, (nameof(Error), Error.RenderingFailedOnMainPanel));
this.RenderingErrorInfoBar.IsOpen = true;
}
@@ -60,7 +60,7 @@ public sealed partial class MainView : UserControl
// Logs rendering failed in a secondary panel
private void SelectionShaderPanel_RenderingFailed(AnimatedComputeShaderPanel sender, RenderingFailedEventArgs args)
{
Ioc.Default.GetRequiredService<IAnalyticsService>().Log(args.Exception, (nameof(Error), Error.RenderingFailedOnSelectionPanel));
App.Current.Services.GetRequiredService<IAnalyticsService>().Log(args.Exception, (nameof(Error), Error.RenderingFailedOnSelectionPanel));
this.RenderingErrorInfoBar.IsOpen = true;
}