MVC:
MVP: 绑定 model 层和 view 层
1. model 层定义 ReactiveProperty 变量
public class TodoItem
{
public BoolReactiveProperty Completed = new BoolReactiveProperty (false);
public StringReactiveProperty Content = new StringReactiveProperty (string.Empty);
}
2. view 层使用 ,更新 Content 的值
BtnUpdate.onClick.AddListener(() =>
{
mSelectedItemModel.Content.Value = InputField.text;
});
3.订阅,例如 mModel.Content.Value 值发生变化 ,view层 随即更新
mModel.Content.Subscribe(content =>
{
Content.text = content;
});
代码示例:
using QF;
using QF.Extensions;
using QF.Res;
using QFramework;
using QFramework.Afra;
using System.Collections;
using System.Collections.Generic;
using UniRx;
using UnityEngine;
/// <summary>
/// TodoApp
/// 1. 完成,未完成
/// 2. 列表/待办事项
/// 3. 增加,删除,更改待办事项
///
/// 有一个列表,列表里多个待办事项, 待办事项能够完成未完成 ,内容
/// </summary>
public class TodoListApp : MonoBehaviour
{
public TodoList mModel;
void Start()
{
ResMgr.Init();
UIMgr.SetResolution(1080, 1920, 0);
mModel = TodoList.Load();
// 打开 panel
UIMgr.OpenPanel<UITodoList>(new UITodoListData()
{
// 将Model层数据 传递到 UIPanel 控制层
Model = mModel
}) ;
}
private void OnApplicationQuit()
{
mModel.Save();
}
}
public class TodoList
{
public List<TodoItem> m_TodoItems = new List<TodoItem>() {};
// Json 反序列化(读取)
public static TodoList Load()
{
var jsonContent = PlayerPrefs.GetString("TodoListData1", string.Empty);
return jsonContent.IsNullOrEmpty() ? new TodoList(): jsonContent.FromJson<TodoList>();
}
//Json 序列化(写入)
public void Save()
{
PlayerPrefs.SetString("TodoListData1", this.ToJson());
}
}
public class TodoItem
{
public BoolReactiveProperty Completed = new BoolReactiveProperty (false);
public StringReactiveProperty Content = new StringReactiveProperty (string.Empty);
}
/****************************************************************************
* 2019.10 DESKTOP-SF453R4
****************************************************************************/
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using QFramework;
using QF.Extensions;
using UniRx;
namespace QFramework.Afra
{
public enum UIInputContentState
{
Create,
Modify,
}
public partial class UIInputContent : UIElement
{
public ReactiveProperty<UIInputContentState> State = new UniRx.ReactiveProperty<UIInputContentState>(UIInputContentState.Create);
TodoItem mSelectedItemModel;
TodoList mTodoListModel;
public void Init(TodoList todoListModel)
{
mTodoListModel = todoListModel;
}
public void ModifyState(TodoItem selectedItemModel)
{
mSelectedItemModel = selectedItemModel;
State.Value = UIInputContentState.Modify;
InputField.text = selectedItemModel.Content.Value;
}
private void Awake()
{
State.Subscribe(State =>
{
if(State == UIInputContentState.Create)
{
BtnUpdate.Hide();
BtnCancel.Hide();
InputField.text = string.Empty;
}
else
{
BtnUpdate.Show();
BtnUpdate.interactable = false;
BtnCancel.Show();
}
});
BtnUpdate.onClick.AddListener(() =>
{
mSelectedItemModel.Content.Value = InputField.text;
State.Value = UIInputContentState.Create;
});
BtnCancel.onClick.AddListener(()=>{
State.Value = UIInputContentState.Create;
});
InputField.onValueChanged.AddListener(content =>
{
if (State.Value == UIInputContentState.Modify)
{
if (BtnUpdate.interactable && content == mSelectedItemModel.Content.Value)
{
BtnUpdate.interactable = false;
}
else if (!BtnUpdate.interactable && content != mSelectedItemModel.Content.Value)
{
BtnUpdate.interactable = true;
}
}
});
// 监听输入完成
InputField.onEndEdit.AddListener(content =>
{
if (State.Value == UIInputContentState.Create)
{
if (Input.GetKeyDown(KeyCode.Return))
{
var addedItem = new TodoItem();
addedItem.Content.Value = content;
addedItem.Completed.Value = false;
SendMsg(new OnTodoItemAddedMsg(addedItem));
}
InputField.text = string.Empty;
}
});
}
protected override void OnBeforeDestroy()
{
}
}
}
/****************************************************************************
* 2019.10 PS2019HINRKUYZ
****************************************************************************/
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using QFramework;
using QF.Extensions;
using UniRx;
namespace QFramework.Afra
{
public partial class UITodoItem : UIElement
{
TodoItem mModel;
// 控制层到显示层
public void Init(TodoItem model)
{
mModel = model;
Completed.isOn = mModel.Completed.Value;
Completed.onValueChanged.AddListener(on=> {
if (on)
{
mModel.Completed.Value = on;
this.DestroyGameObj();
}
});
Button.onClick.AddListener(() =>
{
Debug.Log("clicked");
SendMsg(new OnTodoItemSelectedMsg(mModel));
});
mModel.Content.Subscribe(content =>
{
Content.text = content;
});
}
private void Awake()
{
}
protected override void OnBeforeDestroy()
{
}
}
}