From b1a6b1df2960730f100f75e522e139d512814ae1 Mon Sep 17 00:00:00 2001 From: Lbugnion Date: Sun, 8 Oct 2017 16:26:43 +0200 Subject: [PATCH] Refactoring design mode detection. --- .../GalaSoft.MvvmLight (NET35).csproj | 3 + .../GalaSoft.MvvmLight (NET4).csproj | 3 + .../Helpers/DesignerPlatformLibrary.cs | 190 ++++++++++++++++++ .../GalaSoft.MvvmLight (PCL)/ViewModelBase.cs | 181 +---------------- .../GalaSoft.MvvmLight (SL5).csproj | 3 + .../GalaSoft.MvvmLight.Extras (NET35).csproj | 3 + .../GalaSoft.MvvmLight.Extras (NET4).csproj | 3 + .../GalaSoft.MvvmLight.Extras (PCL).csproj | 7 +- .../GalaSoft.MvvmLight.Extras (SL5).csproj | 3 + .../Resources/Resource.Designer.cs | 60 ------ ...le_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs | 0 11 files changed, 214 insertions(+), 242 deletions(-) create mode 100644 GalaSoft.MvvmLight/Tests/GalaSoft.MvvmLight.Test (STD10)/obj/Release/TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs diff --git a/GalaSoft.MvvmLight/GalaSoft.MvvmLight (NET35)/GalaSoft.MvvmLight (NET35).csproj b/GalaSoft.MvvmLight/GalaSoft.MvvmLight (NET35)/GalaSoft.MvvmLight (NET35).csproj index 5c72cad..7f14192 100644 --- a/GalaSoft.MvvmLight/GalaSoft.MvvmLight (NET35)/GalaSoft.MvvmLight (NET35).csproj +++ b/GalaSoft.MvvmLight/GalaSoft.MvvmLight (NET35)/GalaSoft.MvvmLight (NET35).csproj @@ -56,6 +56,9 @@ Command\RelayCommandGeneric.cs + + Helpers\DesignerPlatformLibrary.cs + Helpers\IExecuteWithObject.cs diff --git a/GalaSoft.MvvmLight/GalaSoft.MvvmLight (NET4)/GalaSoft.MvvmLight (NET4).csproj b/GalaSoft.MvvmLight/GalaSoft.MvvmLight (NET4)/GalaSoft.MvvmLight (NET4).csproj index bce22a6..75de18a 100644 --- a/GalaSoft.MvvmLight/GalaSoft.MvvmLight (NET4)/GalaSoft.MvvmLight (NET4).csproj +++ b/GalaSoft.MvvmLight/GalaSoft.MvvmLight (NET4)/GalaSoft.MvvmLight (NET4).csproj @@ -58,6 +58,9 @@ Command\RelayCommandGeneric.cs + + Helpers\DesignerPlatformLibrary.cs + Helpers\IExecuteWithObject.cs diff --git a/GalaSoft.MvvmLight/GalaSoft.MvvmLight (PCL)/Helpers/DesignerPlatformLibrary.cs b/GalaSoft.MvvmLight/GalaSoft.MvvmLight (PCL)/Helpers/DesignerPlatformLibrary.cs index 4285747..00be2af 100644 --- a/GalaSoft.MvvmLight/GalaSoft.MvvmLight (PCL)/Helpers/DesignerPlatformLibrary.cs +++ b/GalaSoft.MvvmLight/GalaSoft.MvvmLight (PCL)/Helpers/DesignerPlatformLibrary.cs @@ -14,6 +14,8 @@ // **************************************************************************** using System; +using System.Linq; +using System.Reflection; namespace GalaSoft.MvvmLight.Helpers { @@ -67,6 +69,194 @@ namespace GalaSoft.MvvmLight.Helpers private static DesignerPlatformLibrary? _detectedDesignerPlatformLibrary; + + private static bool? _isInDesignMode; + + public static bool IsInDesignMode + { + get + { + if (!_isInDesignMode.HasValue) + { +#if PORTABLE + _isInDesignMode = IsInDesignModePortable(); +#elif SILVERLIGHT + _isInDesignMode = System.ComponentModel.DesignerProperties.IsInDesignTool; +#elif NETFX_CORE + _isInDesignMode = DesignMode.DesignModeEnabled; +#elif XAMARIN + _isInDesignMode = false; +#else + var prop = System.ComponentModel.DesignerProperties.IsInDesignModeProperty; + _isInDesignMode + = (bool)System.ComponentModel.DependencyPropertyDescriptor + .FromProperty(prop, typeof(System.Windows.FrameworkElement)) + .Metadata.DefaultValue; +#endif + } + + return _isInDesignMode.Value; + } + } + +#if PORTABLE + private static bool IsInDesignModePortable() + { + // As a portable lib, we need see what framework we're runnign on + // and use reflection to get the designer value + + var platform = DesignerLibrary.DetectedDesignerLibrary; + + if (platform == DesignerPlatformLibrary.WinRt) + { + return IsInDesignModeMetro(); + } + + if (platform == DesignerPlatformLibrary.Silverlight) + { + var desMode = IsInDesignModeSilverlight(); + if (!desMode) + { + desMode = IsInDesignModeNet(); // hard to tell these apart in the designer + } + + return desMode; + } + + if (platform == DesignerPlatformLibrary.Net) + { + return IsInDesignModeNet(); + } + + return false; + } + + private static bool IsInDesignModeSilverlight() + { + try + { + var dm = Type.GetType("System.ComponentModel.DesignerProperties, System.Windows, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e"); + + if (dm == null) + { + return false; + } + + var dme = dm.GetTypeInfo().GetDeclaredProperty("IsInDesignTool"); + + if (dme == null) + { + return false; + } + + return (bool)dme.GetValue(null, null); + } + catch + { + return false; + } + } + + private static bool IsInDesignModeMetro() + { + try + { + var dm = Type.GetType("Windows.ApplicationModel.DesignMode, Windows, ContentType=WindowsRuntime"); + + var dme = dm.GetTypeInfo().GetDeclaredProperty("DesignModeEnabled"); + return (bool)dme.GetValue(null, null); + } + catch + { + return false; + } + } + + private static bool IsInDesignModeNet() + { + try + { + var dm = + Type.GetType( + "System.ComponentModel.DesignerProperties, PresentationFramework, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"); + + if (dm == null) + { + return false; + } + + var dmp = dm.GetTypeInfo().GetDeclaredField("IsInDesignModeProperty").GetValue(null); + + var dpd = + Type.GetType( + "System.ComponentModel.DependencyPropertyDescriptor, WindowsBase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"); + var typeFe = + Type.GetType("System.Windows.FrameworkElement, PresentationFramework, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"); + + if (dpd == null + || typeFe == null) + { + return false; + } + + var fromPropertys = dpd + .GetTypeInfo() + .GetDeclaredMethods("FromProperty") + .ToList(); + + if (fromPropertys == null + || fromPropertys.Count == 0) + { + return false; + } + + var fromProperty = fromPropertys + .FirstOrDefault(mi => mi.IsPublic && mi.IsStatic && mi.GetParameters().Length == 2); + + if (fromProperty == null) + { + return false; + } + + var descriptor = fromProperty.Invoke(null, new[] { dmp, typeFe }); + + if (descriptor == null) + { + return false; + } + + var metaProp = dpd.GetTypeInfo().GetDeclaredProperty("Metadata"); + + if (metaProp == null) + { + return false; + } + + var metadata = metaProp.GetValue(descriptor, null); + var tPropMeta = Type.GetType("System.Windows.PropertyMetadata, WindowsBase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"); + + if (metadata == null + || tPropMeta == null) + { + return false; + } + + var dvProp = tPropMeta.GetTypeInfo().GetDeclaredProperty("DefaultValue"); + + if (dvProp == null) + { + return false; + } + + var dv = (bool)dvProp.GetValue(metadata, null); + return dv; + } + catch + { + return false; + } + } +#endif } internal enum DesignerPlatformLibrary diff --git a/GalaSoft.MvvmLight/GalaSoft.MvvmLight (PCL)/ViewModelBase.cs b/GalaSoft.MvvmLight/GalaSoft.MvvmLight (PCL)/ViewModelBase.cs index 269b26a..b44e834 100644 --- a/GalaSoft.MvvmLight/GalaSoft.MvvmLight (PCL)/ViewModelBase.cs +++ b/GalaSoft.MvvmLight/GalaSoft.MvvmLight (PCL)/ViewModelBase.cs @@ -57,7 +57,6 @@ namespace GalaSoft.MvvmLight Justification = "Constructors should remain public to allow serialization.")] public abstract class ViewModelBase : ObservableObject, ICleanup { - private static bool? _isInDesignMode; private IMessenger _messengerInstance; /// @@ -113,188 +112,10 @@ namespace GalaSoft.MvvmLight { get { - if (!_isInDesignMode.HasValue) - { -#if PORTABLE - _isInDesignMode = IsInDesignModePortable(); -#elif SILVERLIGHT - _isInDesignMode = DesignerProperties.IsInDesignTool; -#elif NETFX_CORE - _isInDesignMode = DesignMode.DesignModeEnabled; -#elif XAMARIN - _isInDesignMode = false; -#else - var prop = DesignerProperties.IsInDesignModeProperty; - _isInDesignMode - = (bool)DependencyPropertyDescriptor - .FromProperty(prop, typeof(FrameworkElement)) - .Metadata.DefaultValue; -#endif - } - - return _isInDesignMode.Value; + return DesignerLibrary.IsInDesignMode; } } -#if PORTABLE - private static bool IsInDesignModePortable() - { - // As a portable lib, we need see what framework we're runnign on - // and use reflection to get the designer value - - var platform = DesignerLibrary.DetectedDesignerLibrary; - - if (platform == DesignerPlatformLibrary.WinRt) - { - return IsInDesignModeMetro(); - } - - if(platform == DesignerPlatformLibrary.Silverlight) - { - var desMode = IsInDesignModeSilverlight(); - if (!desMode) - { - desMode = IsInDesignModeNet(); // hard to tell these apart in the designer - } - - return desMode; - } - - if (platform == DesignerPlatformLibrary.Net) - { - return IsInDesignModeNet(); - } - - return false; - } - - private static bool IsInDesignModeSilverlight() - { - try - { - var dm = Type.GetType("System.ComponentModel.DesignerProperties, System.Windows, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e"); - - if (dm == null) - { - return false; - } - - var dme = dm.GetTypeInfo().GetDeclaredProperty("IsInDesignTool"); - - if (dme == null) - { - return false; - } - - return (bool)dme.GetValue(null, null); - } - catch - { - return false; - } - } - - private static bool IsInDesignModeMetro() - { - try - { - var dm = Type.GetType("Windows.ApplicationModel.DesignMode, Windows, ContentType=WindowsRuntime"); - - var dme = dm.GetTypeInfo().GetDeclaredProperty("DesignModeEnabled"); - return (bool)dme.GetValue(null, null); - } - catch - { - return false; - } - } - - private static bool IsInDesignModeNet() - { - try - { - var dm = - Type.GetType( - "System.ComponentModel.DesignerProperties, PresentationFramework, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"); - - if (dm == null) - { - return false; - } - - var dmp = dm.GetTypeInfo().GetDeclaredField("IsInDesignModeProperty").GetValue(null); - - var dpd = - Type.GetType( - "System.ComponentModel.DependencyPropertyDescriptor, WindowsBase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"); - var typeFe = - Type.GetType("System.Windows.FrameworkElement, PresentationFramework, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"); - - if (dpd == null - || typeFe == null) - { - return false; - } - - var fromPropertys = dpd - .GetTypeInfo() - .GetDeclaredMethods("FromProperty") - .ToList(); - - if (fromPropertys == null - || fromPropertys.Count == 0) - { - return false; - } - - var fromProperty = fromPropertys - .FirstOrDefault(mi => mi.IsPublic && mi.IsStatic && mi.GetParameters().Length == 2); - - if (fromProperty == null) - { - return false; - } - - var descriptor = fromProperty.Invoke(null, new[] {dmp, typeFe}); - - if (descriptor == null) - { - return false; - } - - var metaProp = dpd.GetTypeInfo().GetDeclaredProperty("Metadata"); - - if (metaProp == null) - { - return false; - } - - var metadata = metaProp.GetValue(descriptor, null); - var tPropMeta = Type.GetType("System.Windows.PropertyMetadata, WindowsBase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"); - - if (metadata == null - || tPropMeta == null) - { - return false; - } - - var dvProp = tPropMeta.GetTypeInfo().GetDeclaredProperty("DefaultValue"); - - if (dvProp == null) - { - return false; - } - - var dv = (bool)dvProp.GetValue(metadata, null); - return dv; - } - catch - { - return false; - } - } -#endif - /// /// Gets or sets an instance of a used to /// broadcast messages to other objects. If null, this class will diff --git a/GalaSoft.MvvmLight/GalaSoft.MvvmLight (SL5)/GalaSoft.MvvmLight (SL5).csproj b/GalaSoft.MvvmLight/GalaSoft.MvvmLight (SL5)/GalaSoft.MvvmLight (SL5).csproj index aa2cbf2..8f6c55d 100644 --- a/GalaSoft.MvvmLight/GalaSoft.MvvmLight (SL5)/GalaSoft.MvvmLight (SL5).csproj +++ b/GalaSoft.MvvmLight/GalaSoft.MvvmLight (SL5)/GalaSoft.MvvmLight (SL5).csproj @@ -70,6 +70,9 @@ Command\RelayCommandGeneric.cs + + Helpers\DesignerPlatformLibrary.cs + Helpers\IExecuteWithObject.cs diff --git a/GalaSoft.MvvmLight/GalaSoft.MvvmLight.Extras (NET35)/GalaSoft.MvvmLight.Extras (NET35).csproj b/GalaSoft.MvvmLight/GalaSoft.MvvmLight.Extras (NET35)/GalaSoft.MvvmLight.Extras (NET35).csproj index 745c8b9..5843579 100644 --- a/GalaSoft.MvvmLight/GalaSoft.MvvmLight.Extras (NET35)/GalaSoft.MvvmLight.Extras (NET35).csproj +++ b/GalaSoft.MvvmLight/GalaSoft.MvvmLight.Extras (NET35)/GalaSoft.MvvmLight.Extras (NET35).csproj @@ -57,6 +57,9 @@ + + Helpers\DesignerPlatformLibrary.cs + Ioc\ISimpleIoc.cs diff --git a/GalaSoft.MvvmLight/GalaSoft.MvvmLight.Extras (NET4)/GalaSoft.MvvmLight.Extras (NET4).csproj b/GalaSoft.MvvmLight/GalaSoft.MvvmLight.Extras (NET4)/GalaSoft.MvvmLight.Extras (NET4).csproj index ea53ed4..67a5ae8 100644 --- a/GalaSoft.MvvmLight/GalaSoft.MvvmLight.Extras (NET4)/GalaSoft.MvvmLight.Extras (NET4).csproj +++ b/GalaSoft.MvvmLight/GalaSoft.MvvmLight.Extras (NET4)/GalaSoft.MvvmLight.Extras (NET4).csproj @@ -60,6 +60,9 @@ + + Helpers\DesignerPlatformLibrary.cs + Ioc\ISimpleIoc.cs diff --git a/GalaSoft.MvvmLight/GalaSoft.MvvmLight.Extras (PCL)/GalaSoft.MvvmLight.Extras (PCL).csproj b/GalaSoft.MvvmLight/GalaSoft.MvvmLight.Extras (PCL)/GalaSoft.MvvmLight.Extras (PCL).csproj index 274e389..deea15f 100644 --- a/GalaSoft.MvvmLight/GalaSoft.MvvmLight.Extras (PCL)/GalaSoft.MvvmLight.Extras (PCL).csproj +++ b/GalaSoft.MvvmLight/GalaSoft.MvvmLight.Extras (PCL)/GalaSoft.MvvmLight.Extras (PCL).csproj @@ -21,7 +21,7 @@ full false bin\Debug\ - TRACE;DEBUG;NETFX_CORE + TRACE;DEBUG;NETFX_CORE;CMNATTR;PORTABLE prompt 4 bin\Debug\GalaSoft.MvvmLight.Extras.XML @@ -30,7 +30,7 @@ pdbonly true bin\Release\ - TRACE;NETFX_CORE + TRACE;NETFX_CORE;CMNATTR;PORTABLE prompt 4 bin\Release\GalaSoft.MvvmLight.Extras.XML @@ -50,6 +50,9 @@ + + Helpers\DesignerPlatformLibrary.cs + diff --git a/GalaSoft.MvvmLight/GalaSoft.MvvmLight.Extras (SL5)/GalaSoft.MvvmLight.Extras (SL5).csproj b/GalaSoft.MvvmLight/GalaSoft.MvvmLight.Extras (SL5)/GalaSoft.MvvmLight.Extras (SL5).csproj index e450abd..8b9c68e 100644 --- a/GalaSoft.MvvmLight/GalaSoft.MvvmLight.Extras (SL5)/GalaSoft.MvvmLight.Extras (SL5).csproj +++ b/GalaSoft.MvvmLight/GalaSoft.MvvmLight.Extras (SL5)/GalaSoft.MvvmLight.Extras (SL5).csproj @@ -71,6 +71,9 @@ + + Helpers\DesignerPlatformLibrary.cs + Ioc\ISimpleIoc.cs diff --git a/GalaSoft.MvvmLight/GalaSoft.MvvmLight.Platform (Android)/Resources/Resource.Designer.cs b/GalaSoft.MvvmLight/GalaSoft.MvvmLight.Platform (Android)/Resources/Resource.Designer.cs index 2a6e7a6..e69de29 100644 --- a/GalaSoft.MvvmLight/GalaSoft.MvvmLight.Platform (Android)/Resources/Resource.Designer.cs +++ b/GalaSoft.MvvmLight/GalaSoft.MvvmLight.Platform (Android)/Resources/Resource.Designer.cs @@ -1,60 +0,0 @@ -#pragma warning disable 1591 -//------------------------------------------------------------------------------ -// -// This code was generated by a tool. -// Runtime Version:4.0.30319.42000 -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -[assembly: global::Android.Runtime.ResourceDesignerAttribute("GalaSoft.MvvmLight.Resource", IsApplication=false)] - -namespace GalaSoft.MvvmLight -{ - - - [System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Android.Build.Tasks", "1.0.0.0")] - public partial class Resource - { - - static Resource() - { - global::Android.Runtime.ResourceIdManager.UpdateIdValues(); - } - - public partial class Attribute - { - - static Attribute() - { - global::Android.Runtime.ResourceIdManager.UpdateIdValues(); - } - - private Attribute() - { - } - } - - public partial class String - { - - // aapt resource value: 0x7f020001 - public static int ApplicationName = 2130837505; - - // aapt resource value: 0x7f020000 - public static int Hello = 2130837504; - - static String() - { - global::Android.Runtime.ResourceIdManager.UpdateIdValues(); - } - - private String() - { - } - } - } -} -#pragma warning restore 1591 diff --git a/GalaSoft.MvvmLight/Tests/GalaSoft.MvvmLight.Test (STD10)/obj/Release/TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs b/GalaSoft.MvvmLight/Tests/GalaSoft.MvvmLight.Test (STD10)/obj/Release/TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs new file mode 100644 index 0000000..e69de29