/
Bootstrapper.cs
165 lines (141 loc) · 4.45 KB
/
Bootstrapper.cs
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using System;
using System.Windows;
using System.Windows.Media;
using MaterialDesignColors;
using MaterialDesignThemes.Wpf;
using Speckle.Core.Logging;
using Speckle.DesktopUI.Streams;
using Speckle.DesktopUI.Utils;
using Stylet;
using Stylet.Xaml;
using StyletIoC;
namespace Speckle.DesktopUI
{
public class Bootstrapper : Bootstrapper<RootViewModel>
{
public ConnectorBindings Bindings = new DummyBindings();
private Window _rootWindow;
public Window RootWindow
{
get => _rootWindow ?? ( _rootWindow = ( Window ) RootViewModel.View );
}
protected override void OnStart()
{
base.OnStart();
Core.Logging.Setup.Init(Bindings.GetHostAppName());
InitializeMaterialDesign();
Stylet.Logging.LogManager.Enabled = true;
}
public void ShowRootView()
{
RootWindow.Show();
RootWindow.Activate();
}
public void CloseRootView()
{
RootViewModel.RequestClose();
}
protected override void ConfigureIoC(IStyletIoCBuilder builder)
{
base.ConfigureIoC(builder);
// Bind view model factory
builder.Bind<IViewModelFactory>().ToAbstractFactory();
// and factory for individual stream pages
builder.Bind<IStreamViewModelFactory>().ToAbstractFactory();
// and factory for dialog modals (eg create stream)
builder.Bind<IDialogFactory>().ToAbstractFactory();
// and factory for repositories
builder.Bind<StreamsRepository>().ToSelf();
// and finally the external bindings (eg from Revit, Rhino, etc)
builder.Bind<ConnectorBindings>().ToFactory(container =>
{
var bindings = Bindings;
container.BuildUp(bindings); // build with ioc to make use of injection
return bindings;
});
}
private void InitializeMaterialDesign()
{
// Create dummy objects to force the MaterialDesign assemblies to be loaded
// from this assembly
// https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/issues/1249
var card = new Card();
var hue = new Hue("Dummy", Colors.Black, Colors.White);
var behavior = new Microsoft.Xaml.Behaviors.Media.PlaySoundAction(); //force loading of behaviors reference
}
public override void Start(string[ ] args)
{
// stop. get help.
// this is getting triggered from _somewhere_ so I'm overriding it to prevent it from messing things up
}
public void Start(Application app)
{
OnStart();
ConfigureBootstrapper();
try
{
app.Resources.Add(View.ViewManagerResourceKey, GetInstance(typeof(IViewManager)));
}
catch ( Exception e )
{
// already been added somewhere...
}
Configure();
Launch();
OnLaunch();
}
public void SetParent(IntPtr parent)
{
var helper = new System.Windows.Interop.WindowInteropHelper(RootWindow);
helper.Owner = parent;
}
}
/// <summary>
/// Taken from stylet and modified to not use the application.
/// Added to your App.xaml, this is responsible for loading the Boostrapper you specify, and Stylet's other resources
/// </summary>
public class StyletAppLoader : ResourceDictionary
{
private readonly ResourceDictionary styletResourceDictionary;
/// <summary>
/// Initialises a new instance of the <see cref="ApplicationLoader"/> class
/// </summary>
public StyletAppLoader()
{
styletResourceDictionary = new ResourceDictionary()
{
Source = new Uri("pack://application:,,,/Stylet;component/Xaml/StyletResourceDictionary.xaml",
UriKind.Absolute)
};
LoadStyletResources = true;
}
private Bootstrapper _bootstrapper;
/// <summary>
/// Gets or sets the bootstrapper instance to use to start your application. This must be set.
/// </summary>
public Bootstrapper Bootstrapper
{
get => _bootstrapper;
set
{
_bootstrapper = value;
}
}
private bool _loadStyletResources;
/// <summary>
/// Gets or sets a value indicating whether to load Stylet's own resources (e.g. StyletConductorTabControl). Defaults to true.
/// </summary>
public bool LoadStyletResources
{
get => _loadStyletResources;
set
{
_loadStyletResources = value;
if ( _loadStyletResources )
MergedDictionaries.Add(styletResourceDictionary);
else
MergedDictionaries.Remove(styletResourceDictionary);
}
}
}
}