-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
ConstraintExpression.cs
70 lines (61 loc) · 2.27 KB
/
ConstraintExpression.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
#nullable disable
using System;
using System.Linq;
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Maui.Controls.Internals;
using Microsoft.Maui.Controls.Xaml;
namespace Microsoft.Maui.Controls.Compatibility
{
[RequireService([typeof(IReferenceProvider), typeof(IProvideValueTarget)])]
public class ConstraintExpression : IMarkupExtension<Constraint>
{
public ConstraintExpression()
{
Factor = 1.0;
}
public double Constant { get; set; }
public string ElementName { get; set; }
public double Factor { get; set; }
public string Property { get; set; }
public ConstraintType Type { get; set; }
object IMarkupExtension.ProvideValue(IServiceProvider serviceProvider)
{
return (this as IMarkupExtension<Constraint>).ProvideValue(serviceProvider);
}
public Constraint ProvideValue(IServiceProvider serviceProvider)
{
MethodInfo minfo;
switch (Type)
{
default:
case ConstraintType.RelativeToParent:
if (string.IsNullOrEmpty(Property))
return null;
minfo = typeof(View).GetProperties().First(pi => pi.Name == Property && pi.CanRead && pi.GetMethod.IsPublic).GetMethod;
return Constraint.RelativeToParent(p => (double)minfo.Invoke(p, Array.Empty<object>()) * Factor + Constant);
case ConstraintType.Constant:
return Constraint.Constant(Constant);
case ConstraintType.RelativeToView:
if (string.IsNullOrEmpty(Property))
return null;
if (string.IsNullOrEmpty(ElementName))
return null;
minfo = typeof(View).GetProperties().First(pi => pi.Name == Property && pi.CanRead && pi.GetMethod.IsPublic).GetMethod;
var referenceProvider = serviceProvider.GetService<IReferenceProvider>();
View view;
if (referenceProvider != null)
view = (View)referenceProvider.FindByName(ElementName);
else
{ //legacy path
var valueProvider = serviceProvider.GetService<IProvideValueTarget>();
if (valueProvider == null || !(valueProvider.TargetObject is INameScope))
return null;
view = ((INameScope)valueProvider.TargetObject).FindByName<View>(ElementName);
}
return Constraint.RelativeToView(view, delegate (RelativeLayout p, View v)
{ return (double)minfo.Invoke(v, Array.Empty<object>()) * Factor + Constant; });
}
}
}
}