From e7f108af6a2e8cf7b3145b76be48c567ed6c5137 Mon Sep 17 00:00:00 2001 From: DenisT Date: Sat, 12 Mar 2022 10:02:48 +0700 Subject: [PATCH 1/2] ViewModelLocator --- samples/MvvmSampleUwp/App.xaml.cs | 27 +- samples/MvvmSampleUwp/MvvmSampleUwp.csproj | 843 +++++++++--------- samples/MvvmSampleUwp/ViewModelLocator.cs | 34 + .../Views/AsyncRelayCommandPage.xaml | 6 +- .../Views/AsyncRelayCommandPage.xaml.cs | 3 - .../Views/BuildingTheUIPage.xaml | 6 +- .../Views/BuildingTheUIPage.xaml.cs | 3 - .../MvvmSampleUwp/Views/IntroductionPage.xaml | 6 +- .../Views/IntroductionPage.xaml.cs | 5 +- samples/MvvmSampleUwp/Views/IocPage.xaml | 6 +- samples/MvvmSampleUwp/Views/IocPage.xaml.cs | 3 - .../MvvmSampleUwp/Views/MessengerPage.xaml | 6 +- .../MvvmSampleUwp/Views/MessengerPage.xaml.cs | 3 - .../Views/MessengerRequestPage.xaml | 6 +- .../Views/MessengerRequestPage.xaml.cs | 3 - .../Views/MessengerSendPage.xaml | 6 +- .../Views/MessengerSendPage.xaml.cs | 3 - .../Views/ObservableObjectPage.xaml | 6 +- .../Views/ObservableObjectPage.xaml.cs | 3 - .../Views/ObservableValidatorPage.xaml | 6 +- .../Views/ObservableValidatorPage.xaml.cs | 3 - .../Views/PuttingThingsTogetherPage.xaml | 6 +- .../Views/PuttingThingsTogetherPage.xaml.cs | 3 - .../Views/RedditBrowserPage.xaml | 2 +- .../Views/RedditServicePage.xaml | 6 +- .../Views/RedditServicePage.xaml.cs | 3 - .../MvvmSampleUwp/Views/RelayCommandPage.xaml | 6 +- .../Views/RelayCommandPage.xaml.cs | 3 - .../Views/SettingUpTheViewModelsPage.xaml | 6 +- .../Views/SettingUpTheViewModelsPage.xaml.cs | 3 - .../Views/SettingsServicePage.xaml | 6 +- .../Views/SettingsServicePage.xaml.cs | 3 - .../Views/Widgets/PostWidget.xaml | 6 +- .../Views/Widgets/PostWidget.xaml.cs | 3 - .../Views/Widgets/SubredditWidget.xaml | 6 +- .../Views/Widgets/SubredditWidget.xaml.cs | 3 - .../Views/Widgets/ValidationFormWidget.xaml | 6 +- .../Widgets/ValidationFormWidget.xaml.cs | 3 - samples/MvvmSampleXF/MvvmSampleXF/App.xaml.cs | 32 +- .../MvvmSampleXF/MvvmSampleXF/AppShell.xaml | 2 +- .../MvvmSampleXF/MvvmSampleXF.csproj | 4 +- .../MvvmSampleXF/ViewModelLocator.cs | 34 + .../Views/AsyncRelayCommandPage.xaml | 4 +- .../Views/AsyncRelayCommandPage.xaml.cs | 3 - .../MvvmSampleXF/Views/BuildingTheUIPage.xaml | 4 +- .../Views/BuildingTheUIPage.xaml.cs | 3 - .../MvvmSampleXF/Views/IntroductionPage.xaml | 4 +- .../Views/IntroductionPage.xaml.cs | 5 +- .../MvvmSampleXF/Views/IoCPage.xaml | 6 +- .../MvvmSampleXF/Views/IoCPage.xaml.cs | 7 +- .../MvvmSampleXF/Views/MessengerPage.xaml | 4 +- .../MvvmSampleXF/Views/MessengerPage.xaml.cs | 3 - .../Views/MessengerRequestPage.xaml | 4 +- .../Views/MessengerRequestPage.xaml.cs | 3 - .../MvvmSampleXF/Views/MessengerSendPage.xaml | 4 +- .../Views/MessengerSendPage.xaml.cs | 3 - .../Views/ObservableObjectPage.xaml | 4 +- .../Views/ObservableObjectPage.xaml.cs | 3 - .../Views/PuttingThingsTogetherPage.xaml | 4 +- .../Views/PuttingThingsTogetherPage.xaml.cs | 3 - .../MvvmSampleXF/Views/RedditServicePage.xaml | 4 +- .../Views/RedditServicePage.xaml.cs | 3 - .../MvvmSampleXF/Views/RelayCommandPage.xaml | 4 +- .../Views/RelayCommandPage.xaml.cs | 3 - .../Views/SettingUpTheViewModelsPage.xaml | 4 +- .../Views/SettingUpTheViewModelsPage.xaml.cs | 3 - .../Views/SettingsServicePage.xaml | 4 +- .../Views/SettingsServicePage.xaml.cs | 3 - .../Views/Widgets/PostWidget.xaml | 4 +- .../Views/Widgets/PostWidget.xaml.cs | 3 - .../Views/Widgets/SubredditWidget.xaml | 4 +- .../Views/Widgets/SubredditWidget.xaml.cs | 3 - 72 files changed, 661 insertions(+), 585 deletions(-) create mode 100644 samples/MvvmSampleUwp/ViewModelLocator.cs create mode 100644 samples/MvvmSampleXF/MvvmSampleXF/ViewModelLocator.cs diff --git a/samples/MvvmSampleUwp/App.xaml.cs b/samples/MvvmSampleUwp/App.xaml.cs index 2875e3a..ad2a1c9 100644 --- a/samples/MvvmSampleUwp/App.xaml.cs +++ b/samples/MvvmSampleUwp/App.xaml.cs @@ -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; @@ -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() .AddSingleton() @@ -60,7 +59,27 @@ protected override void OnLaunched(LaunchActivatedEventArgs e) .AddTransient() .AddTransient() .AddTransient() - .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 diff --git a/samples/MvvmSampleUwp/MvvmSampleUwp.csproj b/samples/MvvmSampleUwp/MvvmSampleUwp.csproj index 1ee3ba2..d551e03 100644 --- a/samples/MvvmSampleUwp/MvvmSampleUwp.csproj +++ b/samples/MvvmSampleUwp/MvvmSampleUwp.csproj @@ -1,428 +1,429 @@ - - - - - Debug - x86 - {E667FBF4-30F2-49AC-8576-7A2B867F8F1A} - AppContainerExe - Properties - MvvmSampleUwp - MvvmSampleUwp - en-US - UAP - 10.0.22000.0 - 10.0.17763.0 - 14 - 512 - {A5A43C5B-DE2A-4C0C-9213-0A381AF9435A};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} - true - false - - - true - bin\x86\Debug\ - DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP - ;2008 - full - x86 - false - prompt - true - - - bin\x86\Release\ - TRACE;NETFX_CORE;WINDOWS_UWP - true - ;2008 - pdbonly - x86 - false - prompt - true - true - - - true - bin\ARM\Debug\ - DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP - ;2008 - full - ARM - false - prompt - true - - - bin\ARM\Release\ - TRACE;NETFX_CORE;WINDOWS_UWP - true - ;2008 - pdbonly - ARM - false - prompt - true - true - - - true - bin\ARM64\Debug\ - DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP - ;2008 - full - ARM64 - false - prompt - true - true - - - bin\ARM64\Release\ - TRACE;NETFX_CORE;WINDOWS_UWP - true - ;2008 - pdbonly - ARM64 - false - prompt - true - true - - - true - bin\x64\Debug\ - DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP - ;2008 - full - x64 - false - prompt - true - - - bin\x64\Release\ - TRACE;NETFX_CORE;WINDOWS_UWP - true - ;2008 - pdbonly - x64 - false - prompt - true - true - - - PackageReference - - - - App.xaml - - - ValidationTextBox.xaml - - - DocumentationBlock.xaml - - - InteractiveSample.xaml - - - - - - - - - Shell.xaml - - - AsyncRelayCommandPage.xaml - - - BuildingTheUIPage.xaml - - - ObservableValidatorPage.xaml - - - RedditServicePage.xaml - - - SettingsServicePage.xaml - - - SettingUpTheViewModelsPage.xaml - - - PuttingThingsTogetherPage.xaml - - - MessengerRequestPage.xaml - - - MessengerSendPage.xaml - - - MessengerPage.xaml - - - IocPage.xaml - - - IntroductionPage.xaml - - - RedditBrowserPage.xaml - - - RelayCommandPage.xaml - - - ObservableObjectPage.xaml - - - PostWidget.xaml - - - SubredditWidget.xaml - - - ValidationFormWidget.xaml - - - - - Designer - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - MSBuild:Compile - Designer - - - - - 6.0.0 - - - 6.2.13 - - - 7.1.2 - - - 7.1.2 - - - 7.1.2 - - - 2.8.0-prerelease.220118001 - - - 2.0.1 - - - - - MSBuild:Compile - Designer - - - MSBuild:Compile - Designer - - - Designer - MSBuild:Compile - - - Designer - MSBuild:Compile - - - MSBuild:Compile - Designer - - - MSBuild:Compile - Designer - - - MSBuild:Compile - Designer - - - MSBuild:Compile - Designer - - - MSBuild:Compile - Designer - - - MSBuild:Compile - Designer - - - MSBuild:Compile - Designer - - - MSBuild:Compile - Designer - - - MSBuild:Compile - Designer - - - MSBuild:Compile - Designer - - - MSBuild:Compile - Designer - - - MSBuild:Compile - Designer - - - Designer - MSBuild:Compile - - - MSBuild:Compile - Designer - - - Designer - MSBuild:Compile - - - MSBuild:Compile - Designer - - - Designer - MSBuild:Compile - - - Designer - MSBuild:Compile - - - - - Assets\docs\AsyncRelayCommand.md - PreserveNewest - - - Assets\docs\Introduction.md - PreserveNewest - - - Assets\docs\Ioc.md - PreserveNewest - - - Assets\docs\Messenger.md - PreserveNewest - - - Assets\docs\ObservableObject.md - PreserveNewest - - - Assets\docs\ObservableValidator.md - PreserveNewest - - - Assets\docs\ObservableRecipient.md - PreserveNewest - - - Assets\docs\PuttingThingsTogether.md - PreserveNewest - - - Assets\docs\RelayCommand.md - PreserveNewest - - - - - {28e524f7-61b3-455d-8cbd-615bc4fde814} - MvvmSample.Core - - - - 14.0 - - + + + + + Debug + x86 + {E667FBF4-30F2-49AC-8576-7A2B867F8F1A} + AppContainerExe + Properties + MvvmSampleUwp + MvvmSampleUwp + en-US + UAP + 10.0.22000.0 + 10.0.17763.0 + 14 + 512 + {A5A43C5B-DE2A-4C0C-9213-0A381AF9435A};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + true + false + + + true + bin\x86\Debug\ + DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP + ;2008 + full + x86 + false + prompt + true + + + bin\x86\Release\ + TRACE;NETFX_CORE;WINDOWS_UWP + true + ;2008 + pdbonly + x86 + false + prompt + true + true + + + true + bin\ARM\Debug\ + DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP + ;2008 + full + ARM + false + prompt + true + + + bin\ARM\Release\ + TRACE;NETFX_CORE;WINDOWS_UWP + true + ;2008 + pdbonly + ARM + false + prompt + true + true + + + true + bin\ARM64\Debug\ + DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP + ;2008 + full + ARM64 + false + prompt + true + true + + + bin\ARM64\Release\ + TRACE;NETFX_CORE;WINDOWS_UWP + true + ;2008 + pdbonly + ARM64 + false + prompt + true + true + + + true + bin\x64\Debug\ + DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP + ;2008 + full + x64 + false + prompt + true + + + bin\x64\Release\ + TRACE;NETFX_CORE;WINDOWS_UWP + true + ;2008 + pdbonly + x64 + false + prompt + true + true + + + PackageReference + + + + App.xaml + + + ValidationTextBox.xaml + + + DocumentationBlock.xaml + + + InteractiveSample.xaml + + + + + + + + + Shell.xaml + + + + AsyncRelayCommandPage.xaml + + + BuildingTheUIPage.xaml + + + ObservableValidatorPage.xaml + + + RedditServicePage.xaml + + + SettingsServicePage.xaml + + + SettingUpTheViewModelsPage.xaml + + + PuttingThingsTogetherPage.xaml + + + MessengerRequestPage.xaml + + + MessengerSendPage.xaml + + + MessengerPage.xaml + + + IocPage.xaml + + + IntroductionPage.xaml + + + RedditBrowserPage.xaml + + + RelayCommandPage.xaml + + + ObservableObjectPage.xaml + + + PostWidget.xaml + + + SubredditWidget.xaml + + + ValidationFormWidget.xaml + + + + + Designer + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + MSBuild:Compile + Designer + + + + + 6.0.0 + + + 6.2.13 + + + 7.1.2 + + + 7.1.2 + + + 7.1.2 + + + 2.8.0-prerelease.220118001 + + + 2.0.1 + + + + + MSBuild:Compile + Designer + + + MSBuild:Compile + Designer + + + Designer + MSBuild:Compile + + + Designer + MSBuild:Compile + + + MSBuild:Compile + Designer + + + MSBuild:Compile + Designer + + + MSBuild:Compile + Designer + + + MSBuild:Compile + Designer + + + MSBuild:Compile + Designer + + + MSBuild:Compile + Designer + + + MSBuild:Compile + Designer + + + MSBuild:Compile + Designer + + + MSBuild:Compile + Designer + + + MSBuild:Compile + Designer + + + MSBuild:Compile + Designer + + + MSBuild:Compile + Designer + + + Designer + MSBuild:Compile + + + MSBuild:Compile + Designer + + + Designer + MSBuild:Compile + + + MSBuild:Compile + Designer + + + Designer + MSBuild:Compile + + + Designer + MSBuild:Compile + + + + + Assets\docs\AsyncRelayCommand.md + PreserveNewest + + + Assets\docs\Introduction.md + PreserveNewest + + + Assets\docs\Ioc.md + PreserveNewest + + + Assets\docs\Messenger.md + PreserveNewest + + + Assets\docs\ObservableObject.md + PreserveNewest + + + Assets\docs\ObservableValidator.md + PreserveNewest + + + Assets\docs\ObservableRecipient.md + PreserveNewest + + + Assets\docs\PuttingThingsTogether.md + PreserveNewest + + + Assets\docs\RelayCommand.md + PreserveNewest + + + + + {28e524f7-61b3-455d-8cbd-615bc4fde814} + MvvmSample.Core + + + + 14.0 + + + --> \ No newline at end of file diff --git a/samples/MvvmSampleUwp/ViewModelLocator.cs b/samples/MvvmSampleUwp/ViewModelLocator.cs new file mode 100644 index 0000000..ef82e3b --- /dev/null +++ b/samples/MvvmSampleUwp/ViewModelLocator.cs @@ -0,0 +1,34 @@ +using System; +using Windows.UI.Xaml; + +namespace MvvmSampleUwp; +public static class ViewModelLocator +{ + private static Func? _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 factory) + => _viewModelFactory = factory; +} diff --git a/samples/MvvmSampleUwp/Views/AsyncRelayCommandPage.xaml b/samples/MvvmSampleUwp/Views/AsyncRelayCommandPage.xaml index fb4df63..6169bc5 100644 --- a/samples/MvvmSampleUwp/Views/AsyncRelayCommandPage.xaml +++ b/samples/MvvmSampleUwp/Views/AsyncRelayCommandPage.xaml @@ -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"> diff --git a/samples/MvvmSampleUwp/Views/AsyncRelayCommandPage.xaml.cs b/samples/MvvmSampleUwp/Views/AsyncRelayCommandPage.xaml.cs index 023ac53..e51efd8 100644 --- a/samples/MvvmSampleUwp/Views/AsyncRelayCommandPage.xaml.cs +++ b/samples/MvvmSampleUwp/Views/AsyncRelayCommandPage.xaml.cs @@ -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; @@ -16,8 +15,6 @@ public sealed partial class AsyncRelayCommandPage : Page public AsyncRelayCommandPage() { this.InitializeComponent(); - - DataContext = Ioc.Default.GetRequiredService(); } public AsyncRelayCommandPageViewModel ViewModel => (AsyncRelayCommandPageViewModel)DataContext; diff --git a/samples/MvvmSampleUwp/Views/BuildingTheUIPage.xaml b/samples/MvvmSampleUwp/Views/BuildingTheUIPage.xaml index 81a9931..73a7cc1 100644 --- a/samples/MvvmSampleUwp/Views/BuildingTheUIPage.xaml +++ b/samples/MvvmSampleUwp/Views/BuildingTheUIPage.xaml @@ -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"> diff --git a/samples/MvvmSampleUwp/Views/BuildingTheUIPage.xaml.cs b/samples/MvvmSampleUwp/Views/BuildingTheUIPage.xaml.cs index 077d3f7..253335b 100644 --- a/samples/MvvmSampleUwp/Views/BuildingTheUIPage.xaml.cs +++ b/samples/MvvmSampleUwp/Views/BuildingTheUIPage.xaml.cs @@ -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; @@ -16,8 +15,6 @@ public sealed partial class BuildingTheUIPage : Page public BuildingTheUIPage() { this.InitializeComponent(); - - DataContext = Ioc.Default.GetRequiredService(); } public SamplePageViewModel ViewModel => (SamplePageViewModel)DataContext; diff --git a/samples/MvvmSampleUwp/Views/IntroductionPage.xaml b/samples/MvvmSampleUwp/Views/IntroductionPage.xaml index b612fbe..9c8db05 100644 --- a/samples/MvvmSampleUwp/Views/IntroductionPage.xaml +++ b/samples/MvvmSampleUwp/Views/IntroductionPage.xaml @@ -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"> diff --git a/samples/MvvmSampleUwp/Views/IntroductionPage.xaml.cs b/samples/MvvmSampleUwp/Views/IntroductionPage.xaml.cs index dcd5e06..4fe231e 100644 --- a/samples/MvvmSampleUwp/Views/IntroductionPage.xaml.cs +++ b/samples/MvvmSampleUwp/Views/IntroductionPage.xaml.cs @@ -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; @@ -16,9 +15,7 @@ public sealed partial class IntroductionPage : Page public IntroductionPage() { this.InitializeComponent(); - - DataContext = Ioc.Default.GetRequiredService(); } - public ObservableObjectPageViewModel ViewModel => (ObservableObjectPageViewModel)DataContext; + public SamplePageViewModel ViewModel => (SamplePageViewModel)DataContext; } diff --git a/samples/MvvmSampleUwp/Views/IocPage.xaml b/samples/MvvmSampleUwp/Views/IocPage.xaml index 7000492..bd289a5 100644 --- a/samples/MvvmSampleUwp/Views/IocPage.xaml +++ b/samples/MvvmSampleUwp/Views/IocPage.xaml @@ -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"> diff --git a/samples/MvvmSampleUwp/Views/IocPage.xaml.cs b/samples/MvvmSampleUwp/Views/IocPage.xaml.cs index 2bfa56b..c05e113 100644 --- a/samples/MvvmSampleUwp/Views/IocPage.xaml.cs +++ b/samples/MvvmSampleUwp/Views/IocPage.xaml.cs @@ -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; @@ -16,8 +15,6 @@ public sealed partial class IocPage : Page public IocPage() { this.InitializeComponent(); - - DataContext = Ioc.Default.GetRequiredService(); } public IocPageViewModel ViewModel => (IocPageViewModel)DataContext; diff --git a/samples/MvvmSampleUwp/Views/MessengerPage.xaml b/samples/MvvmSampleUwp/Views/MessengerPage.xaml index 567c321..2323bfb 100644 --- a/samples/MvvmSampleUwp/Views/MessengerPage.xaml +++ b/samples/MvvmSampleUwp/Views/MessengerPage.xaml @@ -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"> diff --git a/samples/MvvmSampleUwp/Views/MessengerPage.xaml.cs b/samples/MvvmSampleUwp/Views/MessengerPage.xaml.cs index 8a041eb..67f4ec5 100644 --- a/samples/MvvmSampleUwp/Views/MessengerPage.xaml.cs +++ b/samples/MvvmSampleUwp/Views/MessengerPage.xaml.cs @@ -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; @@ -16,8 +15,6 @@ public sealed partial class MessengerPage : Page public MessengerPage() { this.InitializeComponent(); - - DataContext = Ioc.Default.GetRequiredService(); } public MessengerPageViewModel ViewModel => (MessengerPageViewModel)DataContext; diff --git a/samples/MvvmSampleUwp/Views/MessengerRequestPage.xaml b/samples/MvvmSampleUwp/Views/MessengerRequestPage.xaml index ea77300..57605f1 100644 --- a/samples/MvvmSampleUwp/Views/MessengerRequestPage.xaml +++ b/samples/MvvmSampleUwp/Views/MessengerRequestPage.xaml @@ -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"> diff --git a/samples/MvvmSampleUwp/Views/MessengerRequestPage.xaml.cs b/samples/MvvmSampleUwp/Views/MessengerRequestPage.xaml.cs index 6382621..3e47a62 100644 --- a/samples/MvvmSampleUwp/Views/MessengerRequestPage.xaml.cs +++ b/samples/MvvmSampleUwp/Views/MessengerRequestPage.xaml.cs @@ -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; @@ -17,8 +16,6 @@ public sealed partial class MessengerRequestPage : Page public MessengerRequestPage() { this.InitializeComponent(); - - DataContext = Ioc.Default.GetRequiredService(); } public MessengerPageViewModel ViewModel => (MessengerPageViewModel)DataContext; diff --git a/samples/MvvmSampleUwp/Views/MessengerSendPage.xaml b/samples/MvvmSampleUwp/Views/MessengerSendPage.xaml index dc3cab8..b46de5a 100644 --- a/samples/MvvmSampleUwp/Views/MessengerSendPage.xaml +++ b/samples/MvvmSampleUwp/Views/MessengerSendPage.xaml @@ -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"> diff --git a/samples/MvvmSampleUwp/Views/MessengerSendPage.xaml.cs b/samples/MvvmSampleUwp/Views/MessengerSendPage.xaml.cs index 2e5c3be..4cac0ce 100644 --- a/samples/MvvmSampleUwp/Views/MessengerSendPage.xaml.cs +++ b/samples/MvvmSampleUwp/Views/MessengerSendPage.xaml.cs @@ -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; @@ -17,8 +16,6 @@ public sealed partial class MessengerSendPage : Page public MessengerSendPage() { this.InitializeComponent(); - - DataContext = Ioc.Default.GetRequiredService(); } public MessengerPageViewModel ViewModel => (MessengerPageViewModel)DataContext; diff --git a/samples/MvvmSampleUwp/Views/ObservableObjectPage.xaml b/samples/MvvmSampleUwp/Views/ObservableObjectPage.xaml index 8831893..41ffc75 100644 --- a/samples/MvvmSampleUwp/Views/ObservableObjectPage.xaml +++ b/samples/MvvmSampleUwp/Views/ObservableObjectPage.xaml @@ -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"> diff --git a/samples/MvvmSampleUwp/Views/ObservableObjectPage.xaml.cs b/samples/MvvmSampleUwp/Views/ObservableObjectPage.xaml.cs index 7985c75..0edef3b 100644 --- a/samples/MvvmSampleUwp/Views/ObservableObjectPage.xaml.cs +++ b/samples/MvvmSampleUwp/Views/ObservableObjectPage.xaml.cs @@ -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; @@ -16,8 +15,6 @@ public sealed partial class ObservableObjectPage : Page public ObservableObjectPage() { this.InitializeComponent(); - - DataContext = Ioc.Default.GetRequiredService(); } public ObservableObjectPageViewModel ViewModel => (ObservableObjectPageViewModel)DataContext; diff --git a/samples/MvvmSampleUwp/Views/ObservableValidatorPage.xaml b/samples/MvvmSampleUwp/Views/ObservableValidatorPage.xaml index 1e3691b..03f366e 100644 --- a/samples/MvvmSampleUwp/Views/ObservableValidatorPage.xaml +++ b/samples/MvvmSampleUwp/Views/ObservableValidatorPage.xaml @@ -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"> diff --git a/samples/MvvmSampleUwp/Views/ObservableValidatorPage.xaml.cs b/samples/MvvmSampleUwp/Views/ObservableValidatorPage.xaml.cs index 4413b4a..62b3342 100644 --- a/samples/MvvmSampleUwp/Views/ObservableValidatorPage.xaml.cs +++ b/samples/MvvmSampleUwp/Views/ObservableValidatorPage.xaml.cs @@ -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; @@ -16,8 +15,6 @@ public sealed partial class ObservableValidatorPage : Page public ObservableValidatorPage() { this.InitializeComponent(); - - DataContext = Ioc.Default.GetRequiredService(); } public ObservableValidatorPageViewModel ViewModel => (ObservableValidatorPageViewModel)DataContext; diff --git a/samples/MvvmSampleUwp/Views/PuttingThingsTogetherPage.xaml b/samples/MvvmSampleUwp/Views/PuttingThingsTogetherPage.xaml index c246b98..d761f50 100644 --- a/samples/MvvmSampleUwp/Views/PuttingThingsTogetherPage.xaml +++ b/samples/MvvmSampleUwp/Views/PuttingThingsTogetherPage.xaml @@ -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"> diff --git a/samples/MvvmSampleUwp/Views/PuttingThingsTogetherPage.xaml.cs b/samples/MvvmSampleUwp/Views/PuttingThingsTogetherPage.xaml.cs index a04789d..3d0967a 100644 --- a/samples/MvvmSampleUwp/Views/PuttingThingsTogetherPage.xaml.cs +++ b/samples/MvvmSampleUwp/Views/PuttingThingsTogetherPage.xaml.cs @@ -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; @@ -16,8 +15,6 @@ public sealed partial class PuttingThingsTogetherPage : Page public PuttingThingsTogetherPage() { this.InitializeComponent(); - - DataContext = Ioc.Default.GetRequiredService(); } public SamplePageViewModel ViewModel => (SamplePageViewModel)DataContext; diff --git a/samples/MvvmSampleUwp/Views/RedditBrowserPage.xaml b/samples/MvvmSampleUwp/Views/RedditBrowserPage.xaml index ae65923..be22633 100644 --- a/samples/MvvmSampleUwp/Views/RedditBrowserPage.xaml +++ b/samples/MvvmSampleUwp/Views/RedditBrowserPage.xaml @@ -5,7 +5,7 @@ xmlns:controls="using:MvvmSampleUwp.Controls" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" - xmlns:widgets="using:MvvmSampleUwp.Views.Widgets" + xmlns:widgets="using:MvvmSampleUwp.Views.Widgets" mc:Ignorable="d"> diff --git a/samples/MvvmSampleUwp/Views/RedditServicePage.xaml b/samples/MvvmSampleUwp/Views/RedditServicePage.xaml index 9771fc2..a31d8d7 100644 --- a/samples/MvvmSampleUwp/Views/RedditServicePage.xaml +++ b/samples/MvvmSampleUwp/Views/RedditServicePage.xaml @@ -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"> diff --git a/samples/MvvmSampleUwp/Views/RedditServicePage.xaml.cs b/samples/MvvmSampleUwp/Views/RedditServicePage.xaml.cs index d9c9252..6ba48e1 100644 --- a/samples/MvvmSampleUwp/Views/RedditServicePage.xaml.cs +++ b/samples/MvvmSampleUwp/Views/RedditServicePage.xaml.cs @@ -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; @@ -16,8 +15,6 @@ public sealed partial class RedditServicePage : Page public RedditServicePage() { this.InitializeComponent(); - - DataContext = Ioc.Default.GetRequiredService(); } public SamplePageViewModel ViewModel => (SamplePageViewModel)DataContext; diff --git a/samples/MvvmSampleUwp/Views/RelayCommandPage.xaml b/samples/MvvmSampleUwp/Views/RelayCommandPage.xaml index 0154d36..e554b86 100644 --- a/samples/MvvmSampleUwp/Views/RelayCommandPage.xaml +++ b/samples/MvvmSampleUwp/Views/RelayCommandPage.xaml @@ -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"> diff --git a/samples/MvvmSampleUwp/Views/RelayCommandPage.xaml.cs b/samples/MvvmSampleUwp/Views/RelayCommandPage.xaml.cs index b1f7e98..6d4f51b 100644 --- a/samples/MvvmSampleUwp/Views/RelayCommandPage.xaml.cs +++ b/samples/MvvmSampleUwp/Views/RelayCommandPage.xaml.cs @@ -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; @@ -16,8 +15,6 @@ public sealed partial class RelayCommandPage : Page public RelayCommandPage() { this.InitializeComponent(); - - DataContext = Ioc.Default.GetRequiredService(); } public RelayCommandPageViewModel ViewModel => (RelayCommandPageViewModel)DataContext; diff --git a/samples/MvvmSampleUwp/Views/SettingUpTheViewModelsPage.xaml b/samples/MvvmSampleUwp/Views/SettingUpTheViewModelsPage.xaml index 2f08e00..5d1319a 100644 --- a/samples/MvvmSampleUwp/Views/SettingUpTheViewModelsPage.xaml +++ b/samples/MvvmSampleUwp/Views/SettingUpTheViewModelsPage.xaml @@ -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"> diff --git a/samples/MvvmSampleUwp/Views/SettingUpTheViewModelsPage.xaml.cs b/samples/MvvmSampleUwp/Views/SettingUpTheViewModelsPage.xaml.cs index f34301f..eef03a4 100644 --- a/samples/MvvmSampleUwp/Views/SettingUpTheViewModelsPage.xaml.cs +++ b/samples/MvvmSampleUwp/Views/SettingUpTheViewModelsPage.xaml.cs @@ -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; @@ -16,8 +15,6 @@ public sealed partial class SettingUpTheViewModelsPage : Page public SettingUpTheViewModelsPage() { this.InitializeComponent(); - - DataContext = Ioc.Default.GetRequiredService(); } public SamplePageViewModel ViewModel => (SamplePageViewModel)DataContext; diff --git a/samples/MvvmSampleUwp/Views/SettingsServicePage.xaml b/samples/MvvmSampleUwp/Views/SettingsServicePage.xaml index 68cd490..9b11517 100644 --- a/samples/MvvmSampleUwp/Views/SettingsServicePage.xaml +++ b/samples/MvvmSampleUwp/Views/SettingsServicePage.xaml @@ -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"> diff --git a/samples/MvvmSampleUwp/Views/SettingsServicePage.xaml.cs b/samples/MvvmSampleUwp/Views/SettingsServicePage.xaml.cs index e523a18..59309e0 100644 --- a/samples/MvvmSampleUwp/Views/SettingsServicePage.xaml.cs +++ b/samples/MvvmSampleUwp/Views/SettingsServicePage.xaml.cs @@ -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; @@ -16,8 +15,6 @@ public sealed partial class SettingsServicePage : Page public SettingsServicePage() { this.InitializeComponent(); - - DataContext = Ioc.Default.GetRequiredService(); } public SamplePageViewModel ViewModel => (SamplePageViewModel)DataContext; diff --git a/samples/MvvmSampleUwp/Views/Widgets/PostWidget.xaml b/samples/MvvmSampleUwp/Views/Widgets/PostWidget.xaml index f8c5126..2f3f131 100644 --- a/samples/MvvmSampleUwp/Views/Widgets/PostWidget.xaml +++ b/samples/MvvmSampleUwp/Views/Widgets/PostWidget.xaml @@ -5,10 +5,12 @@ xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" - xmlns:ui="using:Microsoft.Toolkit.Uwp.UI" + xmlns:ui="using:Microsoft.Toolkit.Uwp.UI" + xmlns:mvvmSampleUwp="using:MvvmSampleUwp" d:DesignHeight="300" d:DesignWidth="400" - mc:Ignorable="d"> + mc:Ignorable="d" + mvvmSampleUwp:ViewModelLocator.InitViewModel="True"> (); - this.Loaded += (s, e) => ViewModel.IsActive = true; this.Unloaded += (s, e) => ViewModel.IsActive = false; } diff --git a/samples/MvvmSampleUwp/Views/Widgets/SubredditWidget.xaml b/samples/MvvmSampleUwp/Views/Widgets/SubredditWidget.xaml index 51143fb..65eb5ba 100644 --- a/samples/MvvmSampleUwp/Views/Widgets/SubredditWidget.xaml +++ b/samples/MvvmSampleUwp/Views/Widgets/SubredditWidget.xaml @@ -9,10 +9,12 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:models="using:MvvmSample.Core.Models" xmlns:muxc="using:Microsoft.UI.Xaml.Controls" - xmlns:ui="using:Microsoft.Toolkit.Uwp.UI" + xmlns:ui="using:Microsoft.Toolkit.Uwp.UI" + xmlns:mvvmSampleUwp="using:MvvmSampleUwp" d:DesignHeight="300" d:DesignWidth="400" - mc:Ignorable="d"> + mc:Ignorable="d" + mvvmSampleUwp:ViewModelLocator.InitViewModel="True"> (); } public SubredditWidgetViewModel ViewModel => (SubredditWidgetViewModel)DataContext; diff --git a/samples/MvvmSampleUwp/Views/Widgets/ValidationFormWidget.xaml b/samples/MvvmSampleUwp/Views/Widgets/ValidationFormWidget.xaml index feb08af..1c73d17 100644 --- a/samples/MvvmSampleUwp/Views/Widgets/ValidationFormWidget.xaml +++ b/samples/MvvmSampleUwp/Views/Widgets/ValidationFormWidget.xaml @@ -7,10 +7,12 @@ xmlns:interactions="using:Microsoft.Xaml.Interactions.Core" 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" d:DesignHeight="300" d:DesignWidth="400" - mc:Ignorable="d"> + mc:Ignorable="d" + mvvmSampleUwp:ViewModelLocator.InitViewModel="True"> diff --git a/samples/MvvmSampleUwp/Views/Widgets/ValidationFormWidget.xaml.cs b/samples/MvvmSampleUwp/Views/Widgets/ValidationFormWidget.xaml.cs index 10c9c69..39cfedf 100644 --- a/samples/MvvmSampleUwp/Views/Widgets/ValidationFormWidget.xaml.cs +++ b/samples/MvvmSampleUwp/Views/Widgets/ValidationFormWidget.xaml.cs @@ -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.Widgets; using Windows.UI.Xaml.Controls; @@ -13,8 +12,6 @@ public sealed partial class ValidationFormWidget : UserControl public ValidationFormWidget() { this.InitializeComponent(); - - DataContext = Ioc.Default.GetRequiredService(); } public ValidationFormWidgetViewModel ViewModel => (ValidationFormWidgetViewModel)DataContext; diff --git a/samples/MvvmSampleXF/MvvmSampleXF/App.xaml.cs b/samples/MvvmSampleXF/MvvmSampleXF/App.xaml.cs index e31a1a4..10234db 100644 --- a/samples/MvvmSampleXF/MvvmSampleXF/App.xaml.cs +++ b/samples/MvvmSampleXF/MvvmSampleXF/App.xaml.cs @@ -1,4 +1,5 @@ -using CommunityToolkit.Mvvm.DependencyInjection; +using System.Linq; +using CommunityToolkit.Mvvm.DependencyInjection; using Microsoft.Extensions.DependencyInjection; using MvvmSample.Core.Services; using MvvmSample.Core.ViewModels; @@ -21,13 +22,13 @@ public App() if (!_initialized) { _initialized = true; - Ioc.Default.ConfigureServices( - new ServiceCollection() + + var serviceProvider = new ServiceCollection() //Services .AddSingleton() .AddSingleton() - .AddSingleton(RestService.For("https://www.reddit.com/")) - //ViewModels + .AddSingleton(RestService.For("https://www.reddit.com/")) + //ViewModels .AddTransient() .AddTransient() .AddTransient() @@ -36,7 +37,26 @@ public App() .AddTransient() .AddTransient() .AddTransient() - .BuildServiceProvider()); + .BuildServiceProvider(); + + 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); + }); } MainPage = new AppShell(); diff --git a/samples/MvvmSampleXF/MvvmSampleXF/AppShell.xaml b/samples/MvvmSampleXF/MvvmSampleXF/AppShell.xaml index 471f96b..b377824 100644 --- a/samples/MvvmSampleXF/MvvmSampleXF/AppShell.xaml +++ b/samples/MvvmSampleXF/MvvmSampleXF/AppShell.xaml @@ -41,7 +41,7 @@ + ContentTemplate="{DataTemplate views:IocPage}" /> MSBuild:UpdateDesignTimeXaml - + MSBuild:UpdateDesignTimeXaml @@ -85,7 +85,7 @@ - + diff --git a/samples/MvvmSampleXF/MvvmSampleXF/ViewModelLocator.cs b/samples/MvvmSampleXF/MvvmSampleXF/ViewModelLocator.cs new file mode 100644 index 0000000..8194e3f --- /dev/null +++ b/samples/MvvmSampleXF/MvvmSampleXF/ViewModelLocator.cs @@ -0,0 +1,34 @@ +using System; +using Xamarin.Forms; + +namespace MvvmSampleXF; +public static class ViewModelLocator +{ + private static Func? _viewModelFactory; + + public static readonly BindableProperty InitViewModelProperty = + BindableProperty.CreateAttached("InitViewModel", typeof(bool?), typeof(ViewModelLocator), null, propertyChanged: OnInitViewModelChanged); + + public static bool? GetInitViewModel(BindableObject bindableObject) + { + return (bool?)bindableObject.GetValue(InitViewModelProperty); + } + + public static void SetInitViewModel(BindableObject bindableObject, bool? value) + { + bindableObject.SetValue(InitViewModelProperty, value); + } + + private static void OnInitViewModelChanged(BindableObject bindableObject, object oldValue, object newValue) + { + var needSet = (bool?)newValue; + if (needSet == true) + { + var viewModel = _viewModelFactory?.Invoke(bindableObject); + bindableObject.BindingContext = viewModel; + } + } + + public static void SetViewModelFactory(Func factory) + => _viewModelFactory = factory; +} \ No newline at end of file diff --git a/samples/MvvmSampleXF/MvvmSampleXF/Views/AsyncRelayCommandPage.xaml b/samples/MvvmSampleXF/MvvmSampleXF/Views/AsyncRelayCommandPage.xaml index d05fb61..d30af23 100644 --- a/samples/MvvmSampleXF/MvvmSampleXF/Views/AsyncRelayCommandPage.xaml +++ b/samples/MvvmSampleXF/MvvmSampleXF/Views/AsyncRelayCommandPage.xaml @@ -5,9 +5,11 @@ xmlns:md="clr-namespace:Xam.Forms.Markdown;assembly=Xam.Forms.MarkdownView" xmlns:controls="clr-namespace:MvvmSampleXF.Controls" xmlns:converters="clr-namespace:MvvmSampleXF.Converters" + xmlns:mvvmSampleXf="clr-namespace:MvvmSampleXF;assembly=MvvmSampleXF" x:Class="MvvmSampleXF.Views.AsyncRelayCommandPage" x:DataType="vm:AsyncRelayCommandPageViewModel" - Title="Commands"> + Title="Commands" + mvvmSampleXf:ViewModelLocator.InitViewModel="True"> diff --git a/samples/MvvmSampleXF/MvvmSampleXF/Views/AsyncRelayCommandPage.xaml.cs b/samples/MvvmSampleXF/MvvmSampleXF/Views/AsyncRelayCommandPage.xaml.cs index c8da44b..ab1b910 100644 --- a/samples/MvvmSampleXF/MvvmSampleXF/Views/AsyncRelayCommandPage.xaml.cs +++ b/samples/MvvmSampleXF/MvvmSampleXF/Views/AsyncRelayCommandPage.xaml.cs @@ -1,5 +1,4 @@ using MvvmSample.Core.ViewModels; -using CommunityToolkit.Mvvm.DependencyInjection; using Xamarin.Forms; using Xamarin.Forms.Xaml; @@ -12,8 +11,6 @@ public partial class AsyncRelayCommandPage : ContentPage public AsyncRelayCommandPage() { InitializeComponent(); - - BindingContext = Ioc.Default.GetRequiredService(); } public AsyncRelayCommandPageViewModel ViewModel => (AsyncRelayCommandPageViewModel)BindingContext; diff --git a/samples/MvvmSampleXF/MvvmSampleXF/Views/BuildingTheUIPage.xaml b/samples/MvvmSampleXF/MvvmSampleXF/Views/BuildingTheUIPage.xaml index 2347f1b..6d6e81a 100644 --- a/samples/MvvmSampleXF/MvvmSampleXF/Views/BuildingTheUIPage.xaml +++ b/samples/MvvmSampleXF/MvvmSampleXF/Views/BuildingTheUIPage.xaml @@ -3,9 +3,11 @@ xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:vm="clr-namespace:MvvmSample.Core.ViewModels;assembly=MvvmSample.Core" xmlns:md="clr-namespace:Xam.Forms.Markdown;assembly=Xam.Forms.MarkdownView" + xmlns:mvvmSampleXf="clr-namespace:MvvmSampleXF;assembly=MvvmSampleXF" x:Class="MvvmSampleXF.Views.BuildingTheUIPage" x:DataType="vm:SamplePageViewModel" - Title="Building the UI"> + Title="Building the UI" + mvvmSampleXf:ViewModelLocator.InitViewModel="True"> diff --git a/samples/MvvmSampleXF/MvvmSampleXF/Views/BuildingTheUIPage.xaml.cs b/samples/MvvmSampleXF/MvvmSampleXF/Views/BuildingTheUIPage.xaml.cs index dab2e48..eb9665c 100644 --- a/samples/MvvmSampleXF/MvvmSampleXF/Views/BuildingTheUIPage.xaml.cs +++ b/samples/MvvmSampleXF/MvvmSampleXF/Views/BuildingTheUIPage.xaml.cs @@ -1,5 +1,4 @@ using MvvmSample.Core.ViewModels; -using CommunityToolkit.Mvvm.DependencyInjection; using Xamarin.Forms; using Xamarin.Forms.Xaml; @@ -12,8 +11,6 @@ public partial class BuildingTheUIPage : ContentPage public BuildingTheUIPage() { InitializeComponent(); - - BindingContext = Ioc.Default.GetRequiredService(); } public SamplePageViewModel ViewModel => (SamplePageViewModel)BindingContext; diff --git a/samples/MvvmSampleXF/MvvmSampleXF/Views/IntroductionPage.xaml b/samples/MvvmSampleXF/MvvmSampleXF/Views/IntroductionPage.xaml index 8aaacbf..b2df629 100644 --- a/samples/MvvmSampleXF/MvvmSampleXF/Views/IntroductionPage.xaml +++ b/samples/MvvmSampleXF/MvvmSampleXF/Views/IntroductionPage.xaml @@ -3,9 +3,11 @@ xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:vm="clr-namespace:MvvmSample.Core.ViewModels;assembly=MvvmSample.Core" xmlns:md="clr-namespace:Xam.Forms.Markdown;assembly=Xam.Forms.MarkdownView" + xmlns:mvvmSampleXf="clr-namespace:MvvmSampleXF;assembly=MvvmSampleXF" x:Class="MvvmSampleXF.Views.IntroductionPage" x:DataType="vm:ObservableObjectPageViewModel" - Title="Introduction"> + Title="Introduction" + mvvmSampleXf:ViewModelLocator.InitViewModel="True"> diff --git a/samples/MvvmSampleXF/MvvmSampleXF/Views/IntroductionPage.xaml.cs b/samples/MvvmSampleXF/MvvmSampleXF/Views/IntroductionPage.xaml.cs index 53b2399..b692cac 100644 --- a/samples/MvvmSampleXF/MvvmSampleXF/Views/IntroductionPage.xaml.cs +++ b/samples/MvvmSampleXF/MvvmSampleXF/Views/IntroductionPage.xaml.cs @@ -1,5 +1,4 @@ using MvvmSample.Core.ViewModels; -using CommunityToolkit.Mvvm.DependencyInjection; using Xamarin.Forms; using Xamarin.Forms.Xaml; @@ -12,11 +11,9 @@ public partial class IntroductionPage : ContentPage public IntroductionPage() { InitializeComponent(); - - BindingContext = Ioc.Default.GetRequiredService(); } - public ObservableObjectPageViewModel ViewModel => (ObservableObjectPageViewModel)BindingContext; + public SamplePageViewModel ViewModel => (SamplePageViewModel)BindingContext; protected override void OnAppearing() { diff --git a/samples/MvvmSampleXF/MvvmSampleXF/Views/IoCPage.xaml b/samples/MvvmSampleXF/MvvmSampleXF/Views/IoCPage.xaml index c0d2397..d0c8534 100644 --- a/samples/MvvmSampleXF/MvvmSampleXF/Views/IoCPage.xaml +++ b/samples/MvvmSampleXF/MvvmSampleXF/Views/IoCPage.xaml @@ -3,9 +3,11 @@ xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:vm="clr-namespace:MvvmSample.Core.ViewModels;assembly=MvvmSample.Core" xmlns:md="clr-namespace:Xam.Forms.Markdown;assembly=Xam.Forms.MarkdownView" - x:Class="MvvmSampleXF.Views.IoCPage" + xmlns:mvvmSampleXf="clr-namespace:MvvmSampleXF;assembly=MvvmSampleXF" + x:Class="MvvmSampleXF.Views.IocPage" x:DataType="vm:IocPageViewModel" - Title="Inversion of Control"> + Title="Inversion of Control" + mvvmSampleXf:ViewModelLocator.InitViewModel="True"> diff --git a/samples/MvvmSampleXF/MvvmSampleXF/Views/IoCPage.xaml.cs b/samples/MvvmSampleXF/MvvmSampleXF/Views/IoCPage.xaml.cs index 81a1fd9..83dcae6 100644 --- a/samples/MvvmSampleXF/MvvmSampleXF/Views/IoCPage.xaml.cs +++ b/samples/MvvmSampleXF/MvvmSampleXF/Views/IoCPage.xaml.cs @@ -1,5 +1,4 @@ using MvvmSample.Core.ViewModels; -using CommunityToolkit.Mvvm.DependencyInjection; using Xamarin.Forms; using Xamarin.Forms.Xaml; @@ -7,13 +6,11 @@ namespace MvvmSampleXF.Views { [XamlCompilation(XamlCompilationOptions.Compile)] - public partial class IoCPage : ContentPage + public partial class IocPage : ContentPage { - public IoCPage() + public IocPage() { InitializeComponent(); - - BindingContext = Ioc.Default.GetRequiredService(); } public IocPageViewModel ViewModel => (IocPageViewModel)BindingContext; diff --git a/samples/MvvmSampleXF/MvvmSampleXF/Views/MessengerPage.xaml b/samples/MvvmSampleXF/MvvmSampleXF/Views/MessengerPage.xaml index 26598c6..31f2384 100644 --- a/samples/MvvmSampleXF/MvvmSampleXF/Views/MessengerPage.xaml +++ b/samples/MvvmSampleXF/MvvmSampleXF/Views/MessengerPage.xaml @@ -3,9 +3,11 @@ xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:vm="clr-namespace:MvvmSample.Core.ViewModels;assembly=MvvmSample.Core" xmlns:md="clr-namespace:Xam.Forms.Markdown;assembly=Xam.Forms.MarkdownView" + xmlns:mvvmSampleXf="clr-namespace:MvvmSampleXF;assembly=MvvmSampleXF" x:Class="MvvmSampleXF.Views.MessengerPage" x:DataType="vm:MessengerPageViewModel" - Title="Messenger"> + Title="Messenger" + mvvmSampleXf:ViewModelLocator.InitViewModel="True"> diff --git a/samples/MvvmSampleXF/MvvmSampleXF/Views/MessengerPage.xaml.cs b/samples/MvvmSampleXF/MvvmSampleXF/Views/MessengerPage.xaml.cs index a389bed..a095988 100644 --- a/samples/MvvmSampleXF/MvvmSampleXF/Views/MessengerPage.xaml.cs +++ b/samples/MvvmSampleXF/MvvmSampleXF/Views/MessengerPage.xaml.cs @@ -1,5 +1,4 @@ using MvvmSample.Core.ViewModels; -using CommunityToolkit.Mvvm.DependencyInjection; using Xamarin.Forms; using Xamarin.Forms.Xaml; @@ -12,8 +11,6 @@ public partial class MessengerPage : ContentPage public MessengerPage() { InitializeComponent(); - - BindingContext = Ioc.Default.GetRequiredService(); } public MessengerPageViewModel ViewModel => (MessengerPageViewModel)BindingContext; diff --git a/samples/MvvmSampleXF/MvvmSampleXF/Views/MessengerRequestPage.xaml b/samples/MvvmSampleXF/MvvmSampleXF/Views/MessengerRequestPage.xaml index b965389..560b3a6 100644 --- a/samples/MvvmSampleXF/MvvmSampleXF/Views/MessengerRequestPage.xaml +++ b/samples/MvvmSampleXF/MvvmSampleXF/Views/MessengerRequestPage.xaml @@ -4,8 +4,10 @@ xmlns:vm="clr-namespace:MvvmSample.Core.ViewModels;assembly=MvvmSample.Core" xmlns:md="clr-namespace:Xam.Forms.Markdown;assembly=Xam.Forms.MarkdownView" xmlns:controls="clr-namespace:MvvmSampleXF.Controls" + xmlns:mvvmSampleXf="clr-namespace:MvvmSampleXF;assembly=MvvmSampleXF" x:Class="MvvmSampleXF.Views.MessengerRequestPage" - Title="Messenger"> + Title="Messenger" + mvvmSampleXf:ViewModelLocator.InitViewModel="True"> diff --git a/samples/MvvmSampleXF/MvvmSampleXF/Views/MessengerRequestPage.xaml.cs b/samples/MvvmSampleXF/MvvmSampleXF/Views/MessengerRequestPage.xaml.cs index b334e34..19a2eac 100644 --- a/samples/MvvmSampleXF/MvvmSampleXF/Views/MessengerRequestPage.xaml.cs +++ b/samples/MvvmSampleXF/MvvmSampleXF/Views/MessengerRequestPage.xaml.cs @@ -1,5 +1,4 @@ using MvvmSample.Core.ViewModels; -using CommunityToolkit.Mvvm.DependencyInjection; using Xamarin.Forms; using Xamarin.Forms.Xaml; @@ -12,8 +11,6 @@ public partial class MessengerRequestPage : ContentPage public MessengerRequestPage() { InitializeComponent(); - - BindingContext = Ioc.Default.GetRequiredService(); } public MessengerPageViewModel ViewModel => (MessengerPageViewModel)BindingContext; diff --git a/samples/MvvmSampleXF/MvvmSampleXF/Views/MessengerSendPage.xaml b/samples/MvvmSampleXF/MvvmSampleXF/Views/MessengerSendPage.xaml index 640a622..f418179 100644 --- a/samples/MvvmSampleXF/MvvmSampleXF/Views/MessengerSendPage.xaml +++ b/samples/MvvmSampleXF/MvvmSampleXF/Views/MessengerSendPage.xaml @@ -4,9 +4,11 @@ xmlns:vm="clr-namespace:MvvmSample.Core.ViewModels;assembly=MvvmSample.Core" xmlns:md="clr-namespace:Xam.Forms.Markdown;assembly=Xam.Forms.MarkdownView" xmlns:controls="clr-namespace:MvvmSampleXF.Controls" + xmlns:mvvmSampleXf="clr-namespace:MvvmSampleXF;assembly=MvvmSampleXF" x:Class="MvvmSampleXF.Views.MessengerSendPage" x:DataType="vm:MessengerPageViewModel" - Title="Messenger"> + Title="Messenger" + mvvmSampleXf:ViewModelLocator.InitViewModel="True"> diff --git a/samples/MvvmSampleXF/MvvmSampleXF/Views/MessengerSendPage.xaml.cs b/samples/MvvmSampleXF/MvvmSampleXF/Views/MessengerSendPage.xaml.cs index 7ebb261..1c254aa 100644 --- a/samples/MvvmSampleXF/MvvmSampleXF/Views/MessengerSendPage.xaml.cs +++ b/samples/MvvmSampleXF/MvvmSampleXF/Views/MessengerSendPage.xaml.cs @@ -1,5 +1,4 @@ using MvvmSample.Core.ViewModels; -using CommunityToolkit.Mvvm.DependencyInjection; using Xamarin.Forms; using Xamarin.Forms.Xaml; @@ -12,8 +11,6 @@ public partial class MessengerSendPage : ContentPage public MessengerSendPage() { InitializeComponent(); - - BindingContext = Ioc.Default.GetRequiredService(); } public MessengerPageViewModel ViewModel => (MessengerPageViewModel)BindingContext; diff --git a/samples/MvvmSampleXF/MvvmSampleXF/Views/ObservableObjectPage.xaml b/samples/MvvmSampleXF/MvvmSampleXF/Views/ObservableObjectPage.xaml index 9713779..6fa02f7 100644 --- a/samples/MvvmSampleXF/MvvmSampleXF/Views/ObservableObjectPage.xaml +++ b/samples/MvvmSampleXF/MvvmSampleXF/Views/ObservableObjectPage.xaml @@ -4,9 +4,11 @@ xmlns:vm="clr-namespace:MvvmSample.Core.ViewModels;assembly=MvvmSample.Core" xmlns:md="clr-namespace:Xam.Forms.Markdown;assembly=Xam.Forms.MarkdownView" xmlns:controls="clr-namespace:MvvmSampleXF.Controls" + xmlns:mvvmSampleXf="clr-namespace:MvvmSampleXF;assembly=MvvmSampleXF" x:Class="MvvmSampleXF.Views.ObservableObjectPage" x:DataType="vm:ObservableObjectPageViewModel" - Title="ObservableObject"> + Title="ObservableObject" + mvvmSampleXf:ViewModelLocator.InitViewModel="True"> diff --git a/samples/MvvmSampleXF/MvvmSampleXF/Views/ObservableObjectPage.xaml.cs b/samples/MvvmSampleXF/MvvmSampleXF/Views/ObservableObjectPage.xaml.cs index 297acc7..08aa3f7 100644 --- a/samples/MvvmSampleXF/MvvmSampleXF/Views/ObservableObjectPage.xaml.cs +++ b/samples/MvvmSampleXF/MvvmSampleXF/Views/ObservableObjectPage.xaml.cs @@ -1,5 +1,4 @@ using MvvmSample.Core.ViewModels; -using CommunityToolkit.Mvvm.DependencyInjection; using Xamarin.Forms; using Xamarin.Forms.Xaml; @@ -12,8 +11,6 @@ public partial class ObservableObjectPage : ContentPage public ObservableObjectPage() { InitializeComponent(); - - BindingContext = Ioc.Default.GetRequiredService(); } public ObservableObjectPageViewModel ViewModel => (ObservableObjectPageViewModel)BindingContext; diff --git a/samples/MvvmSampleXF/MvvmSampleXF/Views/PuttingThingsTogetherPage.xaml b/samples/MvvmSampleXF/MvvmSampleXF/Views/PuttingThingsTogetherPage.xaml index 5366786..249e196 100644 --- a/samples/MvvmSampleXF/MvvmSampleXF/Views/PuttingThingsTogetherPage.xaml +++ b/samples/MvvmSampleXF/MvvmSampleXF/Views/PuttingThingsTogetherPage.xaml @@ -3,8 +3,10 @@ xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:vm="clr-namespace:MvvmSample.Core.ViewModels;assembly=MvvmSample.Core" xmlns:md="clr-namespace:Xam.Forms.Markdown;assembly=Xam.Forms.MarkdownView" + xmlns:mvvmSampleXf="clr-namespace:MvvmSampleXF;assembly=MvvmSampleXF" x:Class="MvvmSampleXF.Views.PuttingThingsTogetherPage" - x:DataType="vm:SamplePageViewModel"> + x:DataType="vm:SamplePageViewModel" + mvvmSampleXf:ViewModelLocator.InitViewModel="True"> diff --git a/samples/MvvmSampleXF/MvvmSampleXF/Views/PuttingThingsTogetherPage.xaml.cs b/samples/MvvmSampleXF/MvvmSampleXF/Views/PuttingThingsTogetherPage.xaml.cs index 452bce1..e7f7e62 100644 --- a/samples/MvvmSampleXF/MvvmSampleXF/Views/PuttingThingsTogetherPage.xaml.cs +++ b/samples/MvvmSampleXF/MvvmSampleXF/Views/PuttingThingsTogetherPage.xaml.cs @@ -1,5 +1,4 @@ using MvvmSample.Core.ViewModels; -using CommunityToolkit.Mvvm.DependencyInjection; using Xamarin.Forms; using Xamarin.Forms.Xaml; @@ -12,8 +11,6 @@ public partial class PuttingThingsTogetherPage : ContentPage public PuttingThingsTogetherPage() { InitializeComponent(); - - BindingContext = Ioc.Default.GetRequiredService(); } public SamplePageViewModel ViewModel => (SamplePageViewModel)BindingContext; diff --git a/samples/MvvmSampleXF/MvvmSampleXF/Views/RedditServicePage.xaml b/samples/MvvmSampleXF/MvvmSampleXF/Views/RedditServicePage.xaml index 561a422..c5686f7 100644 --- a/samples/MvvmSampleXF/MvvmSampleXF/Views/RedditServicePage.xaml +++ b/samples/MvvmSampleXF/MvvmSampleXF/Views/RedditServicePage.xaml @@ -3,9 +3,11 @@ xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:vm="clr-namespace:MvvmSample.Core.ViewModels;assembly=MvvmSample.Core" xmlns:md="clr-namespace:Xam.Forms.Markdown;assembly=Xam.Forms.MarkdownView" + xmlns:mvvmSampleXf="clr-namespace:MvvmSampleXF;assembly=MvvmSampleXF" x:DataType="vm:SamplePageViewModel" x:Class="MvvmSampleXF.Views.RedditServicePage" - Title="Creating a Service"> + Title="Creating a Service" + mvvmSampleXf:ViewModelLocator.InitViewModel="True"> (); } public SamplePageViewModel ViewModel => (SamplePageViewModel)BindingContext; diff --git a/samples/MvvmSampleXF/MvvmSampleXF/Views/RelayCommandPage.xaml b/samples/MvvmSampleXF/MvvmSampleXF/Views/RelayCommandPage.xaml index df6d620..3eb1663 100644 --- a/samples/MvvmSampleXF/MvvmSampleXF/Views/RelayCommandPage.xaml +++ b/samples/MvvmSampleXF/MvvmSampleXF/Views/RelayCommandPage.xaml @@ -4,9 +4,11 @@ xmlns:md="clr-namespace:Xam.Forms.Markdown;assembly=Xam.Forms.MarkdownView" xmlns:vm="clr-namespace:MvvmSample.Core.ViewModels;assembly=MvvmSample.Core" xmlns:controls="clr-namespace:MvvmSampleXF.Controls" + xmlns:mvvmSampleXf="clr-namespace:MvvmSampleXF;assembly=MvvmSampleXF" x:Class="MvvmSampleXF.Views.RelayCommandPage" x:DataType="vm:RelayCommandPageViewModel" - Title="Commands"> + Title="Commands" + mvvmSampleXf:ViewModelLocator.InitViewModel="True"> diff --git a/samples/MvvmSampleXF/MvvmSampleXF/Views/RelayCommandPage.xaml.cs b/samples/MvvmSampleXF/MvvmSampleXF/Views/RelayCommandPage.xaml.cs index 1bb4bf1..1df8eda 100644 --- a/samples/MvvmSampleXF/MvvmSampleXF/Views/RelayCommandPage.xaml.cs +++ b/samples/MvvmSampleXF/MvvmSampleXF/Views/RelayCommandPage.xaml.cs @@ -1,5 +1,4 @@ using MvvmSample.Core.ViewModels; -using CommunityToolkit.Mvvm.DependencyInjection; using Xamarin.Forms; using Xamarin.Forms.Xaml; @@ -12,8 +11,6 @@ public partial class RelayCommandPage : ContentPage public RelayCommandPage() { InitializeComponent(); - - BindingContext = Ioc.Default.GetRequiredService(); } public RelayCommandPageViewModel ViewModel => (RelayCommandPageViewModel)BindingContext; diff --git a/samples/MvvmSampleXF/MvvmSampleXF/Views/SettingUpTheViewModelsPage.xaml b/samples/MvvmSampleXF/MvvmSampleXF/Views/SettingUpTheViewModelsPage.xaml index 31e9380..b0420e5 100644 --- a/samples/MvvmSampleXF/MvvmSampleXF/Views/SettingUpTheViewModelsPage.xaml +++ b/samples/MvvmSampleXF/MvvmSampleXF/Views/SettingUpTheViewModelsPage.xaml @@ -4,8 +4,10 @@ x:Class="MvvmSampleXF.Views.SettingUpTheViewModelsPage" xmlns:vm="clr-namespace:MvvmSample.Core.ViewModels;assembly=MvvmSample.Core" xmlns:md="clr-namespace:Xam.Forms.Markdown;assembly=Xam.Forms.MarkdownView" + xmlns:mvvmSampleXf="clr-namespace:MvvmSampleXF;assembly=MvvmSampleXF" x:DataType="vm:SamplePageViewModel" - Title="ViewModel Setup"> + Title="ViewModel Setup" + mvvmSampleXf:ViewModelLocator.InitViewModel="True"> (); } public SamplePageViewModel ViewModel => (SamplePageViewModel)BindingContext; diff --git a/samples/MvvmSampleXF/MvvmSampleXF/Views/SettingsServicePage.xaml b/samples/MvvmSampleXF/MvvmSampleXF/Views/SettingsServicePage.xaml index 8e3d970..ae3ec65 100644 --- a/samples/MvvmSampleXF/MvvmSampleXF/Views/SettingsServicePage.xaml +++ b/samples/MvvmSampleXF/MvvmSampleXF/Views/SettingsServicePage.xaml @@ -4,8 +4,10 @@ x:Class="MvvmSampleXF.Views.SettingsServicePage" xmlns:vm="clr-namespace:MvvmSample.Core.ViewModels;assembly=MvvmSample.Core" xmlns:md="clr-namespace:Xam.Forms.Markdown;assembly=Xam.Forms.MarkdownView" + xmlns:mvvmSampleXf="clr-namespace:MvvmSampleXF;assembly=MvvmSampleXF" x:DataType="vm:SamplePageViewModel" - Title="Creating a Service"> + Title="Creating a Service" + mvvmSampleXf:ViewModelLocator.InitViewModel="True"> (); } public SamplePageViewModel ViewModel => (SamplePageViewModel)BindingContext; diff --git a/samples/MvvmSampleXF/MvvmSampleXF/Views/Widgets/PostWidget.xaml b/samples/MvvmSampleXF/MvvmSampleXF/Views/Widgets/PostWidget.xaml index 1a6efcf..56ef186 100644 --- a/samples/MvvmSampleXF/MvvmSampleXF/Views/Widgets/PostWidget.xaml +++ b/samples/MvvmSampleXF/MvvmSampleXF/Views/Widgets/PostWidget.xaml @@ -3,7 +3,9 @@ xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:widgets="clr-namespace:MvvmSample.Core.ViewModels.Widgets;assembly=MvvmSample.Core" xmlns:converters="clr-namespace:MvvmSampleXF.Converters" - x:Class="MvvmSampleXF.Views.Widgets.PostWidget"> + xmlns:mvvmSampleXf="clr-namespace:MvvmSampleXF;assembly=MvvmSampleXF" + x:Class="MvvmSampleXF.Views.Widgets.PostWidget" + mvvmSampleXf:ViewModelLocator.InitViewModel="True"> diff --git a/samples/MvvmSampleXF/MvvmSampleXF/Views/Widgets/PostWidget.xaml.cs b/samples/MvvmSampleXF/MvvmSampleXF/Views/Widgets/PostWidget.xaml.cs index e96e3b8..337388b 100644 --- a/samples/MvvmSampleXF/MvvmSampleXF/Views/Widgets/PostWidget.xaml.cs +++ b/samples/MvvmSampleXF/MvvmSampleXF/Views/Widgets/PostWidget.xaml.cs @@ -1,5 +1,4 @@ using MvvmSample.Core.ViewModels.Widgets; -using CommunityToolkit.Mvvm.DependencyInjection; using Xamarin.Forms; using Xamarin.Forms.Xaml; @@ -12,8 +11,6 @@ public partial class PostWidget : ContentView public PostWidget() { InitializeComponent(); - - BindingContext = Ioc.Default.GetRequiredService(); } public PostWidgetViewModel ViewModel => (PostWidgetViewModel)BindingContext; diff --git a/samples/MvvmSampleXF/MvvmSampleXF/Views/Widgets/SubredditWidget.xaml b/samples/MvvmSampleXF/MvvmSampleXF/Views/Widgets/SubredditWidget.xaml index 6b2620a..e7721fb 100644 --- a/samples/MvvmSampleXF/MvvmSampleXF/Views/Widgets/SubredditWidget.xaml +++ b/samples/MvvmSampleXF/MvvmSampleXF/Views/Widgets/SubredditWidget.xaml @@ -5,7 +5,9 @@ xmlns:models="clr-namespace:MvvmSample.Core.Models;assembly=MvvmSample.Core" xmlns:widgets="clr-namespace:MvvmSample.Core.ViewModels.Widgets;assembly=MvvmSample.Core" xmlns:converters="clr-namespace:MvvmSampleXF.Converters" - x:Class="MvvmSampleXF.Views.Widgets.SubredditWidget"> + xmlns:mvvmSampleXf="clr-namespace:MvvmSampleXF;assembly=MvvmSampleXF" + x:Class="MvvmSampleXF.Views.Widgets.SubredditWidget" + mvvmSampleXf:ViewModelLocator.InitViewModel="True"> diff --git a/samples/MvvmSampleXF/MvvmSampleXF/Views/Widgets/SubredditWidget.xaml.cs b/samples/MvvmSampleXF/MvvmSampleXF/Views/Widgets/SubredditWidget.xaml.cs index 7177681..80743fc 100644 --- a/samples/MvvmSampleXF/MvvmSampleXF/Views/Widgets/SubredditWidget.xaml.cs +++ b/samples/MvvmSampleXF/MvvmSampleXF/Views/Widgets/SubredditWidget.xaml.cs @@ -1,6 +1,5 @@ using MvvmSample.Core.ViewModels.Widgets; using System; -using CommunityToolkit.Mvvm.DependencyInjection; using Xamarin.Forms; using Xamarin.Forms.Xaml; @@ -14,8 +13,6 @@ public partial class SubredditWidget : ContentView public SubredditWidget() { InitializeComponent(); - - BindingContext = Ioc.Default.GetRequiredService(); } public SubredditWidgetViewModel ViewModel => (SubredditWidgetViewModel)BindingContext; From d3c2877b5ede66e2a32ae57adab23765a868070e Mon Sep 17 00:00:00 2001 From: DenisT Date: Sat, 12 Mar 2022 10:15:15 +0700 Subject: [PATCH 2/2] format --- .../MvvmSampleXF/MvvmSampleXF/Views/AsyncRelayCommandPage.xaml | 2 +- samples/MvvmSampleXF/MvvmSampleXF/Views/BuildingTheUIPage.xaml | 2 +- samples/MvvmSampleXF/MvvmSampleXF/Views/IntroductionPage.xaml | 2 +- samples/MvvmSampleXF/MvvmSampleXF/Views/IoCPage.xaml | 2 +- samples/MvvmSampleXF/MvvmSampleXF/Views/MessengerPage.xaml | 2 +- .../MvvmSampleXF/MvvmSampleXF/Views/MessengerRequestPage.xaml | 2 +- samples/MvvmSampleXF/MvvmSampleXF/Views/MessengerSendPage.xaml | 2 +- .../MvvmSampleXF/MvvmSampleXF/Views/ObservableObjectPage.xaml | 2 +- .../MvvmSampleXF/Views/PuttingThingsTogetherPage.xaml | 2 +- samples/MvvmSampleXF/MvvmSampleXF/Views/RedditServicePage.xaml | 2 +- samples/MvvmSampleXF/MvvmSampleXF/Views/RelayCommandPage.xaml | 2 +- .../MvvmSampleXF/Views/SettingUpTheViewModelsPage.xaml | 2 +- .../MvvmSampleXF/MvvmSampleXF/Views/SettingsServicePage.xaml | 2 +- samples/MvvmSampleXF/MvvmSampleXF/Views/Widgets/PostWidget.xaml | 2 +- .../MvvmSampleXF/Views/Widgets/SubredditWidget.xaml | 2 +- 15 files changed, 15 insertions(+), 15 deletions(-) diff --git a/samples/MvvmSampleXF/MvvmSampleXF/Views/AsyncRelayCommandPage.xaml b/samples/MvvmSampleXF/MvvmSampleXF/Views/AsyncRelayCommandPage.xaml index d30af23..452bd79 100644 --- a/samples/MvvmSampleXF/MvvmSampleXF/Views/AsyncRelayCommandPage.xaml +++ b/samples/MvvmSampleXF/MvvmSampleXF/Views/AsyncRelayCommandPage.xaml @@ -9,7 +9,7 @@ x:Class="MvvmSampleXF.Views.AsyncRelayCommandPage" x:DataType="vm:AsyncRelayCommandPageViewModel" Title="Commands" - mvvmSampleXf:ViewModelLocator.InitViewModel="True"> + mvvmSampleXf:ViewModelLocator.InitViewModel="True"> diff --git a/samples/MvvmSampleXF/MvvmSampleXF/Views/BuildingTheUIPage.xaml b/samples/MvvmSampleXF/MvvmSampleXF/Views/BuildingTheUIPage.xaml index 6d6e81a..2060171 100644 --- a/samples/MvvmSampleXF/MvvmSampleXF/Views/BuildingTheUIPage.xaml +++ b/samples/MvvmSampleXF/MvvmSampleXF/Views/BuildingTheUIPage.xaml @@ -7,7 +7,7 @@ x:Class="MvvmSampleXF.Views.BuildingTheUIPage" x:DataType="vm:SamplePageViewModel" Title="Building the UI" - mvvmSampleXf:ViewModelLocator.InitViewModel="True"> + mvvmSampleXf:ViewModelLocator.InitViewModel="True"> diff --git a/samples/MvvmSampleXF/MvvmSampleXF/Views/IntroductionPage.xaml b/samples/MvvmSampleXF/MvvmSampleXF/Views/IntroductionPage.xaml index b2df629..89b63f2 100644 --- a/samples/MvvmSampleXF/MvvmSampleXF/Views/IntroductionPage.xaml +++ b/samples/MvvmSampleXF/MvvmSampleXF/Views/IntroductionPage.xaml @@ -7,7 +7,7 @@ x:Class="MvvmSampleXF.Views.IntroductionPage" x:DataType="vm:ObservableObjectPageViewModel" Title="Introduction" - mvvmSampleXf:ViewModelLocator.InitViewModel="True"> + mvvmSampleXf:ViewModelLocator.InitViewModel="True"> diff --git a/samples/MvvmSampleXF/MvvmSampleXF/Views/IoCPage.xaml b/samples/MvvmSampleXF/MvvmSampleXF/Views/IoCPage.xaml index d0c8534..68d3cc3 100644 --- a/samples/MvvmSampleXF/MvvmSampleXF/Views/IoCPage.xaml +++ b/samples/MvvmSampleXF/MvvmSampleXF/Views/IoCPage.xaml @@ -7,7 +7,7 @@ x:Class="MvvmSampleXF.Views.IocPage" x:DataType="vm:IocPageViewModel" Title="Inversion of Control" - mvvmSampleXf:ViewModelLocator.InitViewModel="True"> + mvvmSampleXf:ViewModelLocator.InitViewModel="True"> diff --git a/samples/MvvmSampleXF/MvvmSampleXF/Views/MessengerPage.xaml b/samples/MvvmSampleXF/MvvmSampleXF/Views/MessengerPage.xaml index 31f2384..17b7df3 100644 --- a/samples/MvvmSampleXF/MvvmSampleXF/Views/MessengerPage.xaml +++ b/samples/MvvmSampleXF/MvvmSampleXF/Views/MessengerPage.xaml @@ -7,7 +7,7 @@ x:Class="MvvmSampleXF.Views.MessengerPage" x:DataType="vm:MessengerPageViewModel" Title="Messenger" - mvvmSampleXf:ViewModelLocator.InitViewModel="True"> + mvvmSampleXf:ViewModelLocator.InitViewModel="True"> diff --git a/samples/MvvmSampleXF/MvvmSampleXF/Views/MessengerRequestPage.xaml b/samples/MvvmSampleXF/MvvmSampleXF/Views/MessengerRequestPage.xaml index 560b3a6..46a2966 100644 --- a/samples/MvvmSampleXF/MvvmSampleXF/Views/MessengerRequestPage.xaml +++ b/samples/MvvmSampleXF/MvvmSampleXF/Views/MessengerRequestPage.xaml @@ -7,7 +7,7 @@ xmlns:mvvmSampleXf="clr-namespace:MvvmSampleXF;assembly=MvvmSampleXF" x:Class="MvvmSampleXF.Views.MessengerRequestPage" Title="Messenger" - mvvmSampleXf:ViewModelLocator.InitViewModel="True"> + mvvmSampleXf:ViewModelLocator.InitViewModel="True"> diff --git a/samples/MvvmSampleXF/MvvmSampleXF/Views/MessengerSendPage.xaml b/samples/MvvmSampleXF/MvvmSampleXF/Views/MessengerSendPage.xaml index f418179..41be164 100644 --- a/samples/MvvmSampleXF/MvvmSampleXF/Views/MessengerSendPage.xaml +++ b/samples/MvvmSampleXF/MvvmSampleXF/Views/MessengerSendPage.xaml @@ -8,7 +8,7 @@ x:Class="MvvmSampleXF.Views.MessengerSendPage" x:DataType="vm:MessengerPageViewModel" Title="Messenger" - mvvmSampleXf:ViewModelLocator.InitViewModel="True"> + mvvmSampleXf:ViewModelLocator.InitViewModel="True"> diff --git a/samples/MvvmSampleXF/MvvmSampleXF/Views/ObservableObjectPage.xaml b/samples/MvvmSampleXF/MvvmSampleXF/Views/ObservableObjectPage.xaml index 6fa02f7..86f747c 100644 --- a/samples/MvvmSampleXF/MvvmSampleXF/Views/ObservableObjectPage.xaml +++ b/samples/MvvmSampleXF/MvvmSampleXF/Views/ObservableObjectPage.xaml @@ -8,7 +8,7 @@ x:Class="MvvmSampleXF.Views.ObservableObjectPage" x:DataType="vm:ObservableObjectPageViewModel" Title="ObservableObject" - mvvmSampleXf:ViewModelLocator.InitViewModel="True"> + mvvmSampleXf:ViewModelLocator.InitViewModel="True"> diff --git a/samples/MvvmSampleXF/MvvmSampleXF/Views/PuttingThingsTogetherPage.xaml b/samples/MvvmSampleXF/MvvmSampleXF/Views/PuttingThingsTogetherPage.xaml index 249e196..e17c54a 100644 --- a/samples/MvvmSampleXF/MvvmSampleXF/Views/PuttingThingsTogetherPage.xaml +++ b/samples/MvvmSampleXF/MvvmSampleXF/Views/PuttingThingsTogetherPage.xaml @@ -6,7 +6,7 @@ xmlns:mvvmSampleXf="clr-namespace:MvvmSampleXF;assembly=MvvmSampleXF" x:Class="MvvmSampleXF.Views.PuttingThingsTogetherPage" x:DataType="vm:SamplePageViewModel" - mvvmSampleXf:ViewModelLocator.InitViewModel="True"> + mvvmSampleXf:ViewModelLocator.InitViewModel="True"> diff --git a/samples/MvvmSampleXF/MvvmSampleXF/Views/RedditServicePage.xaml b/samples/MvvmSampleXF/MvvmSampleXF/Views/RedditServicePage.xaml index c5686f7..4beaf5d 100644 --- a/samples/MvvmSampleXF/MvvmSampleXF/Views/RedditServicePage.xaml +++ b/samples/MvvmSampleXF/MvvmSampleXF/Views/RedditServicePage.xaml @@ -7,7 +7,7 @@ x:DataType="vm:SamplePageViewModel" x:Class="MvvmSampleXF.Views.RedditServicePage" Title="Creating a Service" - mvvmSampleXf:ViewModelLocator.InitViewModel="True"> + mvvmSampleXf:ViewModelLocator.InitViewModel="True"> + mvvmSampleXf:ViewModelLocator.InitViewModel="True"> diff --git a/samples/MvvmSampleXF/MvvmSampleXF/Views/SettingUpTheViewModelsPage.xaml b/samples/MvvmSampleXF/MvvmSampleXF/Views/SettingUpTheViewModelsPage.xaml index b0420e5..23ef9ac 100644 --- a/samples/MvvmSampleXF/MvvmSampleXF/Views/SettingUpTheViewModelsPage.xaml +++ b/samples/MvvmSampleXF/MvvmSampleXF/Views/SettingUpTheViewModelsPage.xaml @@ -7,7 +7,7 @@ xmlns:mvvmSampleXf="clr-namespace:MvvmSampleXF;assembly=MvvmSampleXF" x:DataType="vm:SamplePageViewModel" Title="ViewModel Setup" - mvvmSampleXf:ViewModelLocator.InitViewModel="True"> + mvvmSampleXf:ViewModelLocator.InitViewModel="True"> + mvvmSampleXf:ViewModelLocator.InitViewModel="True"> + mvvmSampleXf:ViewModelLocator.InitViewModel="True"> diff --git a/samples/MvvmSampleXF/MvvmSampleXF/Views/Widgets/SubredditWidget.xaml b/samples/MvvmSampleXF/MvvmSampleXF/Views/Widgets/SubredditWidget.xaml index e7721fb..c18887c 100644 --- a/samples/MvvmSampleXF/MvvmSampleXF/Views/Widgets/SubredditWidget.xaml +++ b/samples/MvvmSampleXF/MvvmSampleXF/Views/Widgets/SubredditWidget.xaml @@ -7,7 +7,7 @@ xmlns:converters="clr-namespace:MvvmSampleXF.Converters" xmlns:mvvmSampleXf="clr-namespace:MvvmSampleXF;assembly=MvvmSampleXF" x:Class="MvvmSampleXF.Views.Widgets.SubredditWidget" - mvvmSampleXf:ViewModelLocator.InitViewModel="True"> + mvvmSampleXf:ViewModelLocator.InitViewModel="True">