This commit is contained in:
James Clancey 2019-06-19 11:46:05 -08:00
Родитель 973a06ca44
Коммит 7894564b33
3 изменённых файлов: 36 добавлений и 20 удалений

Просмотреть файл

@ -14,20 +14,20 @@ namespace HotForms.Sample {
get => GetProperty<string> ();
set => SetProperty (value);
}
public int ClickCount {
get => GetProperty<int> ();
set => SetProperty (value);
}
}
[State]
readonly MyBindingObject state;
readonly State<int> clickCount = new State<int> (1);
readonly State<bool> bar = new State<bool> ();
public MyDynamicStatePage()
{
state = new MyBindingObject {
Text = "Foo",
CanEdit = true,
ClickCount = 1,
};
}
@ -42,12 +42,15 @@ namespace HotForms.Sample {
new Label {Text = state.Text},
new Button{Text = "Toggle Entry/Label", OnClick = ()=> state.CanEdit = !state.CanEdit},
new Button{Text = "Update Text", OnClick = ()=>{
state.Text = $"Click Count: {state.ClickCount++}";
state.Text = $"Click Count: {clickCount.Value++}";
}
}
};
}
//public class MainPage : StateHotPage {
// protected override void CreateState (dynamic state)
// {

Просмотреть файл

@ -51,23 +51,25 @@ namespace HotForms {
}
bool hasChecked = false;
static Assembly hotFormsAssembly = typeof (BindingObject).Assembly;
void CheckForStateAttributes()
{
if (hasChecked)
return;
var type = this.GetType ();
var properties = type.GetProperties (BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance).
Where (x => Attribute.IsDefined (x, typeof (StateAttribute))).ToList ();
//var properties = type.GetProperties (BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance).
// Where (x => Attribute.IsDefined (x, typeof (StateAttribute))).ToList ();
var fields = type.GetFields (BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance).
Where (x => Attribute.IsDefined (x, typeof (StateAttribute))).ToList ();
if (properties.Any()) {
foreach(var prop in properties) {
var child = prop.GetValue (this) as BindingObject;
if (child != null) {
SetProperty(child,prop.Name);
}
}
}
//ToList ();
Where (x => (x.FieldType.Assembly == hotFormsAssembly && x.FieldType.Name == "State`1")|| Attribute.IsDefined (x, typeof (StateAttribute))).ToList ();
//if (properties.Any()) {
// foreach(var prop in properties) {
// var child = prop.GetValue (this) as BindingObject;
// if (child != null) {
// SetProperty(child,prop.Name);
// }
// }
//}
if (fields.Any ()) {
foreach (var field in fields) {

Просмотреть файл

@ -6,14 +6,25 @@ using System.Runtime.CompilerServices;
namespace HotForms {
[AttributeUsage (AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter)]
[AttributeUsage (AttributeTargets.Field)]
public class StateAttribute : Attribute {
}
public interface IState {
public class State<T> : BindingObject {
public State(T value)
{
Value = value;
}
public State ()
{
}
public T Value {
get => GetProperty<T> ();
set => SetProperty (value);
}
}
public class StateBuilder : IDisposable {
static List<State> currentStates = new List<State> ();
public static State CurrentState => currentStates.LastOrDefault ();