前言:mvvm框架不用不知道,一用发现这玩意还是挺好的,就是第一次的时候比较麻烦。以后维护或者换皮之类的老方便了。
[设计思路,还是挺有用的,umvvm](https://www.cnblogs.com/OceanEyes/p/unity3d_framework_designing_get_started_with_mvvm_part1.html#_label3)
[良心,还在维护者呢,老方便了](https://github.com/vovgou/loxodon-framework/blob/)
md里面一大堆呢,实现一个案例
账号显示数据
AccountViewModel.cs
```csharp using Loxodon.Framework.Observables; namespace Test { public class AccountViewModel : ObservableObject { private int id; private string userName; private string userPassword; private bool remember; private ObservableProperty<string> address = new ObservableProperty<string>(); public int ID { get => this.id; set { Set<int>(ref this.id, value, nameof(ID)); } } public string UserName { get => this.userName; set { Set<string>(ref this.userName, value, nameof(UserName)); } } public string UserPassword { get => this.userPassword; set { Set<string>(ref this.userPassword, value, nameof(UserPassword)); } } public bool Remember { get => this.remember; set { Set<bool>(ref this.remember, value, nameof(Remember)); } } public ObservableProperty<string> Address => this.address; } } ``` 数据绑定 DatabindingViewModel.cs ```csharp using Loxodon.Framework.Observables; using Loxodon.Framework.ViewModels; using UnityEngine; namespace Test { public class DatabindingViewModel : ViewModelBase { private AccountViewModel account; private bool remember; private string userName; private string userPassword; private ObservableDictionary<string, string> errors = new ObservableDictionary<string, string>(); public AccountViewModel Account { get => this.account; set { this.Set<AccountViewModel>(ref this.account, value, nameof(this.Account)); } } public bool Remember { get => this.remember; set { this.Set<bool>(ref this.remember, value, nameof(this.Remember)); } } public string UserName { get => this.userName; set { this.Set<string>(ref this.userName, value, nameof(this.UserName)); } } public string UserPassword { get => this.userPassword; set { this.Set<string>(ref this.userPassword, value, nameof(this.UserPassword)); } } public ObservableDictionary<string, string> Errors { get => this.errors; set { this.Set<ObservableDictionary<string, string>>(ref this.errors, value, nameof(this.Errors)); } } public void OnValueUserNameChanged(string value) { Debug.Log($"userName is {value}"); UserName = value; } public void OnValueUserPasswordChanged(string value) { Debug.Log($"userPassword is {value}"); UserPassword = value; } public void OnSubmit() { if (string.IsNullOrEmpty(this.userName) || string.IsNullOrEmpty(this.userPassword)) { this.errors["errorMessage"] = "please input userName or userPassword ! "; return; } this.errors.Clear(); this.Account.UserName = this.UserName; this.Account.UserPassword = this.UserPassword; } } } ```View Code
显示层
DatabindingExample.cs
```csharp using System.Globalization; using Loxodon.Framework.Binding; using Loxodon.Framework.Binding.Builder; using Loxodon.Framework.Binding.Contexts; using Loxodon.Framework.Contexts; using Loxodon.Framework.Localizations; using Loxodon.Framework.Views; using TMPro; using UnityEngine.UI; namespace Test { public class DatabindingExample : UIView { public TMP_Text userName; public TMP_Text userPassword; public TMP_Text remember; public TMP_InputField inpName; public TMP_InputField inpPassword; public Toggle togRemember; public Button submit; protected override void Awake() { base.Awake(); ApplicationContext context = Context.GetApplicationContext(); BindingServiceBundle bindingServiceBundle = new BindingServiceBundle(context.GetContainer()); bindingServiceBundle.Start(); CultureInfo cultureInfo = Locale.GetCultureInfo(); var provider = new DefaultDataProvider("LocalizationTutorials", new XmlDocumentParser()); // Localization.Current = Localization(cultureInfo); } protected override void Start() { base.Start(); AccountViewModel account = new AccountViewModel() { ID = 1, UserName = "", UserPassword = "", }; account.Address.Value = "wodiu?"; DatabindingViewModel databindingViewModel = new DatabindingViewModel() { Account = account }; IBindingContext bindingContext = this.BindingContext(); bindingContext.DataContext = databindingViewModel; BindingSet<DatabindingExample, DatabindingViewModel> bindingSet; bindingSet = new BindingSet<DatabindingExample, DatabindingViewModel>(bindingContext, this); bindingSet.Bind(this.userName).For(v => v.text).To(vm => vm.UserName).OneWay(); bindingSet.Bind(this.userPassword).For(v => v.text).To(vm => vm.UserPassword).OneWay(); bindingSet.Bind(this.remember).For(v => v.text).To(vm => vm.Remember).OneWay(); bindingSet.Bind(this.inpName).For(v => v.text).To(vm => vm.UserName).TwoWay(); bindingSet.Bind(this.inpPassword).For(v => v.text).To(vm => vm.UserPassword).TwoWay(); bindingSet.Bind(this.remember).For(v => v.text).To(vm => vm.Remember ? "true" : "false").TwoWay(); bindingSet.Build(); // var staticBindingSet = this.CreateBindingSet<DatabindingExample>(); // staticBindingSet.Bind() this.submit.onClick.AddListener(() => { databindingViewModel.OnSubmit(); }); this.inpName.onValueChanged.AddListener((str) => { databindingViewModel.OnValueUserNameChanged(str); }); this.inpPassword.onValueChanged.AddListener((str) => { databindingViewModel.OnValueUserPasswordChanged(str); }); } } } ```View Code
还是挺好的。
标签:string,MVVM,userPassword,vm,value,Unity,笔记,using,public From: https://www.cnblogs.com/nianxiaoyu/p/18633297