当前位置:网站首页>Loxodonframework quick start
Loxodonframework quick start
2022-07-07 09:25:00 【heater404】
One 、 brief introduction
LoxodonFramework What is it? , What is it for . I won't introduce it here , Direct up link :vovgou/loxodon-framework: An MVVM & Databinding framework that can use C# and Lua to develop games (github.com)
There is a full introduction and tutorial in the link , This article mainly records the experience of learning and understanding the framework .
Two 、 Quick start
I have used it before WPF Of MVVM frame –Prism, So in unity Also want to find a similar framework , that LoxodonFramework Can it also be like Prism So easy to use ? Now we can simply try .
3、 ... and 、 Example
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-ZRXRgMVy-1638002288693)(C:\Users\DELL\AppData\Roaming\Typora\typora-user-images\image-20211127145949566.png)]](/img/f3/748ec0499b19401367928b626e01e6.jpg)
We have created the above project , Want to do such a thing : After entering a value in the input box, the slider will slide to the corresponding value , When the slider slides, the input box will display the corresponding value .
Just imagine , If not applicable MVVM Thought , We can mount a script , These two controls are found in the script properties , Then assign values to each other during operation , This should be a very simple job . But use MVVM How should I do it ?
3.1Model
Because the data model of the example is very simple , Basic data types can be used , So this layer is omitted .
3.2ViewModel
This layer is our interaction logic . Data update and event response logic code are here .
3.3View
Here is UI layer , Only controls are involved , It does not involve data business . Let's talk about the train of thought , Usually in UI Control to mount a script , This script is what we need View layer , Of course, it will inherit UIView class , Then bind in the script . Binding is mainly to UI The properties or events of the control are bound to ViewModel Properties or methods in . therefore ViewModel It's not a script , There is no need to mount on the control .

Then take a look at the specific implementation of the code :
public class SampleViewModel : ViewModelBase
{
private float sliderValue;
public float SliderValue
{
get {
return sliderValue; }
set {
Set<float>(ref sliderValue, value, "SliderValue"); }
}
public void OnSliderValueChanged(float newValue)
{
if (newValue != sliderValue)
{
this.SliderValue = newValue;
Debug.Log($"NewValue:{
newValue}");
}
}
public void OnInputFieldChanged(string str)
{
this.SliderValue = int.Parse(str);
}
}
public class SampleView : UIView
{
public InputField input;
public Slider slider;
protected override void Awake()
{
// Get the application context
ApplicationContext context = Context.GetApplicationContext();
// Start the data binding service
BindingServiceBundle bindingService = new BindingServiceBundle(context.GetContainer());
bindingService.Start();
}
protected override void Start()
{
// Get the data binding context
IBindingContext bindingContext = this.BindingContext();
bindingContext.DataContext = new SampleViewModel {
SliderValue = 98 };
// binding UI Control to view model
BindingSet<SampleView, SampleViewModel> bindingSet;
bindingSet = this.CreateBindingSet<SampleView, SampleViewModel>();
// take VM The value in is bound to UI Controls , Note that here is one-way binding vm=>v
bindingSet.Bind(this.input).For(v => v.text).To(vm => vm.SliderValue).OneWay();
bindingSet.Bind(this.slider).For(v => v.value).To(vm => vm.SliderValue).OneWay();
// Then bind the event of the control to vm The methods of , Be careful To To use generics , The parameters of the method are consistent with those of the event method .
// Of course, it can also be bound to commands ( Later we will introduce )
bindingSet.Bind(this.slider).For(v => v.onValueChanged).To<float>(vm => vm.OnSliderValueChanged);
bindingSet.Bind(this.input).For(v => v.onEndEdit).To<string>(vm=>vm.OnInputFieldChanged);
// This sentence should not be forgotten
bindingSet.Build();
}
}
The idea of binding is similar .
Four 、 About binding
There is such a piece of code in the above example :
// Get the data binding context
IBindingContext bindingContext = this.BindingContext();
bindingContext.DataContext = new SampleViewModel {
SliderValue = 98 };
The above code can also be modified as follows :
this.SetDataContext(new SampleViewModel {
SliderValue = 98 });
Behaviour Class extends SetDataContext Method , Its internal implementation also uses IBindingContext Interface .
Yes WPF Bound students must be familiar , you 're right , There's also DataContext The concept of . About DataContext, The author of the framework has such a description in the document :
Generally speaking, data binding is initialized in the view creation function , adopt BindingSet To configure the binding relationship between the view control and the view model , When calling BindingSet Of Build Function time ,
Binder Will create BindingSet All binding pairs in , The created binding pair will be saved in the current view BindingContext in .BindingContext Automatically create on the first call , Same as
Automatically generates a BindingContextLifecycle Script , Hang on the current view object , It controls BindingContext Life cycle of , When the view is destroyed ,BindingContext Meeting
And then destroy it , Store in BindingContext The binding relationship pairs in will also be destroyed .
therefore , about View Layer of DataContext, We can set it externally , Or later changes , It is not necessarily the same as the example in build Set it up .
边栏推荐
- DRF authentication, permissions, and flow restrictions (only for views in DRF)
- liunx命令
- 浏览器中如何让视频倍速播放
- Huawei hcip datacom core_ 03day
- sqlplus乱码问题,求解答
- What is the rating of Huishang futures company? Is it safe to open an account? I want to open an account, OK?
- When inputting an expression in the input box, an error is reported: incorrect string value:'\xf0\x9f... ' for column 'XXX' at row 1
- 信息安全实验四:Ip包监视程序实现
- Install pyqt5 and Matplotlib module
- Netease Cloud Wechat applet
猜你喜欢

How to pass the PMP Exam in a short time?

Install pyqt5 and Matplotlib module

2020 year end summary

二叉树高频题型

Integer or int? How to select data types for entity classes in ORM

Storage of data in memory

C language pointer (Part 1)

正则匹配以XXX开头的,XXX结束的

Pycharm importing third-party libraries
![[cloud native] Devops (I): introduction to Devops and use of code tool](/img/e0/6152b3248ce19d0dbba3ac4845eb65.png)
[cloud native] Devops (I): introduction to Devops and use of code tool
随机推荐
Data association between two interfaces of postman
Unity shader (to achieve a simple material effect with adjustable color attributes only)
Upgrade Alibaba cloud RDS (relational database service) instance to com mysql. jdbc. exceptions. Troubleshooting of jdbc4.communicationsexception
数据在内存中的存储
C language pointer (Part 1)
Original collection of hardware bear (updated on May 2022)
Storage of data in memory
Postman interface test (II. Set global variables \ sets)
shake数据库中怎么使用Mongo-shake实现MongoDB的双向同步啊?
Pycharm create a new file and add author information
Entity of cesium data visualization (Part 1)
消费互联网的产业链其实是很短的,它仅仅承接平台上下游的对接和撮合的角色
Leetcode daily questions (2316. count unreachable pairs of nodes in an undirected graph)
C language pointer (special article)
Run can start normally, and debug doesn't start or report an error, which seems to be stuck
Locust performance test 2 (interface request)
Variable parameter of variable length function
十二、排序
Leetcode question brushing record (array) combination sum, combination sum II
PMP certificate preparation experience sharing