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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
using MultiLayout.UiModule;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Navigation;
using Unity;
namespace MultiLayout
{
public class FlyLayoutManager : INotifyPropertyChanged
{
public static NavigationService NavigationService
{
get {
return Application.Current.Properties["NavigationService"] as NavigationService;
}
set {
Application.Current.Properties["NavigationService"] = value;
}
}
NLog.ILogger logger = NLog.LogManager.GetCurrentClassLogger();
/// <summary>
/// 本地模块
/// </summary>
public List<IUiModule2> UIModules = new List<IUiModule2>();
public IUnityContainer uiContainer;
public IUnityContainer container;
LoadingProgress lp;
public FlyLayout layout;
public List<string> serviceNames;
List<IOnInit> onInits;
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// 当前被选中的设备
/// </summary>
public GageTabItem Selected { get; set; }
/// <summary>
/// 设备标签可见
/// </summary>
public bool IsGageTagVisible { get; set; } = true;
public bool IsInOption { get; set; }
public string GageTag
{
get {
if (Selected != null)
return Selected.Title;
else
return null;
}
}
public bool IsErrMsgVisable => !string.IsNullOrEmpty(ErrMsg);
/// <summary>
/// 错误信息
/// </summary>
public string ErrMsg { get; set; }
public Action ErrMsgClick;
/// <summary>
///
/// </summary>
public FlyLayoutManager()
{
}
/// <summary>
///
/// </summary>
/// <param name="layout"></param>
/// <param name="uiContainer"></param>
/// <param name="serviceContainer"></param>
public void Init(
FlyLayout layout,
IUnityContainer container,
LoadingProgress lp
)
{
//从根容器获取 UiModule 容器
this.container = container;
this.layout = layout;
this.uiContainer = container.Resolve<IUnityContainer>("uiModule.ui");
this.lp = lp;
serviceNames = (from r in this.container.Registrations
where r.RegisteredType == typeof(IUnityContainer)
&& !string.IsNullOrEmpty(r.Name)
&& Regex.IsMatch(r.Name, @"\w+\.service")
select r.Name).ToList();
//找到所有的onInit
onInits = new List<IOnInit>();
onInits.AddRange(container.ResolveAll<IOnInit>());
//找到所有的服务中的onInit
foreach (var serviceName in serviceNames)
{
var serviceContainer = container.Resolve<IUnityContainer>(serviceName);
var serviceOnInits = serviceContainer.ResolveAll<IOnInit>();
foreach (var onInit in serviceOnInits)
{
if (!onInits.Contains(onInit))
{
onInits.Add(onInit);
}
}
}
onInits.Sort((o1, o2) =>
{
return o1.Level.CompareTo(o2.Level);
});
}
/// <summary>
/// 加载模块
/// </summary>
public async Task<Page> LoadModule()
{
//加载全部 ui module
UIModules.AddRange(uiContainer.ResolveAll<IUiModule2>());
await Task.Delay(10);
lp.Progress += 10;
//加载尺寸,位置
int SH = (int)SystemParameters.WorkArea.Height;
int SW = (int)SystemParameters.WorkArea.Width;
int x = layout.Rect.X;
if ((x < 0) || (x >= SW))
x = 0;
int y = layout.Rect.Y;
if ((y < 0) || (y >= SH))
y = 0;
int w = layout.Rect.Width;
if ((w < 100) || (w > SW))
w = SW;
int h = layout.Rect.Height;
if ((h < 100)|| (h > SH))
h = SH;
Application.Current.MainWindow.Left = x;
Application.Current.MainWindow.Top = y;
Application.Current.MainWindow.Width = w;
Application.Current.MainWindow.Height = h;
Application.Current.MainWindow.WindowState = layout.WindowState;
UserControl userControl;
if (layout.GageTabItems == null || layout.GageTabItems.Count()==0)
{
//一个设备也没有,返回单设备界面!!!
UcSingleGage ucSingleGage = new UcSingleGage();
ucSingleGage.Init(this);
userControl = ucSingleGage;
}
else if (layout.GageTabItems.Count() == 1)
{
//只有一个设备,返回单设备界面!!!
userControl = await LoadModule_single();
}
else
{
userControl = await LoadModule_multi();
}
Page pg = new Page();
pg.Content = userControl;
return pg;
}
async Task<UserControl> LoadModule_single()
{
var gageTabItem = layout.GageTabItems.First();
//获取容器
IUnityContainer container = null;
try
{
container = this.container.Resolve<IUnityContainer>(gageTabItem.ServiceContainerName);
}
catch (Exception e)
{
//失败了,那就不显示!!!!
logger.Error(e, $"创建失败 gageTabItem({gageTabItem.Title})");
//给它总容器
container = this.container;
}
//把不支持的模块删除
gageTabItem.Components.RemoveAll(c => !UIModules.Any(uiModule => c.Module == uiModule.GetType().FullName));
//把布局内不存在于 Components 的删除
gageTabItem.MenuItems.RemoveAll(id => !gageTabItem.Components.Any(c => c.ID == id));
gageTabItem.DynAreaItems.RemoveAll(id => !gageTabItem.Components.Any(c => c.ID == id));
foreach (var tabitem in gageTabItem.Items)
tabitem.Graphs.RemoveAll(gc => !gageTabItem.Components.Any(c => c.ID == gc.ID));
await Task.Delay(10);
lp.Progress += 10;
//创建页面
UcSingleGage ucMainGage = new UcSingleGage();
ucMainGage.Init(this);
gageTabItem.TabControl = ucMainGage.tabControl_graph;
//初始化每个区
InitializeComponent_graph(gageTabItem, ucMainGage.tabControl_graph);
await Task.Delay(10);
lp.Progress += 10;
InitializeComponent_dynarea(gageTabItem, ucMainGage.stackpanel_dynarea);
await Task.Delay(10);
lp.Progress += 10;
InitializeComponent_menu(gageTabItem, ucMainGage.stackpanel_menu);
await Task.Delay(10);
lp.Progress += 10;
return ucMainGage;
}
async Task<UserControl> LoadModule_multi()
{
ucMultiGage = new UcMultiGage();
ucMultiGage.Init(this);
//注册导航器
NavigationService = ucMultiGage.NavigationService;
NavigationService.LoadCompleted += NavigationService_LoadCompleted;
var tabControl_gage = ucMultiGage.TabControl;
tabControl_gage.Items.Clear();
foreach (var gageTabItem in layout.GageTabItems)
{
//获取容器
IUnityContainer container = null;
try
{
container = this.container.Resolve<IUnityContainer>(gageTabItem.ServiceContainerName);
}
catch (Exception e)
{
//失败了,使用总容器替换,不画了
logger.Error(e, $"创建失败 gageTabItem({gageTabItem.Title})");
continue;
}
//把不支持的模块删除
gageTabItem.Components.RemoveAll(c => !UIModules.Any(uiModule => c.Module == uiModule.GetType().FullName));
//把布局内不存在于 Components 的删除
gageTabItem.MenuItems.RemoveAll(id => !gageTabItem.Components.Any(c => c.ID == id));
gageTabItem.DynAreaItems.RemoveAll(id => !gageTabItem.Components.Any(c => c.ID == id));
foreach (var tabitem in gageTabItem.Items)
tabitem.Graphs.RemoveAll(gc => !gageTabItem.Components.Any(c => c.ID == gc.ID));
await Task.Delay(10);
lp.Progress += 10;
//创建页面
TabItem tabItem = new TabItem();
tabItem.Header = gageTabItem.Title;
UcSingleGage ucMainGage = new UcSingleGage();
ucMainGage.Init(this);
tabItem.Content = ucMainGage;
gageTabItem.TabControl = ucMainGage.tabControl_graph;
//初始化每个区
InitializeComponent_graph(gageTabItem, ucMainGage.tabControl_graph);
await Task.Delay(10);
lp.Progress += 10;
InitializeComponent_dynarea(gageTabItem, ucMainGage.stackpanel_dynarea);
await Task.Delay(10);
lp.Progress += 10;
InitializeComponent_menu(gageTabItem, ucMainGage.stackpanel_menu);
await Task.Delay(10);
lp.Progress += 10;
tabControl_gage.Items.Add(tabItem);
}
TabControl_gage_SelectionChanged(tabControl_gage, null);
tabControl_gage.SelectionChanged += TabControl_gage_SelectionChanged;
return ucMultiGage;
}
public async Task OnPreInit()
{
//运行全部onInit
foreach (var onInit in onInits)
{
if (onInit.Level <= 0 )
{
onInit.OnInit();
lp.Progress += 1;
await Task.Delay(2);
}
}
}
public async Task OnInit()
{
//运行全部onInit
foreach (var onInit in onInits)
{
if (onInit.Level > 0)
{
onInit.OnInit();
lp.Progress += 1;
await Task.Delay(2);
}
}
}
UcMultiGage ucMultiGage;
private void NavigationService_LoadCompleted(object sender, NavigationEventArgs e)
{
if (ucMultiGage.InMainPage)
{
IsGageTagVisible = false;
}
else if (IsInOption)
{
IsGageTagVisible = false;
}
else
{
IsGageTagVisible = true;
}
}
private void TabControl_gage_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var tabControl_gage = sender as TabControl;
if (tabControl_gage.SelectedIndex >= 0)
{
if(tabControl_gage.SelectedIndex>=0 && tabControl_gage.SelectedIndex< layout.GageTabItems.Count())
Selected = layout.GageTabItems[tabControl_gage.SelectedIndex];
}
}
void InitializeComponent_graph(GageTabItem gageTabItem, TabControl tabControl)
{
tabControl.Items.Clear();
foreach (GraphTabItem item in gageTabItem.Items)
{
//grid
Grid grid = new Grid();
for (int i = 0; i < item.ColumnCount; i++)
{
ColumnDefinition cd = new ColumnDefinition();
if (item.ColumnDefinitions.Count() > i)
{
if (item.ColumnDefinitions[i])
cd.Width = GridLength.Auto;
}
grid.ColumnDefinitions.Add(cd);
}
for (int i = 0; i < item.RowCount; i++)
{
RowDefinition rd = new RowDefinition();
if (item.RowDefinitions.Count() > i)
{
if (item.RowDefinitions[i])
rd.Height = GridLength.Auto;
}
grid.RowDefinitions.Add(rd);
}
//graph
foreach (GraphComponent gp in item.Graphs)
{
var flyComponent = gageTabItem.Components.Find(c => c.ID == gp.ID);
var uiModule = GetUIModule(flyComponent.Module);
try
{
var container = this.container.Resolve<IUnityContainer>(flyComponent.ServiceContainerName);
Frame frame = new Frame();
frame.SetValue(Frame.MarginProperty, new Thickness(2));
frame.Content = uiModule.GetComponent(gp.ID, container);
frame.DataContext = gp;
frame.SetValue(Grid.ColumnProperty,gp.Rect.X);
frame.SetValue(Grid.RowProperty, gp.Rect.Y);
frame.SetValue(Grid.ColumnSpanProperty, gp.Rect.Width);
frame.SetValue(Grid.RowSpanProperty, gp.Rect.Height);
grid.Children.Add(frame);
}
catch(Exception e)
{
//失败了,那就不显示!!!!
logger.Error(e, $"创建失败 uiModule({flyComponent.Module})");
}
}
TabItem tabitem = new TabItem()
{
Header = item.Header
};
tabitem.Content = grid;
tabControl.Items.Add(tabitem);
}
}
void InitializeComponent_dynarea(GageTabItem gageTabItem, StackPanel stackpanel)
{
stackpanel.Children.Clear();
foreach (var id in gageTabItem.DynAreaItems)
{
var flyComponent = gageTabItem.Components.Find(c => c.ID == id);
var uiModule = GetUIModule(flyComponent.Module);
try
{
var container = this.container.Resolve<IUnityContainer>(flyComponent.ServiceContainerName);
Frame frame = new Frame();
frame.Content = uiModule.GetComponent(id, container);
stackpanel.Children.Add(frame);
}
catch (Exception e)
{
//失败了,那就不显示!!!!
logger.Error(e, $"创建失败 uiModule({flyComponent.Module})");
}
}
}
void InitializeComponent_menu(GageTabItem gageTabItem, StackPanel stackpanel)
{
stackpanel.Children.Clear();
foreach (var id in gageTabItem.MenuItems)
{
var flyComponent = gageTabItem.Components.Find(c => c.ID == id);
var uiModule = GetUIModule(flyComponent.Module);
try
{
var container = this.container.Resolve<IUnityContainer>(flyComponent.ServiceContainerName);
Frame frame = new Frame();
frame.Content = uiModule.GetComponent(id, container);
stackpanel.Children.Add(frame);
}
catch (Exception e)
{
//失败了,那就不显示!!!!
logger.Error(e, $"创建失败 uiModule({flyComponent.Module})");
}
}
}
IUiModule2 GetUIModule(string moduleName)
{
return UIModules.Find(_uiModule => _uiModule.GetType().FullName == moduleName);
}
/// <summary>
/// 通知全部模块,告诉它们现在正在使用的控件,保存多余的参数就删除。
/// </summary>
public void MatchParam()
{
var components = new List<FlyComponent>();
foreach (var gageTabItem in layout.GageTabItems)
{
if(gageTabItem.Components!=null)
components.AddRange(gageTabItem.Components);
}
Dictionary<string, List<int>> modules = new Dictionary<string, List<int>>();
foreach (var c in components)
{
if (modules.ContainsKey(c.Module))
modules[c.Module].Add(c.ID);
else
modules.Add(c.Module, new List<int>(new int[] { c.ID }));
}
foreach (var m in modules)
{
var uiModule = GetUIModule(m.Key);
if(uiModule!=null)//不应该有null,之前都删除了
{
uiModule.MatchParam(m.Value.ToArray());
}
}
}
}
}