Skip to content

Commit 8ef8f4c

Browse files
committed
UWP ViewModels ctor injection
1 parent fe09efb commit 8ef8f4c

31 files changed

+144
-55
lines changed

samples/MvvmSampleUwp/App.xaml.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
using Refit;
1212
using MvvmSampleUwp.Helpers;
1313
using MvvmSample.Core.Services;
14-
14+
using MvvmSample.Core.ViewModels.Widgets;
15+
using MvvmSample.Core.ViewModels;
16+
1517
namespace MvvmSampleUwp
1618
{
1719
/// <summary>
@@ -41,10 +43,20 @@ protected override void OnLaunched(LaunchActivatedEventArgs e)
4143

4244
// Register services
4345
Ioc.Default.ConfigureServices(
44-
new ServiceCollection()
46+
new ServiceCollection()
47+
//Services
4548
.AddSingleton<IFilesService, FilesService>()
4649
.AddSingleton<ISettingsService, SettingsService>()
47-
.AddSingleton(RestService.For<IRedditService>("https://www.reddit.com/"))
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>()
4860
.BuildServiceProvider());
4961
}
5062

samples/MvvmSampleUwp/Views/AsyncRelayCommandPage.xaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313
xmlns:converters="using:Microsoft.Toolkit.Uwp.UI.Converters"
1414
mc:Ignorable="d"
1515
NavigationCacheMode="Enabled">
16-
<Page.DataContext>
17-
<viewModels:AsyncRelayCommandPageViewModel x:Name="ViewModel"/>
18-
</Page.DataContext>
1916
<interactivity:Interaction.Behaviors>
2017
<core:EventTriggerBehavior EventName="Loaded">
2118
<core:InvokeCommandAction Command="{x:Bind ViewModel.LoadDocsCommand}" CommandParameter="AsyncRelayCommand"/>

samples/MvvmSampleUwp/Views/AsyncRelayCommandPage.xaml.cs

Lines changed: 9 additions & 1 deletion
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
@@ -13,7 +15,13 @@ public sealed partial class AsyncRelayCommandPage : Page
1315
{
1416
public AsyncRelayCommandPage()
1517
{
16-
this.InitializeComponent();
18+
this.InitializeComponent();
19+
20+
ViewModel = Ioc.Default.GetRequiredService<AsyncRelayCommandPageViewModel>();
21+
22+
DataContext = ViewModel;
1723
}
24+
25+
public AsyncRelayCommandPageViewModel ViewModel { get; }
1826
}
1927
}

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:interactivity="using:Microsoft.Xaml.Interactivity"
1111
mc:Ignorable="d"
1212
NavigationCacheMode="Enabled">
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,6 +16,12 @@ 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
}
1927
}

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:interactivity="using:Microsoft.Xaml.Interactivity"
1111
mc:Ignorable="d"
1212
NavigationCacheMode="Enabled">
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,6 +16,12 @@ 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
}
1927
}

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:core="using:Microsoft.Xaml.Interactions.Core"
1111
mc:Ignorable="d"
1212
NavigationCacheMode="Enabled">
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,6 +16,12 @@ 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
}
1927
}

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:core="using:Microsoft.Xaml.Interactions.Core"
1111
mc:Ignorable="d"
1212
NavigationCacheMode="Enabled">
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)