Skip to content

Commit 8f966e2

Browse files
committed
Adding dependency injection
1 parent 7db09ba commit 8f966e2

File tree

5 files changed

+56
-9
lines changed

5 files changed

+56
-9
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using CommunityToolkit.Diagnostics;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using System;
4+
5+
#nullable enable
6+
7+
namespace ModernUwpTemplate;
8+
9+
partial class App
10+
{
11+
private IServiceProvider? _serviceProvider;
12+
13+
public static IServiceProvider Services
14+
{
15+
get
16+
{
17+
IServiceProvider? serviceProvider = ((App)Current)._serviceProvider;
18+
19+
if (serviceProvider is null)
20+
{
21+
ThrowHelper.ThrowInvalidOperationException("The service provider is not initialized");
22+
}
23+
24+
return serviceProvider;
25+
}
26+
}
27+
28+
private static IServiceProvider ConfigureServices()
29+
{
30+
var provider = new ServiceCollection()
31+
// TODO insert services here.
32+
// Example: .AddSingleton<IAppSettings, AppSettings>()
33+
.BuildServiceProvider(true);
34+
35+
return provider;
36+
}
37+
38+
}

templateSource/ModernUwpTemplate/App.xaml.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,15 @@ protected override void OnLaunched(LaunchActivatedEventArgs e)
5050
// just ensure that the window is active
5151
if (rootFrame == null)
5252
{
53+
_serviceProvider ??= ConfigureServices();
54+
5355
// Create a Frame to act as the navigation context and navigate to the first page
5456
rootFrame = new Frame();
5557

58+
// Removes the title bar.
59+
// Comment out below if title bar is desired.
60+
CustomizeTitleBar(rootFrame.ActualTheme == ElementTheme.Dark);
61+
5662
rootFrame.NavigationFailed += OnNavigationFailed;
5763

5864
if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
@@ -71,15 +77,11 @@ protected override void OnLaunched(LaunchActivatedEventArgs e)
7177
// When the navigation stack isn't restored navigate to the first page,
7278
// configuring the new page by passing required information as a navigation
7379
// parameter
74-
rootFrame.Navigate(typeof(MainPage), e.Arguments);
80+
rootFrame.Navigate(typeof(Views.MainPage), e.Arguments);
7581
}
7682
// Ensure the current window is active
7783
Window.Current.Activate();
7884
}
79-
80-
// Removes the title bar.
81-
// Comment out below if title bar is desired.
82-
CustomizeTitleBar(rootFrame.ActualTheme == ElementTheme.Dark);
8385
}
8486

8587
/// <summary>

templateSource/ModernUwpTemplate/ModernUwpTemplate.csproj

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,11 @@
117117
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
118118
</PropertyGroup>
119119
<ItemGroup>
120+
<Compile Include="App.Configuration.xaml.cs" />
120121
<Compile Include="App.xaml.cs">
121122
<DependentUpon>App.xaml</DependentUpon>
122123
</Compile>
123-
<Compile Include="MainPage.xaml.cs">
124+
<Compile Include="Views\MainPage.xaml.cs">
124125
<DependentUpon>MainPage.xaml</DependentUpon>
125126
</Compile>
126127
<Compile Include="Properties\AssemblyInfo.cs" />
@@ -145,12 +146,18 @@
145146
<Generator>MSBuild:Compile</Generator>
146147
<SubType>Designer</SubType>
147148
</ApplicationDefinition>
148-
<Page Include="MainPage.xaml">
149+
<Page Include="Views\MainPage.xaml">
149150
<Generator>MSBuild:Compile</Generator>
150151
<SubType>Designer</SubType>
151152
</Page>
152153
</ItemGroup>
153154
<ItemGroup>
155+
<PackageReference Include="CommunityToolkit.Diagnostics">
156+
<Version>8.2.1</Version>
157+
</PackageReference>
158+
<PackageReference Include="Microsoft.Extensions.DependencyInjection">
159+
<Version>7.0.0</Version>
160+
</PackageReference>
154161
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform">
155162
<Version>6.2.14</Version>
156163
</PackageReference>

templateSource/ModernUwpTemplate/MainPage.xaml renamed to templateSource/ModernUwpTemplate/Views/MainPage.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<Page
2-
x:Class="ModernUwpTemplate.MainPage"
2+
x:Class="ModernUwpTemplate.Views.MainPage"
33
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
44
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
55
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

templateSource/ModernUwpTemplate/MainPage.xaml.cs renamed to templateSource/ModernUwpTemplate/Views/MainPage.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
#nullable enable
1717

18-
namespace ModernUwpTemplate;
18+
namespace ModernUwpTemplate.Views;
1919

2020
/// <summary>
2121
/// An empty page that can be used on its own or navigated to within a Frame.

0 commit comments

Comments
 (0)