Refactoring design mode detection.
This commit is contained in:
Родитель
2c435dfc32
Коммит
b1a6b1df29
|
@ -56,6 +56,9 @@
|
|||
<Compile Include="..\GalaSoft.MvvmLight %28PCL%29\Command\RelayCommandGeneric.cs">
|
||||
<Link>Command\RelayCommandGeneric.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\GalaSoft.MvvmLight %28PCL%29\Helpers\DesignerPlatformLibrary.cs">
|
||||
<Link>Helpers\DesignerPlatformLibrary.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\GalaSoft.MvvmLight %28PCL%29\Helpers\IExecuteWithObject.cs">
|
||||
<Link>Helpers\IExecuteWithObject.cs</Link>
|
||||
</Compile>
|
||||
|
|
|
@ -58,6 +58,9 @@
|
|||
<Compile Include="..\GalaSoft.MvvmLight %28PCL%29\Command\RelayCommandGeneric.cs">
|
||||
<Link>Command\RelayCommandGeneric.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\GalaSoft.MvvmLight %28PCL%29\Helpers\DesignerPlatformLibrary.cs">
|
||||
<Link>Helpers\DesignerPlatformLibrary.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\GalaSoft.MvvmLight %28PCL%29\Helpers\IExecuteWithObject.cs">
|
||||
<Link>Helpers\IExecuteWithObject.cs</Link>
|
||||
</Compile>
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
|
||||
/// <summary>
|
||||
|
@ -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
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets an instance of a <see cref="IMessenger" /> used to
|
||||
/// broadcast messages to other objects. If null, this class will
|
||||
|
|
|
@ -70,6 +70,9 @@
|
|||
<Compile Include="..\GalaSoft.MvvmLight %28PCL%29\Command\RelayCommandGeneric.cs">
|
||||
<Link>Command\RelayCommandGeneric.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\GalaSoft.MvvmLight %28PCL%29\Helpers\DesignerPlatformLibrary.cs">
|
||||
<Link>Helpers\DesignerPlatformLibrary.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\GalaSoft.MvvmLight %28PCL%29\Helpers\IExecuteWithObject.cs">
|
||||
<Link>Helpers\IExecuteWithObject.cs</Link>
|
||||
</Compile>
|
||||
|
|
|
@ -57,6 +57,9 @@
|
|||
<Reference Include="WindowsBase" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="..\GalaSoft.MvvmLight %28PCL%29\Helpers\DesignerPlatformLibrary.cs">
|
||||
<Link>Helpers\DesignerPlatformLibrary.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\GalaSoft.MvvmLight.Extras %28PCL%29\Ioc\ISimpleIoc.cs">
|
||||
<Link>Ioc\ISimpleIoc.cs</Link>
|
||||
</Compile>
|
||||
|
|
|
@ -60,6 +60,9 @@
|
|||
<Reference Include="WindowsBase" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="..\GalaSoft.MvvmLight %28PCL%29\Helpers\DesignerPlatformLibrary.cs">
|
||||
<Link>Helpers\DesignerPlatformLibrary.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\GalaSoft.MvvmLight.Extras %28PCL%29\Ioc\ISimpleIoc.cs">
|
||||
<Link>Ioc\ISimpleIoc.cs</Link>
|
||||
</Compile>
|
||||
|
|
|
@ -21,7 +21,7 @@
|
|||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>TRACE;DEBUG;NETFX_CORE</DefineConstants>
|
||||
<DefineConstants>TRACE;DEBUG;NETFX_CORE;CMNATTR;PORTABLE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<DocumentationFile>bin\Debug\GalaSoft.MvvmLight.Extras.XML</DocumentationFile>
|
||||
|
@ -30,7 +30,7 @@
|
|||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE;NETFX_CORE</DefineConstants>
|
||||
<DefineConstants>TRACE;NETFX_CORE;CMNATTR;PORTABLE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<DocumentationFile>bin\Release\GalaSoft.MvvmLight.Extras.XML</DocumentationFile>
|
||||
|
@ -50,6 +50,9 @@
|
|||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="..\GalaSoft.MvvmLight %28PCL%29\Helpers\DesignerPlatformLibrary.cs">
|
||||
<Link>Helpers\DesignerPlatformLibrary.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Ioc\ISimpleIoc.cs" />
|
||||
<Compile Include="Ioc\PreferredConstructor.cs" />
|
||||
|
|
|
@ -71,6 +71,9 @@
|
|||
<Reference Include="System.Windows.Browser" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="..\GalaSoft.MvvmLight %28PCL%29\Helpers\DesignerPlatformLibrary.cs">
|
||||
<Link>Helpers\DesignerPlatformLibrary.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\GalaSoft.MvvmLight.Extras %28PCL%29\Ioc\ISimpleIoc.cs">
|
||||
<Link>Ioc\ISimpleIoc.cs</Link>
|
||||
</Compile>
|
||||
|
|
|
@ -1,60 +0,0 @@
|
|||
#pragma warning disable 1591
|
||||
//------------------------------------------------------------------------------
|
||||
// <auto-generated>
|
||||
// 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.
|
||||
// </auto-generated>
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
[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
|
Загрузка…
Ссылка в новой задаче