forked from reactiveui/ReactiveUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDesignModeDetector.cs
More file actions
55 lines (49 loc) · 2.6 KB
/
DesignModeDetector.cs
File metadata and controls
55 lines (49 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace ReactiveUI
{
public class DesignModeDetector
{
/// <summary>
/// Private constructor to prevent instantiation.
/// </summary>
private DesignModeDetector() { }
static bool? isInDesignMode;
/// <summary>
/// Determines is this application is currently running in a designer.
/// </summary>
/// <returns>true if in designer; otherwise false</returns>
public static bool IsInDesignMode()
{
if (!isInDesignMode.HasValue)
{
// Check WPF Design Mode
var type = Type.GetType("System.ComponentModel.DesignerProperties, PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35", false);
if (type != null) {
var mInfo = type.GetRuntimeMethods().FirstOrDefault(x => x.Name == "GetIsInDesignMode");
var dependencyObject = Type.GetType("System.Windows.DependencyObject, WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35", false);
if (dependencyObject != null) {
isInDesignMode = (bool)mInfo.Invoke(null, new object[] { Activator.CreateInstance(dependencyObject) });
}
} else if((type = Type.GetType("System.ComponentModel.DesignerProperties, System.Windows, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e", false)) != null) {
// Check Silverlight Design Mode
var mInfo = type.GetRuntimeMethods().FirstOrDefault(x => x.Name == "GetIsInDesignMode");
var dependencyObject = Type.GetType("System.Windows.Controls.Border, System.Windows, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e", false);
if (dependencyObject != null) {
isInDesignMode = (bool)mInfo.Invoke(null, new object[] { Activator.CreateInstance(dependencyObject) });
}
} else if ((type = Type.GetType("Windows.ApplicationModel.DesignMode, Windows, ContentType=WindowsRuntime", false)) != null) {
// check WinRT next
isInDesignMode = (bool)type.GetRuntimeProperty("DesignModeEnabled").GetMethod.Invoke(null, null);
} else {
isInDesignMode = false;
}
}
return isInDesignMode.GetValueOrDefault(false);
}
}
}