Skip to content

Commit

Permalink
Merge pull request PrismLibrary#2171 from PrismLibrary/vml-opt-out
Browse files Browse the repository at this point in the history
Make ViewModelLocator Opt-Out
  • Loading branch information
brianlagunas authored Aug 21, 2020
2 parents 43d1545 + 30a5b5b commit 522799b
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 17 deletions.
5 changes: 3 additions & 2 deletions src/Uno/Prism.Uno/PrismApplicationBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Prism.Events;
using Prism.Common;
using Prism.Events;
using Prism.Ioc;
using Prism.Logging;
using Prism.Modularity;
Expand Down Expand Up @@ -88,7 +89,7 @@ public virtual void Initialize()
var shell = CreateShell();
if (shell != null)
{
_containerExtension.Resolve<IRegionNavigationService>().NavigationFailed += (s, e) => Console.WriteLine($"Region navigation failed {e.Error}");
MvvmHelpers.AutowireViewModel(shell);
InitializeShell(shell);

void FinalizeInitialization()
Expand Down
8 changes: 4 additions & 4 deletions src/Uno/Prism.Uno/Services/Dialogs/DialogService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ IDialogWindow CreateDialogWindow(string name)
void ConfigureDialogWindowContent(string dialogName, IDialogWindow window, IDialogParameters parameters)
{
var content = _containerProvider.Resolve<object>(dialogName);
var dialogContent = content as FrameworkElement;
if (dialogContent == null)
if (!(content is FrameworkElement dialogContent))
{
throw new NullReferenceException("A dialog's content must be a FrameworkElement");
}

var viewModel = dialogContent.DataContext as IDialogAware;
if (viewModel == null)
MvvmHelpers.AutowireViewModel(content);

if (!(dialogContent.DataContext is IDialogAware viewModel))
{
throw new NullReferenceException($"A dialog's ViewModel must implement the IDialogAware interface ({dialogContent.DataContext})");
}
Expand Down
10 changes: 9 additions & 1 deletion src/Wpf/Prism.Wpf/Common/MvvmHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;

using Prism.Mvvm;
#if HAS_WINUI
using Windows.UI.Xaml;
#else
Expand All @@ -15,6 +15,14 @@ namespace Prism.Common
/// </summary>
public static class MvvmHelpers
{
internal static void AutowireViewModel(object viewOrViewModel)
{
if(viewOrViewModel is FrameworkElement view && ViewModelLocator.GetAutoWireViewModel(view) is null)
{
ViewModelLocator.SetAutoWireViewModel(view, true);
}
}

/// <summary>
/// Perform an <see cref="Action{T}"/> on a view and viewmodel.
/// </summary>
Expand Down
11 changes: 6 additions & 5 deletions src/Wpf/Prism.Wpf/Mvvm/ViewModelLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,24 @@ public static class ViewModelLocator
/// <summary>
/// The AutoWireViewModel attached property.
/// </summary>
public static DependencyProperty AutoWireViewModelProperty = DependencyProperty.RegisterAttached("AutoWireViewModel", typeof(bool), typeof(ViewModelLocator), new PropertyMetadata(defaultValue: false, propertyChangedCallback: AutoWireViewModelChanged));
public static DependencyProperty AutoWireViewModelProperty = DependencyProperty.RegisterAttached("AutoWireViewModel", typeof(bool?), typeof(ViewModelLocator), new PropertyMetadata(defaultValue: null, propertyChangedCallback: AutoWireViewModelChanged));

/// <summary>
/// Gets the value for the <see cref="AutoWireViewModelProperty"/> attached property.
/// </summary>
/// <param name="obj">The target element.</param>
/// <returns>The <see cref="AutoWireViewModelProperty"/> attached to the <paramref name="obj"/> element.</returns>
public static bool GetAutoWireViewModel(DependencyObject obj)
public static bool? GetAutoWireViewModel(DependencyObject obj)
{
return (bool)obj.GetValue(AutoWireViewModelProperty);
return (bool?)obj.GetValue(AutoWireViewModelProperty);
}

/// <summary>
/// Sets the <see cref="AutoWireViewModelProperty"/> attached property.
/// </summary>
/// <param name="obj">The target element.</param>
/// <param name="value">The value to attach.</param>
public static void SetAutoWireViewModel(DependencyObject obj, bool value)
public static void SetAutoWireViewModel(DependencyObject obj, bool? value)
{
obj.SetValue(AutoWireViewModelProperty, value);
}
Expand All @@ -44,7 +44,8 @@ private static void AutoWireViewModelChanged(DependencyObject d, DependencyPrope
if (!DesignerProperties.GetIsInDesignMode(d))
#endif
{
if ((bool)e.NewValue)
var value = (bool?)e.NewValue;
if (value.HasValue && value.Value)
{
ViewModelLocationProvider.AutoWireViewModelChanged(d, Bind);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Wpf/Prism.Wpf/PrismApplicationBase.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Windows;
using Prism.Common;
using Prism.Ioc;
using Prism.Modularity;
using Prism.Regions;
Expand Down Expand Up @@ -75,6 +76,7 @@ protected virtual void Initialize()
var shell = CreateShell();
if (shell != null)
{
MvvmHelpers.AutowireViewModel(shell);
RegionManager.SetRegionManager(shell, _containerExtension.Resolve<IRegionManager>());
RegionManager.UpdateRegions();
InitializeShell(shell);
Expand Down
4 changes: 3 additions & 1 deletion src/Wpf/Prism.Wpf/PrismBootstrapperBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Prism.Ioc;
using Prism.Common;
using Prism.Ioc;
using Prism.Modularity;
using Prism.Regions;
using System;
Expand Down Expand Up @@ -72,6 +73,7 @@ protected virtual void Initialize()
var shell = CreateShell();
if (shell != null)
{
MvvmHelpers.AutowireViewModel(shell);
RegionManager.SetRegionManager(shell, _containerExtension.Resolve<IRegionManager>());
RegionManager.UpdateRegions();
InitializeShell(shell);
Expand Down
1 change: 1 addition & 0 deletions src/Wpf/Prism.Wpf/Regions/RegionNavigationContentLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ protected virtual object CreateNewRegionItem(string candidateTargetContract)
try
{
newRegionItem = _container.Resolve<object>(candidateTargetContract);
MvvmHelpers.AutowireViewModel(newRegionItem);
}
catch (Exception e)
{
Expand Down
8 changes: 4 additions & 4 deletions src/Wpf/Prism.Wpf/Services/Dialogs/DialogService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@ protected virtual IDialogWindow CreateDialogWindow(string name)
protected virtual void ConfigureDialogWindowContent(string dialogName, IDialogWindow window, IDialogParameters parameters)
{
var content = _containerExtension.Resolve<object>(dialogName);
var dialogContent = content as FrameworkElement;
if (dialogContent == null)
if (!(content is FrameworkElement dialogContent))
throw new NullReferenceException("A dialog's content must be a FrameworkElement");

var viewModel = dialogContent.DataContext as IDialogAware;
if (viewModel == null)
MvvmHelpers.AutowireViewModel(dialogContent);

if (!(dialogContent.DataContext is IDialogAware viewModel))
throw new NullReferenceException("A dialog's ViewModel must implement the IDialogAware interface");

ConfigureDialogWindowProperties(window, dialogContent, viewModel);
Expand Down

0 comments on commit 522799b

Please sign in to comment.