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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
|
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms.Integration;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Threading;
using Tango.SharedUI.Helpers;
namespace Tango.SharedUI.Controls
{
[ContentProperty(nameof(Elements))]
public class NavigationControl : UserControl
{
private Action _onCompleted;
#region Transition Types
public enum TransitionTypes
{
Fade,
Zoom,
Slide,
}
#endregion
#region Navigation Element
private class NavigationElement : ContentControl
{
public FrameworkElement Element
{
get { return (FrameworkElement)GetValue(ElementProperty); }
set { SetValue(ElementProperty, value); }
}
public static readonly DependencyProperty ElementProperty =
DependencyProperty.Register("Element", typeof(FrameworkElement), typeof(NavigationControl), new PropertyMetadata(null));
public ScaleTransform Scale
{
get { return (ScaleTransform)GetValue(ScaleProperty); }
set { SetValue(ScaleProperty, value); }
}
public static readonly DependencyProperty ScaleProperty =
DependencyProperty.Register("Scale", typeof(ScaleTransform), typeof(NavigationControl), new PropertyMetadata(null));
public RotateTransform Rotate
{
get { return (RotateTransform)GetValue(RotateProperty); }
set { SetValue(RotateProperty, value); }
}
public static readonly DependencyProperty RotateProperty =
DependencyProperty.Register("Rotate", typeof(RotateTransform), typeof(NavigationControl), new PropertyMetadata(null));
public TranslateTransform Translate
{
get { return (TranslateTransform)GetValue(TranslateProperty); }
set { SetValue(TranslateProperty, value); }
}
public static readonly DependencyProperty TranslateProperty =
DependencyProperty.Register("Translate", typeof(TranslateTransform), typeof(NavigationControl), new PropertyMetadata(null));
public NavigationElement()
{
Visibility = Visibility.Hidden;
Scale = new ScaleTransform(1, 1);
Rotate = new RotateTransform(0);
Translate = new TranslateTransform(0, 0);
TransformGroup group = new TransformGroup();
group.Children.Add(Scale);
group.Children.Add(Rotate);
group.Children.Add(Translate);
RenderTransformOrigin = new Point(0.5, 0.5);
RenderTransform = group;
}
public NavigationElement(FrameworkElement element) : this()
{
Element = element;
}
public void AnimateScale(DoubleAnimation animation)
{
Scale.BeginAnimation(ScaleTransform.ScaleXProperty, animation);
Scale.BeginAnimation(ScaleTransform.ScaleYProperty, animation);
}
public void AnimateTranslateX(DoubleAnimation animation)
{
Translate.BeginAnimation(TranslateTransform.XProperty, animation);
}
public void AnimateOpacity(DoubleAnimation animation)
{
BeginAnimation(OpacityProperty, animation);
}
public void Reset()
{
Scale.BeginAnimation(ScaleTransform.ScaleXProperty, null);
Scale.BeginAnimation(ScaleTransform.ScaleYProperty, null);
Rotate.BeginAnimation(RotateTransform.AngleProperty, null);
Translate.BeginAnimation(TranslateTransform.XProperty, null);
Translate.BeginAnimation(TranslateTransform.YProperty, null);
Opacity = 1;
}
public void Activate()
{
if (Content != Element)
{
Content = Element;
}
Visibility = Visibility.Visible;
}
public void Deactivate(bool detach)
{
if (detach)
{
Content = null;
}
Visibility = Visibility.Hidden;
}
}
#endregion
#region INavigationView
public interface INavigationView
{
void OnNavigatedTo();
void OnNavigatedFrom();
}
#endregion
#region INavigationViewModel
/// <summary>
/// Represents a view data context which can be notified when its view is being navigated in or out.
/// </summary>
public interface INavigationViewModel
{
/// <summary>
/// Called when the navigation system has navigated to this VM view.
/// </summary>
void OnNavigatedTo();
/// <summary>
/// Called when the navigation system has navigated from this VM view.
/// </summary>
void OnNavigatedFrom();
}
#endregion
private Grid _grid;
private bool _loaded;
#region Properties
public ObservableCollection<FrameworkElement> Elements
{
get { return (ObservableCollection<FrameworkElement>)GetValue(ElementsProperty); }
set { SetValue(ElementsProperty, value); }
}
public static readonly DependencyProperty ElementsProperty =
DependencyProperty.Register("Elements", typeof(ObservableCollection<FrameworkElement>), typeof(NavigationControl), new PropertyMetadata(null, (d, e) => (d as NavigationControl).OnElementsChanged()));
public FrameworkElement SelectedElement
{
get { return (FrameworkElement)GetValue(SelectedElementProperty); }
set { SetValue(SelectedElementProperty, value); }
}
public static readonly DependencyProperty SelectedElementProperty =
DependencyProperty.Register("SelectedElement", typeof(FrameworkElement), typeof(NavigationControl), new PropertyMetadata(null, (d, e) => (d as NavigationControl).OnSelectedElementChanged(e.OldValue as FrameworkElement, e.NewValue as FrameworkElement)));
public TransitionTypes TransitionType
{
get { return (TransitionTypes)GetValue(TransitionTypeProperty); }
set { SetValue(TransitionTypeProperty, value); }
}
public static readonly DependencyProperty TransitionTypeProperty =
DependencyProperty.Register("TransitionType", typeof(TransitionTypes), typeof(NavigationControl), new PropertyMetadata(TransitionTypes.Zoom));
public bool TransitionAlwaysFades
{
get { return (bool)GetValue(TransitionAlwaysFadesProperty); }
set { SetValue(TransitionAlwaysFadesProperty, value); }
}
public static readonly DependencyProperty TransitionAlwaysFadesProperty =
DependencyProperty.Register("TransitionAlwaysFades", typeof(bool), typeof(NavigationControl), new PropertyMetadata(false));
public Duration TransitionDuration
{
get { return (Duration)GetValue(TransitionDurationProperty); }
set { SetValue(TransitionDurationProperty, value); }
}
public static readonly DependencyProperty TransitionDurationProperty =
DependencyProperty.Register("TransitionDuration", typeof(Duration), typeof(NavigationControl), new PropertyMetadata(new Duration(TimeSpan.FromSeconds(0.5))));
public bool KeepElementsAttached
{
get { return (bool)GetValue(KeepElementsAttachedProperty); }
set { SetValue(KeepElementsAttachedProperty, value); }
}
public static readonly DependencyProperty KeepElementsAttachedProperty =
DependencyProperty.Register("KeepElementsAttached", typeof(bool), typeof(NavigationControl), new PropertyMetadata(false));
public int SelectedIndex
{
get { return (int)GetValue(SelectedIndexProperty); }
set { SetValue(SelectedIndexProperty, value); }
}
// Using a DependencyProperty as the backing store for SelectedIndex. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SelectedIndexProperty =
DependencyProperty.Register("SelectedIndex", typeof(int), typeof(NavigationControl), new PropertyMetadata(0, (d, e) => (d as NavigationControl).OnSelectedIndexChanged()));
public bool UseDefferedRendering
{
get { return (bool)GetValue(UseDefferedRenderingProperty); }
set { SetValue(UseDefferedRenderingProperty, value); }
}
public static readonly DependencyProperty UseDefferedRenderingProperty =
DependencyProperty.Register("UseDefferedRendering", typeof(bool), typeof(NavigationControl), new PropertyMetadata(false));
private void OnSelectedIndexChanged()
{
SelectedElement = Elements[SelectedIndex > Elements.Count - 1 ? 0 : SelectedIndex];
}
#endregion
#region Attached Properties
#region NavigationName
/// <summary>
/// Determines the element navigation name.
/// </summary>
public static readonly DependencyProperty NavigationName =
DependencyProperty.RegisterAttached("NavigationName",
typeof(String), typeof(NavigationControl),
new FrameworkPropertyMetadata(""));
/// <summary>
/// Sets the name of the navigation.
/// </summary>
/// <param name="element">The element.</param>
/// <param name="value">The value.</param>
public static void SetNavigationName(FrameworkElement element, String value)
{
element.SetValue(NavigationName, value);
}
/// <summary>
/// Gets the name of the navigation.
/// </summary>
/// <param name="element">The element.</param>
/// <returns></returns>
public static String GetNavigationName(FrameworkElement element)
{
return element.GetValue(NavigationName).ToStringSafe();
}
#endregion
#endregion
#region Constructors
public NavigationControl()
{
ClipToBounds = true;
_grid = new Grid();
Content = _grid;
Elements = new ObservableCollection<FrameworkElement>();
Loaded += NavigationControl_Loaded;
}
#endregion
#region Event Handlers
private void NavigationControl_Loaded(object sender, RoutedEventArgs e)
{
if (!_loaded)
{
_loaded = true;
if (Elements != null)
{
//foreach (var element in Elements.Where(x => x != SelectedElement))
//{
// LoadVisual(element);
//}
SelectedElement = Elements.FirstOrDefault();
}
}
}
private void Elements_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
SyncElements();
}
#endregion
#region Private Methods
private void OnElementsChanged()
{
if (Elements != null)
{
Elements.CollectionChanged -= Elements_CollectionChanged;
Elements.CollectionChanged += Elements_CollectionChanged;
}
SyncElements();
}
private void SyncElements()
{
if (Elements == null)
{
_grid.Children.Clear();
}
else
{
var toRemove = _grid.Children.OfType<NavigationElement>().ToList().Where(x => !Elements.Contains(x.Element)).ToList();
var toAdd = Elements.Where(x => !_grid.Children.OfType<NavigationElement>().Select(y => y.Element).ToList().Contains(x)).ToList();
toRemove.ForEach(x => _grid.Children.Remove(x));
if (UseDefferedRendering)
{
foreach (var x in toAdd)
{
var navigationElement = new NavigationElement()
{
RenderTransformOrigin = new Point(0.5, 0.5),
Element = x,
Content = x,
};
_grid.Children.Add(navigationElement);
if (!KeepElementsAttached)
{
bool loaded = false;
navigationElement.Loaded += (_, __) =>
{
if (!loaded)
{
if (x != SelectedElement)
{
loaded = true;
navigationElement.Content = null;
}
}
};
}
}
}
else
{
toAdd.ForEach(x => _grid.Children.Add(
new NavigationElement()
{
RenderTransformOrigin = new Point(0.5, 0.5),
Element = x,
Content = KeepElementsAttached ? x : null,
}));
}
}
if (!Elements.Contains(SelectedElement))
{
SelectedElement = null;
}
}
private NavigationElement GetElementContainer(FrameworkElement element)
{
return _grid.Children.OfType<NavigationElement>().ToList().SingleOrDefault(x => x.Element == element);
}
private void Navigate(NavigationElement fromElement, NavigationElement toElement)
{
if (toElement == null || toElement == fromElement)
{
_onCompleted?.Invoke();
}
if (fromElement != null)
{
DoubleAnimation toAnimation = new DoubleAnimation();
toAnimation.Duration = TransitionDuration;
DoubleAnimation fromAnimation = new DoubleAnimation();
fromAnimation.Duration = TransitionDuration;
int fromIndex = Elements.IndexOf(fromElement.Element);
int toIndex = Elements.IndexOf(toElement.Element);
fromElement.Reset();
toElement.Reset();
fromAnimation.Completed += (_, __) =>
{
fromElement.Deactivate(!KeepElementsAttached);
};
bool completed = false;
toAnimation.Completed += (_, __) =>
{
if (!completed)
{
completed = true;
INavigationView fromNavigationView = fromElement.Element as INavigationView;
INavigationView toNavigationView = toElement.Element as INavigationView;
if (fromNavigationView != null)
{
fromNavigationView.OnNavigatedFrom();
}
if (toNavigationView != null)
{
toNavigationView.OnNavigatedTo();
}
INavigationViewModel fromVM = fromElement.Element.DataContext as INavigationViewModel;
INavigationViewModel toVM = toElement.Element.DataContext as INavigationViewModel;
if (fromVM != null && fromVM != toVM) fromVM.OnNavigatedFrom();
if (toVM != null) toVM.OnNavigatedTo();
_onCompleted?.Invoke();
}
};
switch (TransitionType)
{
case TransitionTypes.Fade:
fromAnimation.From = 1;
fromAnimation.To = 0;
toAnimation.From = 0;
toAnimation.To = 1;
fromElement.AnimateOpacity(fromAnimation);
toElement.AnimateOpacity(toAnimation);
break;
case TransitionTypes.Zoom:
fromAnimation.From = 1;
fromAnimation.To = 0;
toAnimation.From = 0;
toAnimation.To = 1;
fromElement.AnimateScale(fromAnimation);
toElement.AnimateScale(toAnimation);
break;
case TransitionTypes.Slide:
if (toIndex > fromIndex)
{
fromAnimation.From = 0;
fromAnimation.To = -ActualWidth;
toAnimation.From = ActualWidth;
toAnimation.To = 0;
}
else
{
fromAnimation.From = 0;
fromAnimation.To = ActualWidth;
toAnimation.From = -ActualWidth;
toAnimation.To = 0;
}
fromElement.AnimateTranslateX(fromAnimation);
toElement.AnimateTranslateX(toAnimation);
break;
}
if (TransitionAlwaysFades && TransitionType != TransitionTypes.Fade)
{
DoubleAnimation fromFadeAnimation = new DoubleAnimation();
fromFadeAnimation.From = 1;
fromFadeAnimation.To = 0;
fromFadeAnimation.Duration = TransitionDuration;
DoubleAnimation toFadeAnimation = new DoubleAnimation();
toFadeAnimation.From = 0;
toFadeAnimation.To = 1;
toFadeAnimation.Duration = TransitionDuration;
fromElement.AnimateOpacity(fromFadeAnimation);
toElement.AnimateOpacity(toFadeAnimation);
}
fromElement.Activate();
toElement.Activate();
}
else
{
toElement.Activate();
toElement.Reset();
INavigationViewModel toVM = toElement.Element.DataContext as INavigationViewModel;
if (toVM != null) toVM.OnNavigatedTo();
}
}
#endregion
#region Properties Changes
protected virtual void OnSelectedElementChanged(FrameworkElement fromElement, FrameworkElement toElement)
{
Navigate(GetElementContainer(fromElement), GetElementContainer(toElement));
}
#endregion
#region Public Methods
public void NavigateTo(FrameworkElement element)
{
var e = Elements.SingleOrDefault(x => x == element);
if (e != null)
{
SelectedElement = element;
}
}
public FrameworkElement NavigateTo(String navigationName)
{
return NavigateTo(navigationName, null);
}
public FrameworkElement NavigateTo(String navigationName, Action onCompleted = null)
{
_onCompleted = onCompleted;
var element = Elements.SingleOrDefault(x => GetNavigationName(x) == navigationName || x.GetType().Name == navigationName);
if (element != null)
{
if (SelectedElement == element)
{
_onCompleted?.Invoke();
}
else
{
SelectedElement = element;
}
}
return element;
}
public FrameworkElement GetElement(String navigationName)
{
return Elements.SingleOrDefault(x => GetNavigationName(x) == navigationName || x.GetType().Name == navigationName);
}
public FrameworkElement GetAndDetach(String navigationName)
{
var element = Elements.SingleOrDefault(x => GetNavigationName(x) == navigationName);
GetElementContainer(element).Deactivate(!KeepElementsAttached);
return element;
}
/// <remarks>
/// This method needs to be called in order for
// the element to print visibly at the correct size.
/// </remarks>
private static void LoadVisual(FrameworkElement element)
{
if (element.Parent == null)
{
try
{
System.Windows.Forms.UserControl controlContainer = new System.Windows.Forms.UserControl();
controlContainer.Width = 100;
controlContainer.Height = 100;
var host = new ElementHost() { Child = element, Dock = System.Windows.Forms.DockStyle.Fill };
controlContainer.Controls.Add(host);
IntPtr handle = controlContainer.Handle;
host.Child = null;
controlContainer.Dispose();
}
catch { }
}
}
#endregion
}
}
|