Skip to content

Commit f93b35b

Browse files
committed
UWP ViewModels ctor injection
1 parent 87a42ec commit f93b35b

31 files changed

+141
-53
lines changed

samples/MvvmSampleUwp/App.xaml.cs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
using Refit;
1212
using MvvmSampleUwp.Helpers;
1313
using MvvmSample.Core.Services;
14+
using MvvmSample.Core.ViewModels.Widgets;
15+
using MvvmSample.Core.ViewModels;
1416

1517
namespace MvvmSampleUwp;
1618

@@ -39,14 +41,24 @@ protected override void OnLaunched(LaunchActivatedEventArgs e)
3941
TitleBarHelper.StyleTitleBar();
4042
TitleBarHelper.ExpandViewIntoTitleBar();
4143

42-
// Register services
43-
Ioc.Default.ConfigureServices(
44-
new ServiceCollection()
45-
.AddSingleton<IFilesService, FilesService>()
46-
.AddSingleton<ISettingsService, SettingsService>()
47-
.AddSingleton(RestService.For<IRedditService>("https://www.reddit.com/"))
48-
.BuildServiceProvider());
49-
}
44+
// Register services
45+
Ioc.Default.ConfigureServices(
46+
new ServiceCollection()
47+
//Services
48+
.AddSingleton<IFilesService, FilesService>()
49+
.AddSingleton<ISettingsService, SettingsService>()
50+
.AddSingleton(RestService.For<IRedditService>("https://www.reddit.com/"))
51+
//ViewModels
52+
.AddTransient<PostWidgetViewModel>()
53+
.AddTransient<SubredditWidgetViewModel>()
54+
.AddTransient<AsyncRelayCommandPageViewModel>()
55+
.AddTransient<IocPageViewModel>()
56+
.AddTransient<MessengerPageViewModel>()
57+
.AddTransient<ObservableObjectPageViewModel>()
58+
.AddTransient<RelayCommandPageViewModel>()
59+
.AddTransient<SamplePageViewModel>()
60+
.BuildServiceProvider());
61+
}
5062

5163
// Enable the prelaunch if needed, and activate the window
5264
if (e.PrelaunchActivated == false)

samples/MvvmSampleUwp/Views/AsyncRelayCommandPage.xaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212
xmlns:viewModels="using:MvvmSample.Core.ViewModels"
1313
NavigationCacheMode="Enabled"
1414
mc:Ignorable="d">
15-
<Page.DataContext>
16-
<viewModels:AsyncRelayCommandPageViewModel x:Name="ViewModel" />
17-
</Page.DataContext>
1815
<interactivity:Interaction.Behaviors>
1916
<core:EventTriggerBehavior EventName="Loaded">
2017
<core:InvokeCommandAction Command="{x:Bind ViewModel.LoadDocsCommand}" CommandParameter="AsyncRelayCommand" />

samples/MvvmSampleUwp/Views/AsyncRelayCommandPage.xaml.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using CommunityToolkit.Mvvm.DependencyInjection;
6+
using MvvmSample.Core.ViewModels;
57
using Windows.UI.Xaml.Controls;
68

79
namespace MvvmSampleUwp.Views;
@@ -14,5 +16,11 @@ public sealed partial class AsyncRelayCommandPage : Page
1416
public AsyncRelayCommandPage()
1517
{
1618
this.InitializeComponent();
19+
20+
ViewModel = Ioc.Default.GetRequiredService<AsyncRelayCommandPageViewModel>();
21+
22+
DataContext = ViewModel;
1723
}
24+
25+
public AsyncRelayCommandPageViewModel ViewModel { get; }
1826
}

samples/MvvmSampleUwp/Views/BuildingTheUIPage.xaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
xmlns:viewModels="using:MvvmSample.Core.ViewModels"
1111
NavigationCacheMode="Enabled"
1212
mc:Ignorable="d">
13-
<Page.DataContext>
14-
<viewModels:SamplePageViewModel x:Name="ViewModel" />
15-
</Page.DataContext>
1613
<interactivity:Interaction.Behaviors>
1714
<core:EventTriggerBehavior EventName="Loaded">
1815
<core:InvokeCommandAction Command="{x:Bind ViewModel.LoadDocsCommand}" CommandParameter="PuttingThingsTogether" />

samples/MvvmSampleUwp/Views/BuildingTheUIPage.xaml.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using CommunityToolkit.Mvvm.DependencyInjection;
6+
using MvvmSample.Core.ViewModels;
57
using Windows.UI.Xaml.Controls;
68

79
namespace MvvmSampleUwp.Views;
@@ -14,5 +16,11 @@ public sealed partial class BuildingTheUIPage : Page
1416
public BuildingTheUIPage()
1517
{
1618
this.InitializeComponent();
19+
20+
ViewModel = Ioc.Default.GetRequiredService<SamplePageViewModel>();
21+
22+
DataContext = ViewModel;
1723
}
24+
25+
public SamplePageViewModel ViewModel { get; }
1826
}

samples/MvvmSampleUwp/Views/IntroductionPage.xaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
xmlns:viewModels="using:MvvmSample.Core.ViewModels"
1111
NavigationCacheMode="Enabled"
1212
mc:Ignorable="d">
13-
<Page.DataContext>
14-
<viewModels:ObservableObjectPageViewModel x:Name="ViewModel" />
15-
</Page.DataContext>
1613
<interactivity:Interaction.Behaviors>
1714
<core:EventTriggerBehavior EventName="Loaded">
1815
<core:InvokeCommandAction Command="{x:Bind ViewModel.LoadDocsCommand}" CommandParameter="Introduction" />

samples/MvvmSampleUwp/Views/IntroductionPage.xaml.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using CommunityToolkit.Mvvm.DependencyInjection;
6+
using MvvmSample.Core.ViewModels;
57
using Windows.UI.Xaml.Controls;
68

79
namespace MvvmSampleUwp.Views;
@@ -14,5 +16,11 @@ public sealed partial class IntroductionPage : Page
1416
public IntroductionPage()
1517
{
1618
this.InitializeComponent();
19+
20+
ViewModel = Ioc.Default.GetRequiredService<ObservableObjectPageViewModel>();
21+
22+
DataContext = ViewModel;
1723
}
24+
25+
public ObservableObjectPageViewModel ViewModel { get; }
1826
}

samples/MvvmSampleUwp/Views/IocPage.xaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
xmlns:viewModels="using:MvvmSample.Core.ViewModels"
1111
NavigationCacheMode="Enabled"
1212
mc:Ignorable="d">
13-
<Page.DataContext>
14-
<viewModels:IocPageViewModel x:Name="ViewModel" />
15-
</Page.DataContext>
1613
<interactivity:Interaction.Behaviors>
1714
<core:EventTriggerBehavior EventName="Loaded">
1815
<core:InvokeCommandAction Command="{x:Bind ViewModel.LoadDocsCommand}" CommandParameter="Ioc" />

samples/MvvmSampleUwp/Views/IocPage.xaml.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using CommunityToolkit.Mvvm.DependencyInjection;
6+
using MvvmSample.Core.ViewModels;
57
using Windows.UI.Xaml.Controls;
68

79
namespace MvvmSampleUwp.Views;
@@ -14,5 +16,11 @@ public sealed partial class IocPage : Page
1416
public IocPage()
1517
{
1618
this.InitializeComponent();
19+
20+
ViewModel = Ioc.Default.GetRequiredService<IocPageViewModel>();
21+
22+
DataContext = ViewModel;
1723
}
24+
25+
public IocPageViewModel ViewModel { get; }
1826
}

samples/MvvmSampleUwp/Views/MessengerPage.xaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
xmlns:viewModels="using:MvvmSample.Core.ViewModels"
1111
NavigationCacheMode="Enabled"
1212
mc:Ignorable="d">
13-
<Page.DataContext>
14-
<viewModels:MessengerPageViewModel x:Name="ViewModel" />
15-
</Page.DataContext>
1613
<interactivity:Interaction.Behaviors>
1714
<core:EventTriggerBehavior EventName="Loaded">
1815
<core:InvokeCommandAction Command="{x:Bind ViewModel.LoadDocsCommand}" CommandParameter="Messenger" />

0 commit comments

Comments
 (0)