Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SwitchView.cs
1using System;
3using System.Linq;
4using Microsoft.Maui.Controls;
5
7{
12 internal class SwitchCaseView<T> : ContentView where T : notnull
13 {
17 public static readonly BindableProperty CasesProperty = BindableProperty.Create(
18 nameof(Cases),
19 typeof(ICollection<CaseView<T>>),
20 typeof(SwitchCaseView<T>),
21 new List<CaseView<T>>(),
22 propertyChanged: SwitchChanged);
23
27 public static readonly BindableProperty DefaultProperty = BindableProperty.Create(
28 nameof(Default),
29 typeof(View),
30 typeof(SwitchCaseView<T>),
31 null,
32 propertyChanged: SwitchChanged);
33
37 public static readonly BindableProperty SwitchProperty = BindableProperty.Create(
38 nameof(Switch),
39 typeof(T),
40 typeof(SwitchCaseView<T>),
41 default(T),
42 propertyChanged: SwitchChanged);
43
51 private static void SwitchChanged(BindableObject bindable, object oldValue, object newValue)
52 {
53 SwitchCaseView<T> SwitchCaseView = (SwitchCaseView<T>)bindable;
54 // Attempt to find a matching case. If none is found, use the default view.
55 SwitchCaseView.Content = SwitchCaseView.Cases
56 .Where(x => x.Case.Equals(SwitchCaseView.Switch))
57 .Select(x => x.Content)
58 .SingleOrDefault() ?? SwitchCaseView.Default;
59 }
60
64 public T Switch
65 {
66 get => (T)this.GetValue(SwitchProperty);
67 set => this.SetValue(SwitchProperty, value);
68 }
69
73 public View Default
74 {
75 get => (View)this.GetValue(DefaultProperty);
76 set => this.SetValue(DefaultProperty, value);
77 }
78
82 public ICollection<CaseView<T>> Cases
83 {
84 get => (ICollection<CaseView<T>>)this.GetValue(CasesProperty);
85 set => this.SetValue(CasesProperty, value);
86 }
87 }
88
93 internal class CaseView<T> : ContentView
94 {
98 public static readonly BindableProperty CaseProperty = BindableProperty.Create(
99 nameof(Case),
100 typeof(T),
101 typeof(CaseView<T>),
102 default(T));
103
107 public T Case
108 {
109 get => (T)this.GetValue(CaseProperty);
110 set => this.SetValue(CaseProperty, value);
111 }
112 }
113}