-
-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathSwitchCaseView.cs
46 lines (40 loc) · 1.7 KB
/
SwitchCaseView.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
namespace MauiConditionView;
public class SwitchCaseView<T> : ContentView
where T : notnull
{
public static readonly BindableProperty ConditionsProperty = BindableProperty.Create(nameof(Conditions), typeof(ICollection<CaseView<T>>), typeof(ConditionView), new List<CaseView<T>>(), propertyChanged:SwitchChanged);
public static readonly BindableProperty DefaultProperty = BindableProperty.Create(nameof(Default), typeof(View), typeof(ConditionView), propertyChanged: SwitchChanged);
public static readonly BindableProperty SwitchProperty = BindableProperty.Create(nameof(Switch), typeof(T), typeof(ConditionView), propertyChanged: SwitchChanged);
private static void SwitchChanged(BindableObject bindable, object oldvalue, object newvalue)
{
var switchCaseView = (SwitchCaseView<T>)bindable;
switchCaseView.Content = switchCaseView.Conditions
.Where(x => x.Case.Equals(switchCaseView.Switch))
.Select(x => x.Content)
.SingleOrDefault(switchCaseView.Default);
}
public T Switch
{
get => (T)GetValue(SwitchProperty);
set => SetValue(SwitchProperty, value);
}
public View? Default
{
get => (View?)GetValue(DefaultProperty);
set => SetValue(DefaultProperty, value);
}
public ICollection<CaseView<T>> Conditions
{
get => (ICollection<CaseView<T>>)GetValue(ConditionsProperty);
set => SetValue(ConditionsProperty, value);
}
}
public class CaseView<T> : ContentView
{
public static readonly BindableProperty CaseProperty = BindableProperty.Create(nameof(Case), typeof(T), typeof(CaseView<T>));
public T Case
{
get => (T)GetValue(CaseProperty);
set => SetValue(CaseProperty, value);
}
}