From f3d4820de0da3c565bc107eb8b0e2db5d81398af Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 17 Jan 2023 00:36:40 +0100 Subject: [PATCH] Stop using Ioc type as service locator --- samples/ComputeSharp.SwapChain.Uwp/App.xaml.cs | 18 +++++++++++++----- .../Views/MainView.xaml.cs | 12 ++++++------ 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/samples/ComputeSharp.SwapChain.Uwp/App.xaml.cs b/samples/ComputeSharp.SwapChain.Uwp/App.xaml.cs index 57495de3..fe9bf060 100644 --- a/samples/ComputeSharp.SwapChain.Uwp/App.xaml.cs +++ b/samples/ComputeSharp.SwapChain.Uwp/App.xaml.cs @@ -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(); } + /// + public static new App Current => (App)Application.Current; + + /// + /// Gets the current instance. + /// + public IServiceProvider Services { get; } = ConfigureServices(); + /// 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 /// /// Configures the services for the application. /// - private static void ConfigureServices() + /// The instance to use. + private static IServiceProvider ConfigureServices() { ServiceCollection services = new(); _ = services.AddAnalyticsService(); - _ = services.AddTransient(static services => new MainViewModel(services.GetRequiredService())); + _ = services.AddTransient(static services => new MainViewModel(services.GetRequiredService())); - Ioc.Default.ConfigureServices(services.BuildServiceProvider()); + return services.BuildServiceProvider(); } /// diff --git a/samples/ComputeSharp.SwapChain.Uwp/Views/MainView.xaml.cs b/samples/ComputeSharp.SwapChain.Uwp/Views/MainView.xaml.cs index 9973a613..dac017c5 100644 --- a/samples/ComputeSharp.SwapChain.Uwp/Views/MainView.xaml.cs +++ b/samples/ComputeSharp.SwapChain.Uwp/Views/MainView.xaml.cs @@ -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(); + this.DataContext = App.Current.Services.GetRequiredService(); } /// @@ -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().Log(Event.OpenShaderSelectionPanel); + App.Current.Services.GetRequiredService().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().Log(Event.CloseShaderSelectionPanel); + App.Current.Services.GetRequiredService().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().Log(args.Exception, (nameof(Error), Error.RenderingFailedOnMainPanel)); + App.Current.Services.GetRequiredService().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().Log(args.Exception, (nameof(Error), Error.RenderingFailedOnSelectionPanel)); + App.Current.Services.GetRequiredService().Log(args.Exception, (nameof(Error), Error.RenderingFailedOnSelectionPanel)); this.RenderingErrorInfoBar.IsOpen = true; }