Skip to content

ViewModelLocator to create ViewModel from DI container and initialize DataContext of View #75

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions samples/MvvmSampleUwp/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Linq;
using Windows.ApplicationModel.Activation;
using Windows.ApplicationModel.Core;
using Windows.UI.Xaml;
Expand Down Expand Up @@ -41,9 +42,7 @@ protected override void OnLaunched(LaunchActivatedEventArgs e)
TitleBarHelper.StyleTitleBar();
TitleBarHelper.ExpandViewIntoTitleBar();

// Register services
Ioc.Default.ConfigureServices(
new ServiceCollection()
var serviceProvider = new ServiceCollection()
//Services
.AddSingleton<IDialogService, DialogService>()
.AddSingleton<IFilesService, FilesService>()
Expand All @@ -60,7 +59,27 @@ protected override void OnLaunched(LaunchActivatedEventArgs e)
.AddTransient<ValidationFormWidgetViewModel>()
.AddTransient<RelayCommandPageViewModel>()
.AddTransient<SamplePageViewModel>()
.BuildServiceProvider());
.BuildServiceProvider();

// Register services
Ioc.Default.ConfigureServices(serviceProvider);

ViewModelLocator.SetViewModelFactory(view =>
{
var viewName = view.GetType().Name;
var viewModelName = $"{viewName}ViewModel";
var viewModelType = typeof(SamplePageViewModel).Assembly.GetTypes().FirstOrDefault(x => x.Name == viewModelName);

if (viewModelType == null)
{
if (viewModelName.Contains("Messenger"))
viewModelType = typeof(MessengerPageViewModel);
else
viewModelType = typeof(SamplePageViewModel);
}

return serviceProvider.GetService(viewModelType);
});
}

// Enable the prelaunch if needed, and activate the window
Expand Down
843 changes: 422 additions & 421 deletions samples/MvvmSampleUwp/MvvmSampleUwp.csproj

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions samples/MvvmSampleUwp/ViewModelLocator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using Windows.UI.Xaml;

namespace MvvmSampleUwp;
public static class ViewModelLocator
{
private static Func<object, object>? _viewModelFactory;

public static readonly DependencyProperty InitViewModelProperty =
DependencyProperty.RegisterAttached("InitViewModel", typeof(bool?), typeof(ViewModelLocator), new PropertyMetadata(null, OnInitViewModelChanged));

public static bool? GetInitViewModel(DependencyObject dependencyObject)
{
return (bool?)dependencyObject.GetValue(InitViewModelProperty);
}

public static void SetInitViewModel(DependencyObject dependencyObject, bool? value)
{
dependencyObject.SetValue(InitViewModelProperty, value);
}

private static void OnInitViewModelChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var needSet = (bool?)e.NewValue;
if (needSet == true && d is FrameworkElement element)
{
var viewModel = _viewModelFactory?.Invoke(d);
element.DataContext = viewModel;
}
}

public static void SetViewModelFactory(Func<object, object> factory)
=> _viewModelFactory = factory;
}
6 changes: 4 additions & 2 deletions samples/MvvmSampleUwp/Views/AsyncRelayCommandPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:muxc="using:Microsoft.UI.Xaml.Controls"
xmlns:muxc="using:Microsoft.UI.Xaml.Controls"
xmlns:mvvmSampleUwp="using:MvvmSampleUwp"
NavigationCacheMode="Enabled"
mc:Ignorable="d">
mc:Ignorable="d"
mvvmSampleUwp:ViewModelLocator.InitViewModel="True">
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Loaded">
<core:InvokeCommandAction Command="{x:Bind ViewModel.LoadDocsCommand}" CommandParameter="AsyncRelayCommand" />
Expand Down
3 changes: 0 additions & 3 deletions samples/MvvmSampleUwp/Views/AsyncRelayCommandPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using CommunityToolkit.Mvvm.DependencyInjection;
using MvvmSample.Core.ViewModels;
using Windows.UI.Xaml.Controls;

Expand All @@ -16,8 +15,6 @@ public sealed partial class AsyncRelayCommandPage : Page
public AsyncRelayCommandPage()
{
this.InitializeComponent();

DataContext = Ioc.Default.GetRequiredService<AsyncRelayCommandPageViewModel>();
}

public AsyncRelayCommandPageViewModel ViewModel => (AsyncRelayCommandPageViewModel)DataContext;
Expand Down
6 changes: 4 additions & 2 deletions samples/MvvmSampleUwp/Views/BuildingTheUIPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:mvvmSampleUwp="using:MvvmSampleUwp"
NavigationCacheMode="Enabled"
mc:Ignorable="d">
mc:Ignorable="d"
mvvmSampleUwp:ViewModelLocator.InitViewModel="True">
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Loaded">
<core:InvokeCommandAction Command="{x:Bind ViewModel.LoadDocsCommand}" CommandParameter="PuttingThingsTogether" />
Expand Down
3 changes: 0 additions & 3 deletions samples/MvvmSampleUwp/Views/BuildingTheUIPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using CommunityToolkit.Mvvm.DependencyInjection;
using MvvmSample.Core.ViewModels;
using Windows.UI.Xaml.Controls;

Expand All @@ -16,8 +15,6 @@ public sealed partial class BuildingTheUIPage : Page
public BuildingTheUIPage()
{
this.InitializeComponent();

DataContext = Ioc.Default.GetRequiredService<SamplePageViewModel>();
}

public SamplePageViewModel ViewModel => (SamplePageViewModel)DataContext;
Expand Down
6 changes: 4 additions & 2 deletions samples/MvvmSampleUwp/Views/IntroductionPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:mvvmSampleUwp="using:MvvmSampleUwp"
NavigationCacheMode="Enabled"
mc:Ignorable="d">
mc:Ignorable="d"
mvvmSampleUwp:ViewModelLocator.InitViewModel="True">
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Loaded">
<core:InvokeCommandAction Command="{x:Bind ViewModel.LoadDocsCommand}" CommandParameter="Introduction" />
Expand Down
5 changes: 1 addition & 4 deletions samples/MvvmSampleUwp/Views/IntroductionPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using CommunityToolkit.Mvvm.DependencyInjection;
using MvvmSample.Core.ViewModels;
using Windows.UI.Xaml.Controls;

Expand All @@ -16,9 +15,7 @@ public sealed partial class IntroductionPage : Page
public IntroductionPage()
{
this.InitializeComponent();

DataContext = Ioc.Default.GetRequiredService<ObservableObjectPageViewModel>();
}

public ObservableObjectPageViewModel ViewModel => (ObservableObjectPageViewModel)DataContext;
public SamplePageViewModel ViewModel => (SamplePageViewModel)DataContext;
}
6 changes: 4 additions & 2 deletions samples/MvvmSampleUwp/Views/IocPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:mvvmSampleUwp="using:MvvmSampleUwp"
NavigationCacheMode="Enabled"
mc:Ignorable="d">
mc:Ignorable="d"
mvvmSampleUwp:ViewModelLocator.InitViewModel="True">
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Loaded">
<core:InvokeCommandAction Command="{x:Bind ViewModel.LoadDocsCommand}" CommandParameter="Ioc" />
Expand Down
3 changes: 0 additions & 3 deletions samples/MvvmSampleUwp/Views/IocPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using CommunityToolkit.Mvvm.DependencyInjection;
using MvvmSample.Core.ViewModels;
using Windows.UI.Xaml.Controls;

Expand All @@ -16,8 +15,6 @@ public sealed partial class IocPage : Page
public IocPage()
{
this.InitializeComponent();

DataContext = Ioc.Default.GetRequiredService<IocPageViewModel>();
}

public IocPageViewModel ViewModel => (IocPageViewModel)DataContext;
Expand Down
6 changes: 4 additions & 2 deletions samples/MvvmSampleUwp/Views/MessengerPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:mvvmSampleUwp="using:MvvmSampleUwp"
NavigationCacheMode="Enabled"
mc:Ignorable="d">
mc:Ignorable="d"
mvvmSampleUwp:ViewModelLocator.InitViewModel="True">
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Loaded">
<core:InvokeCommandAction Command="{x:Bind ViewModel.LoadDocsCommand}" CommandParameter="Messenger" />
Expand Down
3 changes: 0 additions & 3 deletions samples/MvvmSampleUwp/Views/MessengerPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using CommunityToolkit.Mvvm.DependencyInjection;
using MvvmSample.Core.ViewModels;
using Windows.UI.Xaml.Controls;

Expand All @@ -16,8 +15,6 @@ public sealed partial class MessengerPage : Page
public MessengerPage()
{
this.InitializeComponent();

DataContext = Ioc.Default.GetRequiredService<MessengerPageViewModel>();
}

public MessengerPageViewModel ViewModel => (MessengerPageViewModel)DataContext;
Expand Down
6 changes: 4 additions & 2 deletions samples/MvvmSampleUwp/Views/MessengerRequestPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:mvvmSampleUwp="using:MvvmSampleUwp"
NavigationCacheMode="Enabled"
mc:Ignorable="d">
mc:Ignorable="d"
mvvmSampleUwp:ViewModelLocator.InitViewModel="True">
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Loaded">
<core:InvokeCommandAction Command="{x:Bind ViewModel.LoadDocsCommand}" CommandParameter="Messenger" />
Expand Down
3 changes: 0 additions & 3 deletions samples/MvvmSampleUwp/Views/MessengerRequestPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using CommunityToolkit.Mvvm.DependencyInjection;
using MvvmSample.Core.ViewModels;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
Expand All @@ -17,8 +16,6 @@ public sealed partial class MessengerRequestPage : Page
public MessengerRequestPage()
{
this.InitializeComponent();

DataContext = Ioc.Default.GetRequiredService<MessengerPageViewModel>();
}

public MessengerPageViewModel ViewModel => (MessengerPageViewModel)DataContext;
Expand Down
6 changes: 4 additions & 2 deletions samples/MvvmSampleUwp/Views/MessengerSendPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:mvvmSampleUwp="using:MvvmSampleUwp"
NavigationCacheMode="Enabled"
mc:Ignorable="d">
mc:Ignorable="d"
mvvmSampleUwp:ViewModelLocator.InitViewModel="True">
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Loaded">
<core:InvokeCommandAction Command="{x:Bind ViewModel.LoadDocsCommand}" CommandParameter="Messenger" />
Expand Down
3 changes: 0 additions & 3 deletions samples/MvvmSampleUwp/Views/MessengerSendPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using CommunityToolkit.Mvvm.DependencyInjection;
using MvvmSample.Core.ViewModels;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
Expand All @@ -17,8 +16,6 @@ public sealed partial class MessengerSendPage : Page
public MessengerSendPage()
{
this.InitializeComponent();

DataContext = Ioc.Default.GetRequiredService<MessengerPageViewModel>();
}

public MessengerPageViewModel ViewModel => (MessengerPageViewModel)DataContext;
Expand Down
6 changes: 4 additions & 2 deletions samples/MvvmSampleUwp/Views/ObservableObjectPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:mvvmSampleUwp="using:MvvmSampleUwp"
NavigationCacheMode="Enabled"
mc:Ignorable="d">
mc:Ignorable="d"
mvvmSampleUwp:ViewModelLocator.InitViewModel="True">
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Loaded">
<core:InvokeCommandAction Command="{x:Bind ViewModel.LoadDocsCommand}" CommandParameter="ObservableObject" />
Expand Down
3 changes: 0 additions & 3 deletions samples/MvvmSampleUwp/Views/ObservableObjectPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using CommunityToolkit.Mvvm.DependencyInjection;
using MvvmSample.Core.ViewModels;
using Windows.UI.Xaml.Controls;

Expand All @@ -16,8 +15,6 @@ public sealed partial class ObservableObjectPage : Page
public ObservableObjectPage()
{
this.InitializeComponent();

DataContext = Ioc.Default.GetRequiredService<ObservableObjectPageViewModel>();
}

public ObservableObjectPageViewModel ViewModel => (ObservableObjectPageViewModel)DataContext;
Expand Down
6 changes: 4 additions & 2 deletions samples/MvvmSampleUwp/Views/ObservableValidatorPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:widgets="using:MvvmSampleUwp.Views.Widgets"
xmlns:widgets="using:MvvmSampleUwp.Views.Widgets"
xmlns:mvvmSampleUwp="using:MvvmSampleUwp"
NavigationCacheMode="Enabled"
mc:Ignorable="d">
mc:Ignorable="d"
mvvmSampleUwp:ViewModelLocator.InitViewModel="True">
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Loaded">
<core:InvokeCommandAction Command="{x:Bind ViewModel.LoadDocsCommand}" CommandParameter="ObservableValidator" />
Expand Down
3 changes: 0 additions & 3 deletions samples/MvvmSampleUwp/Views/ObservableValidatorPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using CommunityToolkit.Mvvm.DependencyInjection;
using MvvmSample.Core.ViewModels;
using Windows.UI.Xaml.Controls;

Expand All @@ -16,8 +15,6 @@ public sealed partial class ObservableValidatorPage : Page
public ObservableValidatorPage()
{
this.InitializeComponent();

DataContext = Ioc.Default.GetRequiredService<ObservableValidatorPageViewModel>();
}

public ObservableValidatorPageViewModel ViewModel => (ObservableValidatorPageViewModel)DataContext;
Expand Down
6 changes: 4 additions & 2 deletions samples/MvvmSampleUwp/Views/PuttingThingsTogetherPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:mvvmSampleUwp="using:MvvmSampleUwp"
NavigationCacheMode="Enabled"
mc:Ignorable="d">
mc:Ignorable="d"
mvvmSampleUwp:ViewModelLocator.InitViewModel="True">
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Loaded">
<core:InvokeCommandAction Command="{x:Bind ViewModel.LoadDocsCommand}" CommandParameter="PuttingThingsTogether" />
Expand Down
3 changes: 0 additions & 3 deletions samples/MvvmSampleUwp/Views/PuttingThingsTogetherPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using CommunityToolkit.Mvvm.DependencyInjection;
using MvvmSample.Core.ViewModels;
using Windows.UI.Xaml.Controls;

Expand All @@ -16,8 +15,6 @@ public sealed partial class PuttingThingsTogetherPage : Page
public PuttingThingsTogetherPage()
{
this.InitializeComponent();

DataContext = Ioc.Default.GetRequiredService<SamplePageViewModel>();
}

public SamplePageViewModel ViewModel => (SamplePageViewModel)DataContext;
Expand Down
Loading