首页 > 其他分享 >Unity笔记之MVVM框架推荐

Unity笔记之MVVM框架推荐

时间:2024-12-26 16:22:26浏览次数:3  
标签:string MVVM userPassword vm value Unity 笔记 using public

前言: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

相关文章

  • QOwnNotes(开源Markdown笔记本) v24.12.6
    QOwnNotes是开源的记事本,具有Markdown支持和针对GNU/Linux,MacOSX和Windows的待办事项列表管理器,可与NextcloudNotes和ownCloudNotes一起使用。您可以使用QOwnNotes写下您的想法,并稍后在移动设备上进行编辑或搜索,例如使用Android的NextcloudNotes或Nextcloud/o......
  • 365编程自学笔记--001
    day001我打算用365天,也就是一年的时间。从零开始学习编程和AI应用,目标是在明年的圣诞节之前,能够基本掌握全栈的web和移动端的开发,以及AI相关的应用与开发。以使得自己能够跟上这个飞速发展的AI时代,成为AI时代的首批移民。经过一段时间的调研和比选,最终觉得从coderwhy王红......
  • 【Unity 图标资源包】RPG Engineering Skill Icons 专为角色扮演游戏(RPG)开发者设计的
    RPGEngineeringSkillIcons是一款专为角色扮演游戏(RPG)开发者设计的图标资源包,旨在为游戏中的工程技能和工艺系统提供高质量的图标。这些图标特别适用于RPG游戏中的技能树、任务栏、物品制作系统等,帮助开发者清晰地展示各种与工程和工艺相关的技能和操作。无论是打造武器......
  • 营销相关笔记2:电力营销的业务办理和计量计费
    电力营销的业务办理和计量计费转:电力知识图谱:电力营销(下)-知乎一.业务办理电力营销是和客户打交道的最前线,两大业务:一是市场销售,二是客户需求受理和交付,即业务办理。 在一些市场化主导的售电公司里,市场部和营销部可能会分开。但在电网公司,都是统一的营销部,而且业务办理是远......
  • git 命令笔记
    git工作机制工作区缓存区版本库未跟踪已跟踪(未修改已修改已暂存)git常用命令(默认在创建master分支)gitinit初始化仓库gitadd[文件名]添加指定文件名到缓存区gitadd.添加全部文件到缓存区gitstatus工作区文件的状态gitcommit-m"firstcommit"[文件]提......
  • GO 学习笔记之五 编程(三)单元测试 ( VSCODE工具运行 )
    一、目的   当本地写好了一个go文件之后,希望通过单元测试来验证写的go文件中代码逻辑是否正确,减少后期发现问题成本。本案例介绍如何在VSCODE工具中触发执行和单点debug执行单元测试函数(注意:VSCODE工具有一个致命不好的点就是慢)二、编写单元测试  假设:当前业务逻辑go......
  • 网络编程学习笔记
    1.网络编程1.1.介绍(IO)怎么学:理解(应用层)、多回顾、多练、自主要求:互动、认真听、互相尊重1.1.1.认识网络网络:多设备通信认识网络1.2.IP地址1.2.1.基本概念1.IP地址是Internet中主机的标识2.Internet中的主机要与别的机器通信必须具有一个IP地址3.IP地址为32......
  • 弹性波动力学笔记(五) 应变张量简介下
    2.5ProofthatstraintensorisatensorToprovethat\(\varepsilon_{ij}\)isatensoritisnecessarytoshowthatunderarotationofaxisthecomponentstransformaccordingto\[\varepsilon_{kl}^{'}=a_{ki}a_{lj}\varepsilon_{ij}\tag{2.5.1}......
  • day1——伙伴匹配学习笔记
    了解了一些Java8特性lambda语法(parameters)->expression(parameters)->{statements;}相当于是函数的另一种写法,更优雅。stream()用法以及和parallelStream()的一些对比详见博客java8新特性-流-stream()和parallelStream()求求你们了,别再乱用parallelStream......
  • 《程序员修炼之道:从小工到专家》读书笔记——9
    生活平衡与职业规划程序员的职业生涯不仅仅是技术的不断突破,也需要有良好的生活平衡。长时间的高强度工作可能会导致疲劳、压力过大,甚至影响到身体健康。因此,专家程序员往往会更加注重工作与生活的平衡,保持身心健康,避免过度疲劳。在生活平衡方面,专家程序员通常会设定清晰的工作......